1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Written by Tom Tromey <tromey@redhat.com>
13 // Define VERIFY_DEBUG to enable debugging output.
19 #include <java-insns.h>
20 #include <java-interp.h>
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
32 #endif /* VERIFY_DEBUG */
35 static void debug_print (const char *fmt
, ...)
36 __attribute__ ((format (printf
, 1, 2)));
39 debug_print (const char *fmt
, ...)
44 vfprintf (stderr
, fmt
, ap
);
46 #endif /* VERIFY_DEBUG */
49 class _Jv_BytecodeVerifier
53 static const int FLAG_INSN_START
= 1;
54 static const int FLAG_BRANCH_TARGET
= 2;
59 struct subr_entry_info
;
64 // The PC corresponding to the start of the current instruction.
67 // The current state of the stack, locals, etc.
70 // We store the state at branch targets, for merging. This holds
74 // We keep a linked list of all the PCs which we must reverify.
75 // The link is done using the PC values. This is the head of the
79 // We keep some flags for each instruction. The values are the
80 // FLAG_* constants defined above.
83 // We need to keep track of which instructions can call a given
84 // subroutine. FIXME: this is inefficient. We keep a linked list
85 // of all calling `jsr's at at each jsr target.
88 // We keep a linked list of entries which map each `ret' instruction
89 // to its unique subroutine entry point. We expect that there won't
90 // be many `ret' instructions, so a linked list is ok.
91 subr_entry_info
*entry_points
;
93 // The current top of the stack, in terms of slots.
95 // The current depth of the stack. This will be larger than
96 // STACKTOP when wide types are on the stack.
99 // The bytecode itself.
100 unsigned char *bytecode
;
102 _Jv_InterpException
*exception
;
105 jclass current_class
;
107 _Jv_InterpMethod
*current_method
;
109 // A linked list of utf8 objects we allocate. This is really ugly,
110 // but without this our utf8 objects would be collected.
111 linked_utf8
*utf8_list
;
119 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
121 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
122 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
125 r
->length
= val
->length
;
127 memcpy (r
->data
, val
->data
, val
->length
+ 1);
129 linked_utf8
*lu
= (linked_utf8
*) _Jv_Malloc (sizeof (linked_utf8
));
131 lu
->next
= utf8_list
;
137 // This enum holds a list of tags for all the different types we
138 // need to handle. Reference types are treated specially by the
144 // The values for primitive types are chosen to correspond to values
145 // specified to newarray.
155 // Used when overwriting second word of a double or long in the
156 // local variables. Also used after merging local variable states
157 // to indicate an unusable value.
162 // There is an obscure special case which requires us to note when
163 // a local variable has not been used by a subroutine. See
164 // push_jump_merge for more information.
165 unused_by_subroutine_type
,
167 // Everything after `reference_type' must be a reference type.
170 unresolved_reference_type
,
171 uninitialized_reference_type
,
172 uninitialized_unresolved_reference_type
175 // Return the type_val corresponding to a primitive signature
176 // character. For instance `I' returns `int.class'.
177 type_val
get_type_val_for_signature (jchar sig
)
210 verify_fail ("invalid signature");
215 // Return the type_val corresponding to a primitive class.
216 type_val
get_type_val_for_signature (jclass k
)
218 return get_type_val_for_signature ((jchar
) k
->method_count
);
221 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
222 // TARGET haven't been prepared.
223 static bool is_assignable_from_slow (jclass target
, jclass source
)
225 // This will terminate when SOURCE==Object.
228 if (source
== target
)
231 if (target
->isPrimitive () || source
->isPrimitive ())
234 // Check array case first because we can have an array whose
235 // component type is not prepared; _Jv_IsAssignableFrom
236 // doesn't handle this correctly.
237 if (target
->isArray ())
239 if (! source
->isArray ())
241 target
= target
->getComponentType ();
242 source
= source
->getComponentType ();
244 // _Jv_IsAssignableFrom can handle a target which is an
245 // interface even if it hasn't been prepared.
246 else if ((target
->state
> JV_STATE_LINKED
|| target
->isInterface ())
247 && source
->state
> JV_STATE_LINKED
)
248 return _Jv_IsAssignableFrom (target
, source
);
249 else if (target
->isInterface ())
251 for (int i
= 0; i
< source
->interface_count
; ++i
)
253 // We use a recursive call because we also need to
254 // check superinterfaces.
255 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
258 source
= source
->getSuperclass ();
262 else if (target
== &java::lang::Object::class$
)
264 else if (source
->isInterface ()
265 || source
== &java::lang::Object::class$
)
268 source
= source
->getSuperclass ();
272 // This is used to keep track of which `jsr's correspond to a given
276 // PC of the instruction just after the jsr.
282 // This is used to keep track of which subroutine entry point
283 // corresponds to which `ret' instruction.
284 struct subr_entry_info
286 // PC of the subroutine entry point.
288 // PC of the `ret' instruction.
291 subr_entry_info
*next
;
294 // The `type' class is used to represent a single type in the
300 // Some associated data.
303 // For a resolved reference type, this is a pointer to the class.
305 // For other reference types, this it the name of the class.
308 // This is used when constructing a new object. It is the PC of the
309 // `new' instruction which created the object. We use the special
310 // value -2 to mean that this is uninitialized, and the special
311 // value -1 for the case where the current method is itself the
315 static const int UNINIT
= -2;
316 static const int SELF
= -1;
318 // Basic constructor.
321 key
= unsuitable_type
;
326 // Make a new instance given the type tag. We assume a generic
327 // `reference_type' means Object.
332 if (key
== reference_type
)
333 data
.klass
= &java::lang::Object::class$
;
337 // Make a new instance given a class.
340 key
= reference_type
;
345 // Make a new instance given the name of a class.
346 type (_Jv_Utf8Const
*n
)
348 key
= unresolved_reference_type
;
361 // These operators are required because libgcj can't link in
363 void *operator new[] (size_t bytes
)
365 return _Jv_Malloc (bytes
);
368 void operator delete[] (void *mem
)
373 type
& operator= (type_val k
)
381 type
& operator= (const type
& t
)
389 // Promote a numeric type.
392 if (key
== boolean_type
|| key
== char_type
393 || key
== byte_type
|| key
== short_type
)
398 // If *THIS is an unresolved reference type, resolve it.
399 void resolve (_Jv_BytecodeVerifier
*verifier
)
401 if (key
!= unresolved_reference_type
402 && key
!= uninitialized_unresolved_reference_type
)
405 using namespace java::lang
;
406 java::lang::ClassLoader
*loader
407 = verifier
->current_class
->getClassLoader();
408 // We might see either kind of name. Sigh.
409 if (data
.name
->data
[0] == 'L'
410 && data
.name
->data
[data
.name
->length
- 1] == ';')
411 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
413 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
415 key
= (key
== unresolved_reference_type
417 : uninitialized_reference_type
);
420 // Mark this type as the uninitialized result of `new'.
421 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
423 if (key
== reference_type
)
424 key
= uninitialized_reference_type
;
425 else if (key
== unresolved_reference_type
)
426 key
= uninitialized_unresolved_reference_type
;
428 verifier
->verify_fail ("internal error in type::uninitialized");
432 // Mark this type as now initialized.
433 void set_initialized (int npc
)
435 if (npc
!= UNINIT
&& pc
== npc
436 && (key
== uninitialized_reference_type
437 || key
== uninitialized_unresolved_reference_type
))
439 key
= (key
== uninitialized_reference_type
441 : unresolved_reference_type
);
447 // Return true if an object of type K can be assigned to a variable
448 // of type *THIS. Handle various special cases too. Might modify
449 // *THIS or K. Note however that this does not perform numeric
451 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
453 // Any type is compatible with the unsuitable type.
454 if (key
== unsuitable_type
)
457 if (key
< reference_type
|| k
.key
< reference_type
)
460 // The `null' type is convertible to any reference type.
461 // FIXME: is this correct for THIS?
462 if (key
== null_type
|| k
.key
== null_type
)
465 // Any reference type is convertible to Object. This is a special
466 // case so we don't need to unnecessarily resolve a class.
467 if (key
== reference_type
468 && data
.klass
== &java::lang::Object::class$
)
471 // An initialized type and an uninitialized type are not
473 if (isinitialized () != k
.isinitialized ())
476 // Two uninitialized objects are compatible if either:
477 // * The PCs are identical, or
478 // * One PC is UNINIT.
479 if (! isinitialized ())
481 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
485 // Two unresolved types are equal if their names are the same.
488 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
491 // We must resolve both types and check assignability.
493 k
.resolve (verifier
);
494 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
499 return key
== void_type
;
504 return key
== long_type
|| key
== double_type
;
507 // Return number of stack or local variable slots taken by this
511 return iswide () ? 2 : 1;
514 bool isarray () const
516 // We treat null_type as not an array. This is ok based on the
517 // current uses of this method.
518 if (key
== reference_type
)
519 return data
.klass
->isArray ();
520 else if (key
== unresolved_reference_type
)
521 return data
.name
->data
[0] == '[';
527 return key
== null_type
;
530 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
533 if (key
!= reference_type
)
535 return data
.klass
->isInterface ();
538 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
541 if (key
!= reference_type
)
543 using namespace java::lang::reflect
;
544 return Modifier::isAbstract (data
.klass
->getModifiers ());
547 // Return the element type of an array.
548 type
element_type (_Jv_BytecodeVerifier
*verifier
)
550 // FIXME: maybe should do string manipulation here.
552 if (key
!= reference_type
)
553 verifier
->verify_fail ("programmer error in type::element_type()", -1);
555 jclass k
= data
.klass
->getComponentType ();
556 if (k
->isPrimitive ())
557 return type (verifier
->get_type_val_for_signature (k
));
561 // Return the array type corresponding to an initialized
562 // reference. We could expand this to work for other kinds of
563 // types, but currently we don't need to.
564 type
to_array (_Jv_BytecodeVerifier
*verifier
)
566 // Resolving isn't ideal, because it might force us to load
567 // another class, but it's easy. FIXME?
568 if (key
== unresolved_reference_type
)
571 if (key
== reference_type
)
572 return type (_Jv_GetArrayClass (data
.klass
,
573 data
.klass
->getClassLoader ()));
575 verifier
->verify_fail ("internal error in type::to_array()");
578 bool isreference () const
580 return key
>= reference_type
;
588 bool isinitialized () const
590 return (key
== reference_type
592 || key
== unresolved_reference_type
);
595 bool isresolved () const
597 return (key
== reference_type
599 || key
== uninitialized_reference_type
);
602 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
604 // The way this is written, we don't need to check isarray().
605 if (key
== reference_type
)
607 jclass k
= data
.klass
;
608 while (k
->isArray () && ndims
> 0)
610 k
= k
->getComponentType ();
616 // We know KEY == unresolved_reference_type.
617 char *p
= data
.name
->data
;
618 while (*p
++ == '[' && ndims
-- > 0)
623 verifier
->verify_fail ("array type has fewer dimensions than required");
626 // Merge OLD_TYPE into this. On error throw exception.
627 bool merge (type
& old_type
, bool local_semantics
,
628 _Jv_BytecodeVerifier
*verifier
)
630 bool changed
= false;
631 bool refo
= old_type
.isreference ();
632 bool refn
= isreference ();
635 if (old_type
.key
== null_type
)
637 else if (key
== null_type
)
642 else if (isinitialized () != old_type
.isinitialized ())
643 verifier
->verify_fail ("merging initialized and uninitialized types");
646 if (! isinitialized ())
650 else if (old_type
.pc
== UNINIT
)
652 else if (pc
!= old_type
.pc
)
653 verifier
->verify_fail ("merging different uninitialized types");
657 && ! old_type
.isresolved ()
658 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
660 // Types are identical.
665 old_type
.resolve (verifier
);
667 jclass k
= data
.klass
;
668 jclass oldk
= old_type
.data
.klass
;
671 while (k
->isArray () && oldk
->isArray ())
674 k
= k
->getComponentType ();
675 oldk
= oldk
->getComponentType ();
678 // This loop will end when we hit Object.
681 if (is_assignable_from_slow (k
, oldk
))
683 k
= k
->getSuperclass ();
689 while (arraycount
> 0)
691 java::lang::ClassLoader
*loader
692 = verifier
->current_class
->getClassLoader();
693 k
= _Jv_GetArrayClass (k
, loader
);
701 else if (refo
|| refn
|| key
!= old_type
.key
)
705 // If we're merging into an "unused" slot, then we
706 // simply accept whatever we're merging from.
707 if (key
== unused_by_subroutine_type
)
712 else if (old_type
.key
== unused_by_subroutine_type
)
716 // If we already have an `unsuitable' type, then we
717 // don't need to change again.
718 else if (key
!= unsuitable_type
)
720 key
= unsuitable_type
;
725 verifier
->verify_fail ("unmergeable type");
731 void print (void) const
736 case boolean_type
: c
= 'Z'; break;
737 case byte_type
: c
= 'B'; break;
738 case char_type
: c
= 'C'; break;
739 case short_type
: c
= 'S'; break;
740 case int_type
: c
= 'I'; break;
741 case long_type
: c
= 'J'; break;
742 case float_type
: c
= 'F'; break;
743 case double_type
: c
= 'D'; break;
744 case void_type
: c
= 'V'; break;
745 case unsuitable_type
: c
= '-'; break;
746 case return_address_type
: c
= 'r'; break;
747 case continuation_type
: c
= '+'; break;
748 case unused_by_subroutine_type
: c
= '_'; break;
749 case reference_type
: c
= 'L'; break;
750 case null_type
: c
= '@'; break;
751 case unresolved_reference_type
: c
= 'l'; break;
752 case uninitialized_reference_type
: c
= 'U'; break;
753 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
755 debug_print ("%c", c
);
757 #endif /* VERIFY_DEBUG */
760 // This class holds all the state information we need for a given
764 // Current top of stack.
766 // Current stack depth. This is like the top of stack but it
767 // includes wide variable information.
771 // The local variables.
773 // This is used in subroutines to keep track of which local
774 // variables have been accessed.
776 // If not 0, then we are in a subroutine. The value is the PC of
777 // the subroutine's entry point. We can use 0 as an exceptional
778 // value because PC=0 can never be a subroutine.
780 // This is used to keep a linked list of all the states which
781 // require re-verification. We use the PC to keep track.
783 // We keep track of the type of `this' specially. This is used to
784 // ensure that an instance initializer invokes another initializer
785 // on `this' before returning. We must keep track of this
786 // specially because otherwise we might be confused by code which
787 // assigns to locals[0] (overwriting `this') and then returns
788 // without really initializing.
791 // INVALID marks a state which is not on the linked list of states
792 // requiring reverification.
793 static const int INVALID
= -1;
794 // NO_NEXT marks the state at the end of the reverification list.
795 static const int NO_NEXT
= -2;
802 local_changed
= NULL
;
805 state (int max_stack
, int max_locals
)
810 stack
= new type
[max_stack
];
811 for (int i
= 0; i
< max_stack
; ++i
)
812 stack
[i
] = unsuitable_type
;
813 locals
= new type
[max_locals
];
814 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
815 for (int i
= 0; i
< max_locals
; ++i
)
817 locals
[i
] = unsuitable_type
;
818 local_changed
[i
] = false;
824 state (const state
*orig
, int max_stack
, int max_locals
,
825 bool ret_semantics
= false)
827 stack
= new type
[max_stack
];
828 locals
= new type
[max_locals
];
829 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
830 copy (orig
, max_stack
, max_locals
, ret_semantics
);
841 _Jv_Free (local_changed
);
844 void *operator new[] (size_t bytes
)
846 return _Jv_Malloc (bytes
);
849 void operator delete[] (void *mem
)
854 void *operator new (size_t bytes
)
856 return _Jv_Malloc (bytes
);
859 void operator delete (void *mem
)
864 void copy (const state
*copy
, int max_stack
, int max_locals
,
865 bool ret_semantics
= false)
867 stacktop
= copy
->stacktop
;
868 stackdepth
= copy
->stackdepth
;
869 subroutine
= copy
->subroutine
;
870 for (int i
= 0; i
< max_stack
; ++i
)
871 stack
[i
] = copy
->stack
[i
];
872 for (int i
= 0; i
< max_locals
; ++i
)
874 // See push_jump_merge to understand this case.
876 locals
[i
] = type (copy
->local_changed
[i
]
878 : unused_by_subroutine_type
);
880 locals
[i
] = copy
->locals
[i
];
881 local_changed
[i
] = copy
->local_changed
[i
];
883 this_type
= copy
->this_type
;
884 // Don't modify `next'.
887 // Modify this state to reflect entry to an exception handler.
888 void set_exception (type t
, int max_stack
)
893 for (int i
= stacktop
; i
< max_stack
; ++i
)
894 stack
[i
] = unsuitable_type
;
896 // FIXME: subroutine handling?
899 // Modify this state to reflect entry into a subroutine.
900 void enter_subroutine (int npc
, int max_locals
)
903 // Mark all items as unchanged. Each subroutine needs to keep
904 // track of its `changed' state independently. In the case of
905 // nested subroutines, this information will be merged back into
906 // parent by the `ret'.
907 for (int i
= 0; i
< max_locals
; ++i
)
908 local_changed
[i
] = false;
911 // Merge STATE_OLD into this state. Destructively modifies this
912 // state. Returns true if the new state was in fact changed.
913 // Will throw an exception if the states are not mergeable.
914 bool merge (state
*state_old
, bool ret_semantics
,
915 int max_locals
, _Jv_BytecodeVerifier
*verifier
)
917 bool changed
= false;
919 // Special handling for `this'. If one or the other is
920 // uninitialized, then the merge is uninitialized.
921 if (this_type
.isinitialized ())
922 this_type
= state_old
->this_type
;
924 // Merge subroutine states. Here we just keep track of what
925 // subroutine we think we're in. We only check for a merge
926 // (which is invalid) when we see a `ret'.
927 if (subroutine
== state_old
->subroutine
)
931 else if (subroutine
== 0)
933 subroutine
= state_old
->subroutine
;
938 // If the subroutines differ, indicate that the state
939 // changed. This is needed to detect when subroutines have
945 if (state_old
->stacktop
!= stacktop
)
946 verifier
->verify_fail ("stack sizes differ");
947 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
949 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
953 // Merge local variables.
954 for (int i
= 0; i
< max_locals
; ++i
)
956 // If we're not processing a `ret', then we merge every
957 // local variable. If we are processing a `ret', then we
958 // only merge locals which changed in the subroutine. When
959 // processing a `ret', STATE_OLD is the state at the point
960 // of the `ret', and THIS is the state just after the `jsr'.
961 if (! ret_semantics
|| state_old
->local_changed
[i
])
963 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
970 // If we're in a subroutine, we must compute the union of
971 // all the changed local variables.
972 if (state_old
->local_changed
[i
])
979 // Throw an exception if there is an uninitialized object on the
980 // stack or in a local variable. EXCEPTION_SEMANTICS controls
981 // whether we're using backwards-branch or exception-handing
983 void check_no_uninitialized_objects (int max_locals
,
984 _Jv_BytecodeVerifier
*verifier
,
985 bool exception_semantics
= false)
987 if (! exception_semantics
)
989 for (int i
= 0; i
< stacktop
; ++i
)
990 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
991 verifier
->verify_fail ("uninitialized object on stack");
994 for (int i
= 0; i
< max_locals
; ++i
)
995 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
996 verifier
->verify_fail ("uninitialized object in local variable");
998 check_this_initialized (verifier
);
1001 // Ensure that `this' has been initialized.
1002 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1004 if (this_type
.isreference () && ! this_type
.isinitialized ())
1005 verifier
->verify_fail ("`this' is uninitialized");
1008 // Set type of `this'.
1009 void set_this_type (const type
&k
)
1014 // Note that a local variable was modified.
1015 void note_variable (int index
)
1018 local_changed
[index
] = true;
1021 // Mark each `new'd object we know of that was allocated at PC as
1023 void set_initialized (int pc
, int max_locals
)
1025 for (int i
= 0; i
< stacktop
; ++i
)
1026 stack
[i
].set_initialized (pc
);
1027 for (int i
= 0; i
< max_locals
; ++i
)
1028 locals
[i
].set_initialized (pc
);
1029 this_type
.set_initialized (pc
);
1032 // Return true if this state is the unmerged result of a `ret'.
1033 bool is_unmerged_ret_state (int max_locals
) const
1035 for (int i
= 0; i
< max_locals
; ++i
)
1037 if (locals
[i
].key
== unused_by_subroutine_type
)
1044 void print (const char *leader
, int pc
,
1045 int max_stack
, int max_locals
) const
1047 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1049 for (i
= 0; i
< stacktop
; ++i
)
1051 for (; i
< max_stack
; ++i
)
1053 debug_print (" [local] ");
1054 for (i
= 0; i
< max_locals
; ++i
)
1056 if (subroutine
== 0)
1057 debug_print (" | None");
1059 debug_print (" | %4d", subroutine
);
1060 debug_print (" | %p\n", this);
1063 inline void print (const char *, int, int, int) const
1066 #endif /* VERIFY_DEBUG */
1071 if (current_state
->stacktop
<= 0)
1072 verify_fail ("stack empty");
1073 type r
= current_state
->stack
[--current_state
->stacktop
];
1074 current_state
->stackdepth
-= r
.depth ();
1075 if (current_state
->stackdepth
< 0)
1076 verify_fail ("stack empty", start_PC
);
1082 type r
= pop_raw ();
1084 verify_fail ("narrow pop of wide type");
1090 type r
= pop_raw ();
1092 verify_fail ("wide pop of narrow type");
1096 type
pop_type (type match
)
1099 type t
= pop_raw ();
1100 if (! match
.compatible (t
, this))
1101 verify_fail ("incompatible type on stack");
1105 // Pop a reference type or a return address.
1106 type
pop_ref_or_return ()
1108 type t
= pop_raw ();
1109 if (! t
.isreference () && t
.key
!= return_address_type
)
1110 verify_fail ("expected reference or return address on stack");
1114 void push_type (type t
)
1116 // If T is a numeric type like short, promote it to int.
1119 int depth
= t
.depth ();
1120 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1121 verify_fail ("stack overflow");
1122 current_state
->stack
[current_state
->stacktop
++] = t
;
1123 current_state
->stackdepth
+= depth
;
1126 void set_variable (int index
, type t
)
1128 // If T is a numeric type like short, promote it to int.
1131 int depth
= t
.depth ();
1132 if (index
> current_method
->max_locals
- depth
)
1133 verify_fail ("invalid local variable");
1134 current_state
->locals
[index
] = t
;
1135 current_state
->note_variable (index
);
1139 current_state
->locals
[index
+ 1] = continuation_type
;
1140 current_state
->note_variable (index
+ 1);
1142 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1144 current_state
->locals
[index
- 1] = unsuitable_type
;
1145 // There's no need to call note_variable here.
1149 type
get_variable (int index
, type t
)
1151 int depth
= t
.depth ();
1152 if (index
> current_method
->max_locals
- depth
)
1153 verify_fail ("invalid local variable");
1154 if (! t
.compatible (current_state
->locals
[index
], this))
1155 verify_fail ("incompatible type in local variable");
1158 type
t (continuation_type
);
1159 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1160 verify_fail ("invalid local variable");
1162 return current_state
->locals
[index
];
1165 // Make sure ARRAY is an array type and that its elements are
1166 // compatible with type ELEMENT. Returns the actual element type.
1167 type
require_array_type (type array
, type element
)
1169 // An odd case. Here we just pretend that everything went ok. If
1170 // the requested element type is some kind of reference, return
1171 // the null type instead.
1172 if (array
.isnull ())
1173 return element
.isreference () ? type (null_type
) : element
;
1175 if (! array
.isarray ())
1176 verify_fail ("array required");
1178 type t
= array
.element_type (this);
1179 if (! element
.compatible (t
, this))
1181 // Special case for byte arrays, which must also be boolean
1184 if (element
.key
== byte_type
)
1186 type
e2 (boolean_type
);
1187 ok
= e2
.compatible (t
, this);
1190 verify_fail ("incompatible array element type");
1193 // Return T and not ELEMENT, because T might be specialized.
1199 if (PC
>= current_method
->code_length
)
1200 verify_fail ("premature end of bytecode");
1201 return (jint
) bytecode
[PC
++] & 0xff;
1206 jint b1
= get_byte ();
1207 jint b2
= get_byte ();
1208 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1213 jint b1
= get_byte ();
1214 jint b2
= get_byte ();
1215 jshort s
= (b1
<< 8) | b2
;
1221 jint b1
= get_byte ();
1222 jint b2
= get_byte ();
1223 jint b3
= get_byte ();
1224 jint b4
= get_byte ();
1225 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1228 int compute_jump (int offset
)
1230 int npc
= start_PC
+ offset
;
1231 if (npc
< 0 || npc
>= current_method
->code_length
)
1232 verify_fail ("branch out of range", start_PC
);
1236 // Merge the indicated state into the state at the branch target and
1237 // schedule a new PC if there is a change. If RET_SEMANTICS is
1238 // true, then we are merging from a `ret' instruction into the
1239 // instruction after a `jsr'. This is a special case with its own
1240 // modified semantics.
1241 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1243 bool changed
= true;
1244 if (states
[npc
] == NULL
)
1246 // There's a weird situation here. If are examining the
1247 // branch that results from a `ret', and there is not yet a
1248 // state available at the branch target (the instruction just
1249 // after the `jsr'), then we have to construct a special kind
1250 // of state at that point for future merging. This special
1251 // state has the type `unused_by_subroutine_type' in each slot
1252 // which was not modified by the subroutine.
1253 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1254 current_method
->max_locals
, ret_semantics
);
1255 debug_print ("== New state in push_jump_merge\n");
1256 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1257 current_method
->max_locals
);
1261 debug_print ("== Merge states in push_jump_merge\n");
1262 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1263 current_method
->max_locals
);
1264 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1265 current_method
->max_locals
);
1266 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1267 current_method
->max_locals
, this);
1268 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1269 current_method
->max_locals
);
1272 if (changed
&& states
[npc
]->next
== state::INVALID
)
1274 // The merge changed the state, and the new PC isn't yet on our
1275 // list of PCs to re-verify.
1276 states
[npc
]->next
= next_verify_pc
;
1277 next_verify_pc
= npc
;
1281 void push_jump (int offset
)
1283 int npc
= compute_jump (offset
);
1285 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1286 push_jump_merge (npc
, current_state
);
1289 void push_exception_jump (type t
, int pc
)
1291 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1293 state
s (current_state
, current_method
->max_stack
,
1294 current_method
->max_locals
);
1295 if (current_method
->max_stack
< 1)
1296 verify_fail ("stack overflow at exception handler");
1297 s
.set_exception (t
, current_method
->max_stack
);
1298 push_jump_merge (pc
, &s
);
1303 int *prev_loc
= &next_verify_pc
;
1304 int npc
= next_verify_pc
;
1305 bool skipped
= false;
1307 while (npc
!= state::NO_NEXT
)
1309 // If the next available PC is an unmerged `ret' state, then
1310 // we aren't yet ready to handle it. That's because we would
1311 // need all kind of special cases to do so. So instead we
1312 // defer this jump until after we've processed it via a
1313 // fall-through. This has to happen because the instruction
1314 // before this one must be a `jsr'.
1315 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1317 *prev_loc
= states
[npc
]->next
;
1318 states
[npc
]->next
= state::INVALID
;
1323 prev_loc
= &states
[npc
]->next
;
1324 npc
= states
[npc
]->next
;
1327 // If we've skipped states and there is nothing else, that's a
1330 verify_fail ("pop_jump: can't happen");
1331 return state::NO_NEXT
;
1334 void invalidate_pc ()
1336 PC
= state::NO_NEXT
;
1339 void note_branch_target (int pc
, bool is_jsr_target
= false)
1341 // Don't check `pc <= PC', because we've advanced PC after
1342 // fetching the target and we haven't yet checked the next
1344 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1345 verify_fail ("branch not to instruction start", start_PC
);
1346 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1349 // Record the jsr which called this instruction.
1350 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1352 info
->next
= jsr_ptrs
[pc
];
1353 jsr_ptrs
[pc
] = info
;
1357 void skip_padding ()
1359 while ((PC
% 4) > 0)
1360 if (get_byte () != 0)
1361 verify_fail ("found nonzero padding byte");
1364 // Return the subroutine to which the instruction at PC belongs.
1365 int get_subroutine (int pc
)
1367 if (states
[pc
] == NULL
)
1369 return states
[pc
]->subroutine
;
1372 // Do the work for a `ret' instruction. INDEX is the index into the
1374 void handle_ret_insn (int index
)
1376 get_variable (index
, return_address_type
);
1378 int csub
= current_state
->subroutine
;
1380 verify_fail ("no subroutine");
1382 // Check to see if we've merged subroutines.
1383 subr_entry_info
*entry
;
1384 for (entry
= entry_points
; entry
!= NULL
; entry
= entry
->next
)
1386 if (entry
->ret_pc
== start_PC
)
1391 entry
= (subr_entry_info
*) _Jv_Malloc (sizeof (subr_entry_info
));
1393 entry
->ret_pc
= start_PC
;
1394 entry
->next
= entry_points
;
1395 entry_points
= entry
;
1397 else if (entry
->pc
!= csub
)
1398 verify_fail ("subroutines merged");
1400 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1402 // Temporarily modify the current state so it looks like we're
1403 // in the enclosing context.
1404 current_state
->subroutine
= get_subroutine (subr
->pc
);
1406 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1407 push_jump_merge (subr
->pc
, current_state
, true);
1410 current_state
->subroutine
= csub
;
1414 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1415 // sure this subroutine isn't already on the stack.
1416 void check_nonrecursive_call (int sub
, int dest
)
1421 verify_fail ("recursive subroutine call");
1422 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1423 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1426 void handle_jsr_insn (int offset
)
1428 int npc
= compute_jump (offset
);
1431 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1432 check_nonrecursive_call (current_state
->subroutine
, npc
);
1434 // Create a new state and modify it as appropriate for entry into
1435 // a subroutine. We're writing this in a weird way because,
1436 // unfortunately, push_type only works on the current state.
1437 push_type (return_address_type
);
1438 push_jump_merge (npc
, current_state
);
1439 // Clean up the weirdness.
1440 pop_type (return_address_type
);
1442 // On entry to the subroutine, the subroutine number must be set
1443 // and the locals must be marked as cleared. We do this after
1444 // merging state so that we don't erroneously "notice" a variable
1445 // change merely on entry.
1446 states
[npc
]->enter_subroutine (npc
, current_method
->max_locals
);
1449 jclass
construct_primitive_array_type (type_val prim
)
1455 k
= JvPrimClass (boolean
);
1458 k
= JvPrimClass (char);
1461 k
= JvPrimClass (float);
1464 k
= JvPrimClass (double);
1467 k
= JvPrimClass (byte
);
1470 k
= JvPrimClass (short);
1473 k
= JvPrimClass (int);
1476 k
= JvPrimClass (long);
1479 verify_fail ("unknown type in construct_primitive_array_type");
1481 k
= _Jv_GetArrayClass (k
, NULL
);
1485 // This pass computes the location of branch targets and also
1486 // instruction starts.
1487 void branch_prepass ()
1489 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1490 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1491 * current_method
->code_length
);
1493 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1499 bool last_was_jsr
= false;
1502 while (PC
< current_method
->code_length
)
1504 // Set `start_PC' early so that error checking can have the
1507 flags
[PC
] |= FLAG_INSN_START
;
1509 // If the previous instruction was a jsr, then the next
1510 // instruction is a branch target -- the branch being the
1511 // corresponding `ret'.
1513 note_branch_target (PC
);
1514 last_was_jsr
= false;
1516 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1520 case op_aconst_null
:
1656 case op_monitorenter
:
1657 case op_monitorexit
:
1665 case op_arraylength
:
1697 case op_invokespecial
:
1698 case op_invokestatic
:
1699 case op_invokevirtual
:
1703 case op_multianewarray
:
1709 last_was_jsr
= true;
1728 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1731 case op_tableswitch
:
1734 note_branch_target (compute_jump (get_int ()));
1735 jint low
= get_int ();
1736 jint hi
= get_int ();
1738 verify_fail ("invalid tableswitch", start_PC
);
1739 for (int i
= low
; i
<= hi
; ++i
)
1740 note_branch_target (compute_jump (get_int ()));
1744 case op_lookupswitch
:
1747 note_branch_target (compute_jump (get_int ()));
1748 int npairs
= get_int ();
1750 verify_fail ("too few pairs in lookupswitch", start_PC
);
1751 while (npairs
-- > 0)
1754 note_branch_target (compute_jump (get_int ()));
1759 case op_invokeinterface
:
1767 opcode
= (java_opcode
) get_byte ();
1769 if (opcode
== op_iinc
)
1775 last_was_jsr
= true;
1778 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1782 verify_fail ("unrecognized instruction in branch_prepass",
1786 // See if any previous branch tried to branch to the middle of
1787 // this instruction.
1788 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1790 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1791 verify_fail ("branch to middle of instruction", pc
);
1795 // Verify exception handlers.
1796 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1798 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1799 verify_fail ("exception handler not at instruction start",
1800 exception
[i
].handler_pc
);
1801 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1802 verify_fail ("exception start not at instruction start",
1803 exception
[i
].start_pc
);
1804 if (exception
[i
].end_pc
!= current_method
->code_length
1805 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1806 verify_fail ("exception end not at instruction start",
1807 exception
[i
].end_pc
);
1809 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1813 void check_pool_index (int index
)
1815 if (index
< 0 || index
>= current_class
->constants
.size
)
1816 verify_fail ("constant pool index out of range", start_PC
);
1819 type
check_class_constant (int index
)
1821 check_pool_index (index
);
1822 _Jv_Constants
*pool
= ¤t_class
->constants
;
1823 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1824 return type (pool
->data
[index
].clazz
);
1825 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1826 return type (pool
->data
[index
].utf8
);
1827 verify_fail ("expected class constant", start_PC
);
1830 type
check_constant (int index
)
1832 check_pool_index (index
);
1833 _Jv_Constants
*pool
= ¤t_class
->constants
;
1834 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1835 || pool
->tags
[index
] == JV_CONSTANT_String
)
1836 return type (&java::lang::String::class$
);
1837 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1838 return type (int_type
);
1839 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1840 return type (float_type
);
1841 verify_fail ("String, int, or float constant expected", start_PC
);
1844 type
check_wide_constant (int index
)
1846 check_pool_index (index
);
1847 _Jv_Constants
*pool
= ¤t_class
->constants
;
1848 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1849 return type (long_type
);
1850 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1851 return type (double_type
);
1852 verify_fail ("long or double constant expected", start_PC
);
1855 // Helper for both field and method. These are laid out the same in
1856 // the constant pool.
1857 type
handle_field_or_method (int index
, int expected
,
1858 _Jv_Utf8Const
**name
,
1859 _Jv_Utf8Const
**fmtype
)
1861 check_pool_index (index
);
1862 _Jv_Constants
*pool
= ¤t_class
->constants
;
1863 if (pool
->tags
[index
] != expected
)
1864 verify_fail ("didn't see expected constant", start_PC
);
1865 // Once we know we have a Fieldref or Methodref we assume that it
1866 // is correctly laid out in the constant pool. I think the code
1867 // in defineclass.cc guarantees this.
1868 _Jv_ushort class_index
, name_and_type_index
;
1869 _Jv_loadIndexes (&pool
->data
[index
],
1871 name_and_type_index
);
1872 _Jv_ushort name_index
, desc_index
;
1873 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1874 name_index
, desc_index
);
1876 *name
= pool
->data
[name_index
].utf8
;
1877 *fmtype
= pool
->data
[desc_index
].utf8
;
1879 return check_class_constant (class_index
);
1882 // Return field's type, compute class' type if requested.
1883 type
check_field_constant (int index
, type
*class_type
= NULL
)
1885 _Jv_Utf8Const
*name
, *field_type
;
1886 type ct
= handle_field_or_method (index
,
1887 JV_CONSTANT_Fieldref
,
1888 &name
, &field_type
);
1891 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1892 return type (field_type
);
1893 return get_type_val_for_signature (field_type
->data
[0]);
1896 type
check_method_constant (int index
, bool is_interface
,
1897 _Jv_Utf8Const
**method_name
,
1898 _Jv_Utf8Const
**method_signature
)
1900 return handle_field_or_method (index
,
1902 ? JV_CONSTANT_InterfaceMethodref
1903 : JV_CONSTANT_Methodref
),
1904 method_name
, method_signature
);
1907 type
get_one_type (char *&p
)
1925 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1929 // Casting to jchar here is ok since we are looking at an ASCII
1931 type_val rt
= get_type_val_for_signature (jchar (v
));
1933 if (arraycount
== 0)
1935 // Callers of this function eventually push their arguments on
1936 // the stack. So, promote them here.
1937 return type (rt
).promote ();
1940 jclass k
= construct_primitive_array_type (rt
);
1941 while (--arraycount
> 0)
1942 k
= _Jv_GetArrayClass (k
, NULL
);
1946 void compute_argument_types (_Jv_Utf8Const
*signature
,
1949 char *p
= signature
->data
;
1955 types
[i
++] = get_one_type (p
);
1958 type
compute_return_type (_Jv_Utf8Const
*signature
)
1960 char *p
= signature
->data
;
1964 return get_one_type (p
);
1967 void check_return_type (type onstack
)
1969 type rt
= compute_return_type (current_method
->self
->signature
);
1970 if (! rt
.compatible (onstack
, this))
1971 verify_fail ("incompatible return type");
1974 // Initialize the stack for the new method. Returns true if this
1975 // method is an instance initializer.
1976 bool initialize_stack ()
1979 bool is_init
= false;
1981 using namespace java::lang::reflect
;
1982 if (! Modifier::isStatic (current_method
->self
->accflags
))
1984 type
kurr (current_class
);
1985 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
1987 kurr
.set_uninitialized (type::SELF
, this);
1990 set_variable (0, kurr
);
1991 current_state
->set_this_type (kurr
);
1995 // We have to handle wide arguments specially here.
1996 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
1997 type arg_types
[arg_count
];
1998 compute_argument_types (current_method
->self
->signature
, arg_types
);
1999 for (int i
= 0; i
< arg_count
; ++i
)
2001 set_variable (var
, arg_types
[i
]);
2003 if (arg_types
[i
].iswide ())
2010 void verify_instructions_0 ()
2012 current_state
= new state (current_method
->max_stack
,
2013 current_method
->max_locals
);
2018 // True if we are verifying an instance initializer.
2019 bool this_is_init
= initialize_stack ();
2021 states
= (state
**) _Jv_Malloc (sizeof (state
*)
2022 * current_method
->code_length
);
2023 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2026 next_verify_pc
= state::NO_NEXT
;
2030 // If the PC was invalidated, get a new one from the work list.
2031 if (PC
== state::NO_NEXT
)
2034 if (PC
== state::INVALID
)
2035 verify_fail ("can't happen: saw state::INVALID");
2036 if (PC
== state::NO_NEXT
)
2038 // Set up the current state.
2039 current_state
->copy (states
[PC
], current_method
->max_stack
,
2040 current_method
->max_locals
);
2044 // Control can't fall off the end of the bytecode. We
2045 // only need to check this in the fall-through case,
2046 // because branch bounds are checked when they are
2048 if (PC
>= current_method
->code_length
)
2049 verify_fail ("fell off end");
2051 // We only have to do this checking in the situation where
2052 // control flow falls through from the previous
2053 // instruction. Otherwise merging is done at the time we
2055 if (states
[PC
] != NULL
)
2057 // We've already visited this instruction. So merge
2058 // the states together. If this yields no change then
2059 // we don't have to re-verify. However, if the new
2060 // state is an the result of an unmerged `ret', we
2061 // must continue through it.
2062 debug_print ("== Fall through merge\n");
2063 states
[PC
]->print ("Old", PC
, current_method
->max_stack
,
2064 current_method
->max_locals
);
2065 current_state
->print ("Cur", PC
, current_method
->max_stack
,
2066 current_method
->max_locals
);
2067 if (! current_state
->merge (states
[PC
], false,
2068 current_method
->max_locals
, this)
2069 && ! states
[PC
]->is_unmerged_ret_state (current_method
->max_locals
))
2071 debug_print ("== Fall through optimization\n");
2075 // Save a copy of it for later.
2076 states
[PC
]->copy (current_state
, current_method
->max_stack
,
2077 current_method
->max_locals
);
2078 current_state
->print ("New", PC
, current_method
->max_stack
,
2079 current_method
->max_locals
);
2083 // We only have to keep saved state at branch targets. If
2084 // we're at a branch target and the state here hasn't been set
2085 // yet, we set it now.
2086 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2088 states
[PC
] = new state (current_state
, current_method
->max_stack
,
2089 current_method
->max_locals
);
2092 // Set this before handling exceptions so that debug output is
2096 // Update states for all active exception handlers. Ordinarily
2097 // there are not many exception handlers. So we simply run
2098 // through them all.
2099 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2101 if (PC
>= exception
[i
].start_pc
&& PC
< exception
[i
].end_pc
)
2103 type
handler (&java::lang::Throwable::class$
);
2104 if (exception
[i
].handler_type
!= 0)
2105 handler
= check_class_constant (exception
[i
].handler_type
);
2106 push_exception_jump (handler
, exception
[i
].handler_pc
);
2110 current_state
->print (" ", PC
, current_method
->max_stack
,
2111 current_method
->max_locals
);
2112 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2118 case op_aconst_null
:
2119 push_type (null_type
);
2129 push_type (int_type
);
2134 push_type (long_type
);
2140 push_type (float_type
);
2145 push_type (double_type
);
2150 push_type (int_type
);
2155 push_type (int_type
);
2159 push_type (check_constant (get_byte ()));
2162 push_type (check_constant (get_ushort ()));
2165 push_type (check_wide_constant (get_ushort ()));
2169 push_type (get_variable (get_byte (), int_type
));
2172 push_type (get_variable (get_byte (), long_type
));
2175 push_type (get_variable (get_byte (), float_type
));
2178 push_type (get_variable (get_byte (), double_type
));
2181 push_type (get_variable (get_byte (), reference_type
));
2188 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2194 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2200 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2206 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2212 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2215 pop_type (int_type
);
2216 push_type (require_array_type (pop_type (reference_type
),
2220 pop_type (int_type
);
2221 push_type (require_array_type (pop_type (reference_type
),
2225 pop_type (int_type
);
2226 push_type (require_array_type (pop_type (reference_type
),
2230 pop_type (int_type
);
2231 push_type (require_array_type (pop_type (reference_type
),
2235 pop_type (int_type
);
2236 push_type (require_array_type (pop_type (reference_type
),
2240 pop_type (int_type
);
2241 require_array_type (pop_type (reference_type
), byte_type
);
2242 push_type (int_type
);
2245 pop_type (int_type
);
2246 require_array_type (pop_type (reference_type
), char_type
);
2247 push_type (int_type
);
2250 pop_type (int_type
);
2251 require_array_type (pop_type (reference_type
), short_type
);
2252 push_type (int_type
);
2255 set_variable (get_byte (), pop_type (int_type
));
2258 set_variable (get_byte (), pop_type (long_type
));
2261 set_variable (get_byte (), pop_type (float_type
));
2264 set_variable (get_byte (), pop_type (double_type
));
2267 set_variable (get_byte (), pop_ref_or_return ());
2273 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2279 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2285 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2291 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2297 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2300 pop_type (int_type
);
2301 pop_type (int_type
);
2302 require_array_type (pop_type (reference_type
), int_type
);
2305 pop_type (long_type
);
2306 pop_type (int_type
);
2307 require_array_type (pop_type (reference_type
), long_type
);
2310 pop_type (float_type
);
2311 pop_type (int_type
);
2312 require_array_type (pop_type (reference_type
), float_type
);
2315 pop_type (double_type
);
2316 pop_type (int_type
);
2317 require_array_type (pop_type (reference_type
), double_type
);
2320 pop_type (reference_type
);
2321 pop_type (int_type
);
2322 require_array_type (pop_type (reference_type
), reference_type
);
2325 pop_type (int_type
);
2326 pop_type (int_type
);
2327 require_array_type (pop_type (reference_type
), byte_type
);
2330 pop_type (int_type
);
2331 pop_type (int_type
);
2332 require_array_type (pop_type (reference_type
), char_type
);
2335 pop_type (int_type
);
2336 pop_type (int_type
);
2337 require_array_type (pop_type (reference_type
), short_type
);
2364 type t2
= pop_raw ();
2379 type t
= pop_raw ();
2394 type t1
= pop_raw ();
2411 type t1
= pop_raw ();
2414 type t2
= pop_raw ();
2432 type t3
= pop_raw ();
2470 pop_type (int_type
);
2471 push_type (pop_type (int_type
));
2481 pop_type (long_type
);
2482 push_type (pop_type (long_type
));
2487 pop_type (int_type
);
2488 push_type (pop_type (long_type
));
2495 pop_type (float_type
);
2496 push_type (pop_type (float_type
));
2503 pop_type (double_type
);
2504 push_type (pop_type (double_type
));
2510 push_type (pop_type (int_type
));
2513 push_type (pop_type (long_type
));
2516 push_type (pop_type (float_type
));
2519 push_type (pop_type (double_type
));
2522 get_variable (get_byte (), int_type
);
2526 pop_type (int_type
);
2527 push_type (long_type
);
2530 pop_type (int_type
);
2531 push_type (float_type
);
2534 pop_type (int_type
);
2535 push_type (double_type
);
2538 pop_type (long_type
);
2539 push_type (int_type
);
2542 pop_type (long_type
);
2543 push_type (float_type
);
2546 pop_type (long_type
);
2547 push_type (double_type
);
2550 pop_type (float_type
);
2551 push_type (int_type
);
2554 pop_type (float_type
);
2555 push_type (long_type
);
2558 pop_type (float_type
);
2559 push_type (double_type
);
2562 pop_type (double_type
);
2563 push_type (int_type
);
2566 pop_type (double_type
);
2567 push_type (long_type
);
2570 pop_type (double_type
);
2571 push_type (float_type
);
2574 pop_type (long_type
);
2575 pop_type (long_type
);
2576 push_type (int_type
);
2580 pop_type (float_type
);
2581 pop_type (float_type
);
2582 push_type (int_type
);
2586 pop_type (double_type
);
2587 pop_type (double_type
);
2588 push_type (int_type
);
2596 pop_type (int_type
);
2597 push_jump (get_short ());
2605 pop_type (int_type
);
2606 pop_type (int_type
);
2607 push_jump (get_short ());
2611 pop_type (reference_type
);
2612 pop_type (reference_type
);
2613 push_jump (get_short ());
2616 push_jump (get_short ());
2620 handle_jsr_insn (get_short ());
2623 handle_ret_insn (get_byte ());
2625 case op_tableswitch
:
2627 pop_type (int_type
);
2629 push_jump (get_int ());
2630 jint low
= get_int ();
2631 jint high
= get_int ();
2632 // Already checked LOW -vs- HIGH.
2633 for (int i
= low
; i
<= high
; ++i
)
2634 push_jump (get_int ());
2639 case op_lookupswitch
:
2641 pop_type (int_type
);
2643 push_jump (get_int ());
2644 jint npairs
= get_int ();
2645 // Already checked NPAIRS >= 0.
2647 for (int i
= 0; i
< npairs
; ++i
)
2649 jint key
= get_int ();
2650 if (i
> 0 && key
<= lastkey
)
2651 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2653 push_jump (get_int ());
2659 check_return_type (pop_type (int_type
));
2663 check_return_type (pop_type (long_type
));
2667 check_return_type (pop_type (float_type
));
2671 check_return_type (pop_type (double_type
));
2675 check_return_type (pop_type (reference_type
));
2679 // We only need to check this when the return type is
2680 // void, because all instance initializers return void.
2682 current_state
->check_this_initialized (this);
2683 check_return_type (void_type
);
2687 push_type (check_field_constant (get_ushort ()));
2690 pop_type (check_field_constant (get_ushort ()));
2695 type field
= check_field_constant (get_ushort (), &klass
);
2703 type field
= check_field_constant (get_ushort (), &klass
);
2706 // We have an obscure special case here: we can use
2707 // `putfield' on a field declared in this class, even if
2708 // `this' has not yet been initialized.
2709 if (! current_state
->this_type
.isinitialized ()
2710 && current_state
->this_type
.pc
== type::SELF
)
2711 klass
.set_uninitialized (type::SELF
, this);
2716 case op_invokevirtual
:
2717 case op_invokespecial
:
2718 case op_invokestatic
:
2719 case op_invokeinterface
:
2721 _Jv_Utf8Const
*method_name
, *method_signature
;
2723 = check_method_constant (get_ushort (),
2724 opcode
== op_invokeinterface
,
2727 // NARGS is only used when we're processing
2728 // invokeinterface. It is simplest for us to compute it
2729 // here and then verify it later.
2731 if (opcode
== op_invokeinterface
)
2733 nargs
= get_byte ();
2734 if (get_byte () != 0)
2735 verify_fail ("invokeinterface dummy byte is wrong");
2738 bool is_init
= false;
2739 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2742 if (opcode
!= op_invokespecial
)
2743 verify_fail ("can't invoke <init>");
2745 else if (method_name
->data
[0] == '<')
2746 verify_fail ("can't invoke method starting with `<'");
2748 // Pop arguments and check types.
2749 int arg_count
= _Jv_count_arguments (method_signature
);
2750 type arg_types
[arg_count
];
2751 compute_argument_types (method_signature
, arg_types
);
2752 for (int i
= arg_count
- 1; i
>= 0; --i
)
2754 // This is only used for verifying the byte for
2756 nargs
-= arg_types
[i
].depth ();
2757 pop_type (arg_types
[i
]);
2760 if (opcode
== op_invokeinterface
2762 verify_fail ("wrong argument count for invokeinterface");
2764 if (opcode
!= op_invokestatic
)
2766 type t
= class_type
;
2769 // In this case the PC doesn't matter.
2770 t
.set_uninitialized (type::UNINIT
, this);
2774 current_state
->set_initialized (t
.get_pc (),
2775 current_method
->max_locals
);
2778 type rt
= compute_return_type (method_signature
);
2786 type t
= check_class_constant (get_ushort ());
2787 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2788 verify_fail ("type is array, interface, or abstract");
2789 t
.set_uninitialized (start_PC
, this);
2796 int atype
= get_byte ();
2797 // We intentionally have chosen constants to make this
2799 if (atype
< boolean_type
|| atype
> long_type
)
2800 verify_fail ("type not primitive", start_PC
);
2801 pop_type (int_type
);
2802 push_type (construct_primitive_array_type (type_val (atype
)));
2806 pop_type (int_type
);
2807 push_type (check_class_constant (get_ushort ()).to_array (this));
2809 case op_arraylength
:
2811 type t
= pop_type (reference_type
);
2812 if (! t
.isarray () && ! t
.isnull ())
2813 verify_fail ("array type expected");
2814 push_type (int_type
);
2818 pop_type (type (&java::lang::Throwable::class$
));
2822 pop_type (reference_type
);
2823 push_type (check_class_constant (get_ushort ()));
2826 pop_type (reference_type
);
2827 check_class_constant (get_ushort ());
2828 push_type (int_type
);
2830 case op_monitorenter
:
2831 pop_type (reference_type
);
2833 case op_monitorexit
:
2834 pop_type (reference_type
);
2838 switch (get_byte ())
2841 push_type (get_variable (get_ushort (), int_type
));
2844 push_type (get_variable (get_ushort (), long_type
));
2847 push_type (get_variable (get_ushort (), float_type
));
2850 push_type (get_variable (get_ushort (), double_type
));
2853 push_type (get_variable (get_ushort (), reference_type
));
2856 set_variable (get_ushort (), pop_type (int_type
));
2859 set_variable (get_ushort (), pop_type (long_type
));
2862 set_variable (get_ushort (), pop_type (float_type
));
2865 set_variable (get_ushort (), pop_type (double_type
));
2868 set_variable (get_ushort (), pop_type (reference_type
));
2871 handle_ret_insn (get_short ());
2874 get_variable (get_ushort (), int_type
);
2878 verify_fail ("unrecognized wide instruction", start_PC
);
2882 case op_multianewarray
:
2884 type atype
= check_class_constant (get_ushort ());
2885 int dim
= get_byte ();
2887 verify_fail ("too few dimensions to multianewarray", start_PC
);
2888 atype
.verify_dimensions (dim
, this);
2889 for (int i
= 0; i
< dim
; ++i
)
2890 pop_type (int_type
);
2896 pop_type (reference_type
);
2897 push_jump (get_short ());
2900 push_jump (get_int ());
2904 handle_jsr_insn (get_int ());
2908 // Unrecognized opcode.
2909 verify_fail ("unrecognized instruction in verify_instructions_0",
2915 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
2917 using namespace java::lang
;
2918 StringBuffer
*buf
= new StringBuffer ();
2920 buf
->append (JvNewStringLatin1 ("verification failed"));
2925 buf
->append (JvNewStringLatin1 (" at PC "));
2929 _Jv_InterpMethod
*method
= current_method
;
2930 buf
->append (JvNewStringLatin1 (" in "));
2931 buf
->append (current_class
->getName());
2932 buf
->append ((jchar
) ':');
2933 buf
->append (JvNewStringUTF (method
->get_method()->name
->data
));
2934 buf
->append ((jchar
) '(');
2935 buf
->append (JvNewStringUTF (method
->get_method()->signature
->data
));
2936 buf
->append ((jchar
) ')');
2938 buf
->append (JvNewStringLatin1 (": "));
2939 buf
->append (JvNewStringLatin1 (s
));
2940 throw new java::lang::VerifyError (buf
->toString ());
2945 void verify_instructions ()
2948 verify_instructions_0 ();
2951 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
2953 // We just print the text as utf-8. This is just for debugging
2955 debug_print ("--------------------------------\n");
2956 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
2959 bytecode
= m
->bytecode ();
2960 exception
= m
->exceptions ();
2961 current_class
= m
->defining_class
;
2967 entry_points
= NULL
;
2970 ~_Jv_BytecodeVerifier ()
2979 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2981 if (jsr_ptrs
[i
] != NULL
)
2983 subr_info
*info
= jsr_ptrs
[i
];
2984 while (info
!= NULL
)
2986 subr_info
*next
= info
->next
;
2992 _Jv_Free (jsr_ptrs
);
2995 while (utf8_list
!= NULL
)
2997 linked_utf8
*n
= utf8_list
->next
;
2998 _Jv_Free (utf8_list
->val
);
2999 _Jv_Free (utf8_list
);
3003 while (entry_points
!= NULL
)
3005 subr_entry_info
*next
= entry_points
->next
;
3006 _Jv_Free (entry_points
);
3007 entry_points
= next
;
3013 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3015 _Jv_BytecodeVerifier
v (meth
);
3016 v
.verify_instructions ();
3018 #endif /* INTERPRETER */