1 // verify.cc - verify bytecode
3 /* Copyright (C) 2001, 2002, 2003, 2004, 2005 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>
22 // On Solaris 10/x86, <signal.h> indirectly includes <ia32/sys/reg.h>, which
23 // defines PC since g++ predefines __EXTENSIONS__. Undef here to avoid clash
24 // with PC member of class _Jv_BytecodeVerifier below.
29 #include <java/lang/Class.h>
30 #include <java/lang/VerifyError.h>
31 #include <java/lang/Throwable.h>
32 #include <java/lang/reflect/Modifier.h>
33 #include <java/lang/StringBuffer.h>
34 #include <java/lang/NoClassDefFoundError.h>
38 #endif /* VERIFY_DEBUG */
41 // This is used to mark states which are not scheduled for
43 #define INVALID_STATE ((state *) -1)
45 static void debug_print (const char *fmt
, ...)
46 __attribute__ ((format (printf
, 1, 2)));
49 debug_print (MAYBE_UNUSED
const char *fmt
, ...)
54 vfprintf (stderr
, fmt
, ap
);
56 #endif /* VERIFY_DEBUG */
59 // This started as a fairly ordinary verifier, and for the most part
60 // it remains so. It works in the obvious way, by modeling the effect
61 // of each opcode as it is encountered. For most opcodes, this is a
62 // straightforward operation.
64 // This verifier does not do type merging. It used to, but this
65 // results in difficulty verifying some relatively simple code
66 // involving interfaces, and it pushed some verification work into the
69 // Instead of merging reference types, when we reach a point where two
70 // flows of control merge, we simply keep the union of reference types
71 // from each branch. Then, when we need to verify a fact about a
72 // reference on the stack (e.g., that it is compatible with the
73 // argument type of a method), we check to ensure that all possible
74 // types satisfy the requirement.
76 // Another area this verifier differs from the norm is in its handling
77 // of subroutines. The JVM specification has some confusing things to
78 // say about subroutines. For instance, it makes claims about not
79 // allowing subroutines to merge and it rejects recursive subroutines.
80 // For the most part these are red herrings; we used to try to follow
81 // these things but they lead to problems. For example, the notion of
82 // "being in a subroutine" is not well-defined: is an exception
83 // handler in a subroutine? If you never execute the `ret' but
84 // instead `goto 1' do you remain in the subroutine?
86 // For clarity on what is really required for type safety, read
87 // "Simple Verification Technique for Complex Java Bytecode
88 // Subroutines" by Alessandro Coglio. Among other things this paper
89 // shows that recursive subroutines are not harmful to type safety.
90 // We implement something similar to what he proposes. Note that this
91 // means that this verifier will accept code that is rejected by some
94 // For those not wanting to read the paper, the basic observation is
95 // that we can maintain split states in subroutines. We maintain one
96 // state for each calling `jsr'. In other words, we re-verify a
97 // subroutine once for each caller, using the exact types held by the
98 // callers (as opposed to the old approach of merging types and
99 // keeping a bitmap registering what did or did not change). This
100 // approach lets us continue to verify correctly even when a
101 // subroutine is exited via `goto' or `athrow' and not `ret'.
103 // In some other areas the JVM specification is (mildly) incorrect,
104 // so we diverge. For instance, you cannot
105 // violate type safety by allocating an object with `new' and then
106 // failing to initialize it, no matter how one branches or where one
107 // stores the uninitialized reference. See "Improving the official
108 // specification of Java bytecode verification" by Alessandro Coglio.
110 // Note that there's no real point in enforcing that padding bytes or
111 // the mystery byte of invokeinterface must be 0, but we do that
114 // The verifier is currently neither completely lazy nor eager when it
115 // comes to loading classes. It tries to represent types by name when
116 // possible, and then loads them when it needs to verify a fact about
117 // the type. Checking types by name is valid because we only use
118 // names which come from the current class' constant pool. Since all
119 // such names are looked up using the same class loader, there is no
120 // danger that we might be fooled into comparing different types with
123 // In the future we plan to allow for a completely lazy mode of
124 // operation, where the verifier will construct a list of type
125 // assertions to be checked later.
127 // Some test cases for the verifier live in the "verify" module of the
128 // Mauve test suite. However, some of these are presently
129 // (2004-01-20) believed to be incorrect. (More precisely the notion
130 // of "correct" is not well-defined, and this verifier differs from
131 // others while remaining type-safe.) Some other tests live in the
132 // libgcj test suite.
133 class _Jv_BytecodeVerifier
137 static const int FLAG_INSN_START
= 1;
138 static const int FLAG_BRANCH_TARGET
= 2;
143 struct ref_intersection
;
154 // The PC corresponding to the start of the current instruction.
157 // The current state of the stack, locals, etc.
158 state
*current_state
;
160 // At each branch target we keep a linked list of all the states we
161 // can process at that point. We'll only have multiple states at a
162 // given PC if they both have different return-address types in the
163 // same stack or local slot. This array is indexed by PC and holds
164 // the list of all such states.
165 linked
<state
> **states
;
167 // We keep a linked list of all the states which we must reverify.
168 // This is the head of the list.
169 state
*next_verify_state
;
171 // We keep some flags for each instruction. The values are the
172 // FLAG_* constants defined above. This is an array indexed by PC.
175 // The bytecode itself.
176 unsigned char *bytecode
;
178 _Jv_InterpException
*exception
;
181 jclass current_class
;
183 _Jv_InterpMethod
*current_method
;
185 // A linked list of utf8 objects we allocate.
186 linked
<_Jv_Utf8Const
> *utf8_list
;
188 // A linked list of all ref_intersection objects we allocate.
189 ref_intersection
*isect_list
;
191 // Create a new Utf-8 constant and return it. We do this to avoid
192 // having our Utf-8 constants prematurely collected.
193 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
195 linked
<_Jv_Utf8Const
> *lu
= (linked
<_Jv_Utf8Const
> *)
196 _Jv_Malloc (sizeof (linked
<_Jv_Utf8Const
>)
197 + _Jv_Utf8Const::space_needed(s
, len
));
198 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) (lu
+ 1);
201 lu
->next
= utf8_list
;
207 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
209 using namespace java::lang
;
210 StringBuffer
*buf
= new StringBuffer ();
212 buf
->append (JvNewStringLatin1 ("verification failed"));
217 buf
->append (JvNewStringLatin1 (" at PC "));
221 _Jv_InterpMethod
*method
= current_method
;
222 buf
->append (JvNewStringLatin1 (" in "));
223 buf
->append (current_class
->getName());
224 buf
->append ((jchar
) ':');
225 buf
->append (method
->get_method()->name
->toString());
226 buf
->append ((jchar
) '(');
227 buf
->append (method
->get_method()->signature
->toString());
228 buf
->append ((jchar
) ')');
230 buf
->append (JvNewStringLatin1 (": "));
231 buf
->append (JvNewStringLatin1 (s
));
232 throw new java::lang::VerifyError (buf
->toString ());
235 // This enum holds a list of tags for all the different types we
236 // need to handle. Reference types are treated specially by the
242 // The values for primitive types are chosen to correspond to values
243 // specified to newarray.
253 // Used when overwriting second word of a double or long in the
254 // local variables. Also used after merging local variable states
255 // to indicate an unusable value.
258 // This is the second word of a two-word value, i.e., a double or
262 // Everything after `reference_type' must be a reference type.
265 uninitialized_reference_type
268 // This represents a merged class type. Some verifiers (including
269 // earlier versions of this one) will compute the intersection of
270 // two class types when merging states. However, this loses
271 // critical information about interfaces implemented by the various
272 // classes. So instead we keep track of all the actual classes that
274 struct ref_intersection
276 // Whether or not this type has been resolved.
282 // For a resolved reference type, this is a pointer to the class.
284 // For other reference types, this it the name of the class.
288 // Link to the next reference in the intersection.
289 ref_intersection
*ref_next
;
291 // This is used to keep track of all the allocated
292 // ref_intersection objects, so we can free them.
293 // FIXME: we should allocate these in chunks.
294 ref_intersection
*alloc_next
;
296 ref_intersection (jclass klass
, _Jv_BytecodeVerifier
*verifier
)
301 alloc_next
= verifier
->isect_list
;
302 verifier
->isect_list
= this;
305 ref_intersection (_Jv_Utf8Const
*name
, _Jv_BytecodeVerifier
*verifier
)
310 alloc_next
= verifier
->isect_list
;
311 verifier
->isect_list
= this;
314 ref_intersection (ref_intersection
*dup
, ref_intersection
*tail
,
315 _Jv_BytecodeVerifier
*verifier
)
318 is_resolved
= dup
->is_resolved
;
320 alloc_next
= verifier
->isect_list
;
321 verifier
->isect_list
= this;
324 bool equals (ref_intersection
*other
, _Jv_BytecodeVerifier
*verifier
)
326 if (! is_resolved
&& ! other
->is_resolved
327 && _Jv_equalUtf8Consts (data
.name
, other
->data
.name
))
331 if (! other
->is_resolved
)
332 other
->resolve (verifier
);
333 return data
.klass
== other
->data
.klass
;
336 // Merge THIS type into OTHER, returning the result. This will
337 // return OTHER if all the classes in THIS already appear in
339 ref_intersection
*merge (ref_intersection
*other
,
340 _Jv_BytecodeVerifier
*verifier
)
342 ref_intersection
*tail
= other
;
343 for (ref_intersection
*self
= this; self
!= NULL
; self
= self
->ref_next
)
346 for (ref_intersection
*iter
= other
; iter
!= NULL
;
347 iter
= iter
->ref_next
)
349 if (iter
->equals (self
, verifier
))
357 tail
= new ref_intersection (self
, tail
, verifier
);
362 void resolve (_Jv_BytecodeVerifier
*verifier
)
367 using namespace java::lang
;
368 java::lang::ClassLoader
*loader
369 = verifier
->current_class
->getClassLoaderInternal();
370 // We might see either kind of name. Sigh.
371 if (data
.name
->first() == 'L' && data
.name
->limit()[-1] == ';')
373 data
.klass
= _Jv_FindClassFromSignature (data
.name
->chars(), loader
);
374 if (data
.klass
== NULL
)
375 throw new java::lang::NoClassDefFoundError(data
.name
->toString());
378 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
383 // See if an object of type OTHER can be assigned to an object of
384 // type *THIS. This might resolve classes in one chain or the
386 bool compatible (ref_intersection
*other
,
387 _Jv_BytecodeVerifier
*verifier
)
389 ref_intersection
*self
= this;
391 for (; self
!= NULL
; self
= self
->ref_next
)
393 ref_intersection
*other_iter
= other
;
395 for (; other_iter
!= NULL
; other_iter
= other_iter
->ref_next
)
397 // Avoid resolving if possible.
398 if (! self
->is_resolved
399 && ! other_iter
->is_resolved
400 && _Jv_equalUtf8Consts (self
->data
.name
,
401 other_iter
->data
.name
))
404 if (! self
->is_resolved
)
405 self
->resolve(verifier
);
406 if (! other_iter
->is_resolved
)
407 other_iter
->resolve(verifier
);
409 if (! is_assignable_from_slow (self
->data
.klass
,
410 other_iter
->data
.klass
))
420 // assert (ref_next == NULL);
422 return data
.klass
->isArray ();
424 return data
.name
->first() == '[';
427 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
429 // assert (ref_next == NULL);
432 return data
.klass
->isInterface ();
435 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
437 // assert (ref_next == NULL);
440 using namespace java::lang::reflect
;
441 return Modifier::isAbstract (data
.klass
->getModifiers ());
444 jclass
getclass (_Jv_BytecodeVerifier
*verifier
)
451 int count_dimensions ()
456 jclass k
= data
.klass
;
457 while (k
->isArray ())
459 k
= k
->getComponentType ();
465 char *p
= data
.name
->chars();
472 void *operator new (size_t bytes
)
474 return _Jv_Malloc (bytes
);
477 void operator delete (void *mem
)
483 // Return the type_val corresponding to a primitive signature
484 // character. For instance `I' returns `int.class'.
485 type_val
get_type_val_for_signature (jchar sig
)
518 verify_fail ("invalid signature");
523 // Return the type_val corresponding to a primitive class.
524 type_val
get_type_val_for_signature (jclass k
)
526 return get_type_val_for_signature ((jchar
) k
->method_count
);
529 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
530 // TARGET haven't been prepared.
531 static bool is_assignable_from_slow (jclass target
, jclass source
)
533 // First, strip arrays.
534 while (target
->isArray ())
536 // If target is array, source must be as well.
537 if (! source
->isArray ())
539 target
= target
->getComponentType ();
540 source
= source
->getComponentType ();
544 if (target
== &java::lang::Object::class$
)
549 if (source
== target
)
552 if (target
->isPrimitive () || source
->isPrimitive ())
555 if (target
->isInterface ())
557 for (int i
= 0; i
< source
->interface_count
; ++i
)
559 // We use a recursive call because we also need to
560 // check superinterfaces.
561 if (is_assignable_from_slow (target
, source
->getInterface (i
)))
565 source
= source
->getSuperclass ();
567 while (source
!= NULL
);
572 // The `type' class is used to represent a single type in the
579 // For reference types, the representation of the type.
580 ref_intersection
*klass
;
582 // This is used in two situations.
584 // First, when constructing a new object, it is the PC of the
585 // `new' instruction which created the object. We use the special
586 // value UNINIT to mean that this is uninitialized. The special
587 // value SELF is used for the case where the current method is
588 // itself the <init> method. the special value EITHER is used
589 // when we may optionally allow either an uninitialized or
590 // initialized reference to match.
592 // Second, when the key is return_address_type, this holds the PC
593 // of the instruction following the `jsr'.
596 static const int UNINIT
= -2;
597 static const int SELF
= -1;
598 static const int EITHER
= -3;
600 // Basic constructor.
603 key
= unsuitable_type
;
608 // Make a new instance given the type tag. We assume a generic
609 // `reference_type' means Object.
613 // For reference_type, if KLASS==NULL then that means we are
614 // looking for a generic object of any kind, including an
615 // uninitialized reference.
620 // Make a new instance given a class.
621 type (jclass k
, _Jv_BytecodeVerifier
*verifier
)
623 key
= reference_type
;
624 klass
= new ref_intersection (k
, verifier
);
628 // Make a new instance given the name of a class.
629 type (_Jv_Utf8Const
*n
, _Jv_BytecodeVerifier
*verifier
)
631 key
= reference_type
;
632 klass
= new ref_intersection (n
, verifier
);
644 // These operators are required because libgcj can't link in
646 void *operator new[] (size_t bytes
)
648 return _Jv_Malloc (bytes
);
651 void operator delete[] (void *mem
)
656 type
& operator= (type_val k
)
664 type
& operator= (const type
& t
)
672 // Promote a numeric type.
675 if (key
== boolean_type
|| key
== char_type
676 || key
== byte_type
|| key
== short_type
)
681 // Mark this type as the uninitialized result of `new'.
682 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
684 if (key
== reference_type
)
685 key
= uninitialized_reference_type
;
687 verifier
->verify_fail ("internal error in type::uninitialized");
691 // Mark this type as now initialized.
692 void set_initialized (int npc
)
694 if (npc
!= UNINIT
&& pc
== npc
&& key
== uninitialized_reference_type
)
696 key
= reference_type
;
701 // Mark this type as a particular return address.
702 void set_return_address (int npc
)
707 // Return true if this type and type OTHER are considered
708 // mergeable for the purposes of state merging. This is related
709 // to subroutine handling. For this purpose two types are
710 // considered unmergeable if they are both return-addresses but
711 // have different PCs.
712 bool state_mergeable_p (const type
&other
) const
714 return (key
!= return_address_type
715 || other
.key
!= return_address_type
719 // Return true if an object of type K can be assigned to a variable
720 // of type *THIS. Handle various special cases too. Might modify
721 // *THIS or K. Note however that this does not perform numeric
723 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
725 // Any type is compatible with the unsuitable type.
726 if (key
== unsuitable_type
)
729 if (key
< reference_type
|| k
.key
< reference_type
)
732 // The `null' type is convertible to any initialized reference
734 if (key
== null_type
)
735 return k
.key
!= uninitialized_reference_type
;
736 if (k
.key
== null_type
)
737 return key
!= uninitialized_reference_type
;
739 // A special case for a generic reference.
743 verifier
->verify_fail ("programmer error in type::compatible");
745 // Handle the special 'EITHER' case, which is only used in a
746 // special case of 'putfield'. Note that we only need to handle
747 // this on the LHS of a check.
748 if (! isinitialized () && pc
== EITHER
)
750 // If the RHS is uninitialized, it must be an uninitialized
752 if (! k
.isinitialized () && k
.pc
!= SELF
)
755 else if (isinitialized () != k
.isinitialized ())
757 // An initialized type and an uninitialized type are not
758 // otherwise compatible.
763 // Two uninitialized objects are compatible if either:
764 // * The PCs are identical, or
765 // * One PC is UNINIT.
766 if (! isinitialized ())
768 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
773 return klass
->compatible(k
.klass
, verifier
);
776 bool equals (const type
&other
, _Jv_BytecodeVerifier
*vfy
)
778 // Only works for reference types.
779 if ((key
!= reference_type
780 && key
!= uninitialized_reference_type
)
781 || (other
.key
!= reference_type
782 && other
.key
!= uninitialized_reference_type
))
784 // Only for single-valued types.
785 if (klass
->ref_next
|| other
.klass
->ref_next
)
787 return klass
->equals (other
.klass
, vfy
);
792 return key
== void_type
;
797 return key
== long_type
|| key
== double_type
;
800 // Return number of stack or local variable slots taken by this
804 return iswide () ? 2 : 1;
807 bool isarray () const
809 // We treat null_type as not an array. This is ok based on the
810 // current uses of this method.
811 if (key
== reference_type
)
812 return klass
->isarray ();
818 return key
== null_type
;
821 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
823 if (key
!= reference_type
)
825 return klass
->isinterface (verifier
);
828 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
830 if (key
!= reference_type
)
832 return klass
->isabstract (verifier
);
835 // Return the element type of an array.
836 type
element_type (_Jv_BytecodeVerifier
*verifier
)
838 if (key
!= reference_type
)
839 verifier
->verify_fail ("programmer error in type::element_type()", -1);
841 jclass k
= klass
->getclass (verifier
)->getComponentType ();
842 if (k
->isPrimitive ())
843 return type (verifier
->get_type_val_for_signature (k
));
844 return type (k
, verifier
);
847 // Return the array type corresponding to an initialized
848 // reference. We could expand this to work for other kinds of
849 // types, but currently we don't need to.
850 type
to_array (_Jv_BytecodeVerifier
*verifier
)
852 if (key
!= reference_type
)
853 verifier
->verify_fail ("internal error in type::to_array()");
855 jclass k
= klass
->getclass (verifier
);
856 return type (_Jv_GetArrayClass (k
, k
->getClassLoaderInternal()),
860 bool isreference () const
862 return key
>= reference_type
;
870 bool isinitialized () const
872 return key
== reference_type
|| key
== null_type
;
875 bool isresolved () const
877 return (key
== reference_type
879 || key
== uninitialized_reference_type
);
882 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
884 // The way this is written, we don't need to check isarray().
885 if (key
!= reference_type
)
886 verifier
->verify_fail ("internal error in verify_dimensions:"
887 " not a reference type");
889 if (klass
->count_dimensions () < ndims
)
890 verifier
->verify_fail ("array type has fewer dimensions"
894 // Merge OLD_TYPE into this. On error throw exception. Return
895 // true if the merge caused a type change.
896 bool merge (type
& old_type
, bool local_semantics
,
897 _Jv_BytecodeVerifier
*verifier
)
899 bool changed
= false;
900 bool refo
= old_type
.isreference ();
901 bool refn
= isreference ();
904 if (old_type
.key
== null_type
)
906 else if (key
== null_type
)
911 else if (isinitialized () != old_type
.isinitialized ())
912 verifier
->verify_fail ("merging initialized and uninitialized types");
915 if (! isinitialized ())
919 else if (old_type
.pc
== UNINIT
)
921 else if (pc
!= old_type
.pc
)
922 verifier
->verify_fail ("merging different uninitialized types");
925 ref_intersection
*merged
= old_type
.klass
->merge (klass
,
934 else if (refo
|| refn
|| key
!= old_type
.key
)
938 // If we already have an `unsuitable' type, then we
939 // don't need to change again.
940 if (key
!= unsuitable_type
)
942 key
= unsuitable_type
;
947 verifier
->verify_fail ("unmergeable type");
953 void print (void) const
958 case boolean_type
: c
= 'Z'; break;
959 case byte_type
: c
= 'B'; break;
960 case char_type
: c
= 'C'; break;
961 case short_type
: c
= 'S'; break;
962 case int_type
: c
= 'I'; break;
963 case long_type
: c
= 'J'; break;
964 case float_type
: c
= 'F'; break;
965 case double_type
: c
= 'D'; break;
966 case void_type
: c
= 'V'; break;
967 case unsuitable_type
: c
= '-'; break;
968 case return_address_type
: c
= 'r'; break;
969 case continuation_type
: c
= '+'; break;
970 case reference_type
: c
= 'L'; break;
971 case null_type
: c
= '@'; break;
972 case uninitialized_reference_type
: c
= 'U'; break;
974 debug_print ("%c", c
);
976 #endif /* VERIFY_DEBUG */
979 // This class holds all the state information we need for a given
983 // The current top of the stack, in terms of slots.
985 // The current depth of the stack. This will be larger than
986 // STACKTOP when wide types are on the stack.
990 // The local variables.
992 // We keep track of the type of `this' specially. This is used to
993 // ensure that an instance initializer invokes another initializer
994 // on `this' before returning. We must keep track of this
995 // specially because otherwise we might be confused by code which
996 // assigns to locals[0] (overwriting `this') and then returns
997 // without really initializing.
1000 // The PC for this state. This is only valid on states which are
1001 // permanently attached to a given PC. For an object like
1002 // `current_state', which is used transiently, this has no
1005 // We keep a linked list of all states requiring reverification.
1006 // If this is the special value INVALID_STATE then this state is
1007 // not on the list. NULL marks the end of the linked list.
1010 // NO_NEXT is the PC value meaning that a new state must be
1011 // acquired from the verification list.
1012 static const int NO_NEXT
= -1;
1019 next
= INVALID_STATE
;
1022 state (int max_stack
, int max_locals
)
1027 stack
= new type
[max_stack
];
1028 for (int i
= 0; i
< max_stack
; ++i
)
1029 stack
[i
] = unsuitable_type
;
1030 locals
= new type
[max_locals
];
1031 for (int i
= 0; i
< max_locals
; ++i
)
1032 locals
[i
] = unsuitable_type
;
1034 next
= INVALID_STATE
;
1037 state (const state
*orig
, int max_stack
, int max_locals
)
1039 stack
= new type
[max_stack
];
1040 locals
= new type
[max_locals
];
1041 copy (orig
, max_stack
, max_locals
);
1043 next
= INVALID_STATE
;
1054 void *operator new[] (size_t bytes
)
1056 return _Jv_Malloc (bytes
);
1059 void operator delete[] (void *mem
)
1064 void *operator new (size_t bytes
)
1066 return _Jv_Malloc (bytes
);
1069 void operator delete (void *mem
)
1074 void copy (const state
*copy
, int max_stack
, int max_locals
)
1076 stacktop
= copy
->stacktop
;
1077 stackdepth
= copy
->stackdepth
;
1078 for (int i
= 0; i
< max_stack
; ++i
)
1079 stack
[i
] = copy
->stack
[i
];
1080 for (int i
= 0; i
< max_locals
; ++i
)
1081 locals
[i
] = copy
->locals
[i
];
1083 this_type
= copy
->this_type
;
1084 // Don't modify `next' or `pc'.
1087 // Modify this state to reflect entry to an exception handler.
1088 void set_exception (type t
, int max_stack
)
1093 for (int i
= stacktop
; i
< max_stack
; ++i
)
1094 stack
[i
] = unsuitable_type
;
1097 inline int get_pc () const
1102 void set_pc (int npc
)
1107 // Merge STATE_OLD into this state. Destructively modifies this
1108 // state. Returns true if the new state was in fact changed.
1109 // Will throw an exception if the states are not mergeable.
1110 bool merge (state
*state_old
, int max_locals
,
1111 _Jv_BytecodeVerifier
*verifier
)
1113 bool changed
= false;
1115 // Special handling for `this'. If one or the other is
1116 // uninitialized, then the merge is uninitialized.
1117 if (this_type
.isinitialized ())
1118 this_type
= state_old
->this_type
;
1121 if (state_old
->stacktop
!= stacktop
) // FIXME stackdepth instead?
1122 verifier
->verify_fail ("stack sizes differ");
1123 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
1125 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
1129 // Merge local variables.
1130 for (int i
= 0; i
< max_locals
; ++i
)
1132 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
1139 // Ensure that `this' has been initialized.
1140 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1142 if (this_type
.isreference () && ! this_type
.isinitialized ())
1143 verifier
->verify_fail ("`this' is uninitialized");
1146 // Set type of `this'.
1147 void set_this_type (const type
&k
)
1152 // Mark each `new'd object we know of that was allocated at PC as
1154 void set_initialized (int pc
, int max_locals
)
1156 for (int i
= 0; i
< stacktop
; ++i
)
1157 stack
[i
].set_initialized (pc
);
1158 for (int i
= 0; i
< max_locals
; ++i
)
1159 locals
[i
].set_initialized (pc
);
1160 this_type
.set_initialized (pc
);
1163 // This tests to see whether two states can be considered "merge
1164 // compatible". If both states have a return-address in the same
1165 // slot, and the return addresses are different, then they are not
1166 // compatible and we must not try to merge them.
1167 bool state_mergeable_p (state
*other
, int max_locals
,
1168 _Jv_BytecodeVerifier
*verifier
)
1170 // This is tricky: if the stack sizes differ, then not only are
1171 // these not mergeable, but in fact we should give an error, as
1172 // we've found two execution paths that reach a branch target
1173 // with different stack depths. FIXME stackdepth instead?
1174 if (stacktop
!= other
->stacktop
)
1175 verifier
->verify_fail ("stack sizes differ");
1177 for (int i
= 0; i
< stacktop
; ++i
)
1178 if (! stack
[i
].state_mergeable_p (other
->stack
[i
]))
1180 for (int i
= 0; i
< max_locals
; ++i
)
1181 if (! locals
[i
].state_mergeable_p (other
->locals
[i
]))
1186 void reverify (_Jv_BytecodeVerifier
*verifier
)
1188 if (next
== INVALID_STATE
)
1190 next
= verifier
->next_verify_state
;
1191 verifier
->next_verify_state
= this;
1196 void print (const char *leader
, int pc
,
1197 int max_stack
, int max_locals
) const
1199 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1201 for (i
= 0; i
< stacktop
; ++i
)
1203 for (; i
< max_stack
; ++i
)
1205 debug_print (" [local] ");
1206 for (i
= 0; i
< max_locals
; ++i
)
1208 debug_print (" | %p\n", this);
1211 inline void print (const char *, int, int, int) const
1214 #endif /* VERIFY_DEBUG */
1219 if (current_state
->stacktop
<= 0)
1220 verify_fail ("stack empty");
1221 type r
= current_state
->stack
[--current_state
->stacktop
];
1222 current_state
->stackdepth
-= r
.depth ();
1223 if (current_state
->stackdepth
< 0)
1224 verify_fail ("stack empty", start_PC
);
1230 type r
= pop_raw ();
1232 verify_fail ("narrow pop of wide type");
1236 type
pop_type (type match
)
1239 type t
= pop_raw ();
1240 if (! match
.compatible (t
, this))
1241 verify_fail ("incompatible type on stack");
1245 // Pop a reference which is guaranteed to be initialized. MATCH
1246 // doesn't have to be a reference type; in this case this acts like
1248 type
pop_init_ref (type match
)
1250 type t
= pop_raw ();
1251 if (t
.isreference () && ! t
.isinitialized ())
1252 verify_fail ("initialized reference required");
1253 else if (! match
.compatible (t
, this))
1254 verify_fail ("incompatible type on stack");
1258 // Pop a reference type or a return address.
1259 type
pop_ref_or_return ()
1261 type t
= pop_raw ();
1262 if (! t
.isreference () && t
.key
!= return_address_type
)
1263 verify_fail ("expected reference or return address on stack");
1267 void push_type (type t
)
1269 // If T is a numeric type like short, promote it to int.
1272 int depth
= t
.depth ();
1273 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1274 verify_fail ("stack overflow");
1275 current_state
->stack
[current_state
->stacktop
++] = t
;
1276 current_state
->stackdepth
+= depth
;
1279 void set_variable (int index
, type t
)
1281 // If T is a numeric type like short, promote it to int.
1284 int depth
= t
.depth ();
1285 if (index
> current_method
->max_locals
- depth
)
1286 verify_fail ("invalid local variable");
1287 current_state
->locals
[index
] = t
;
1290 current_state
->locals
[index
+ 1] = continuation_type
;
1291 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1292 current_state
->locals
[index
- 1] = unsuitable_type
;
1295 type
get_variable (int index
, type t
)
1297 int depth
= t
.depth ();
1298 if (index
> current_method
->max_locals
- depth
)
1299 verify_fail ("invalid local variable");
1300 if (! t
.compatible (current_state
->locals
[index
], this))
1301 verify_fail ("incompatible type in local variable");
1304 type
t (continuation_type
);
1305 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1306 verify_fail ("invalid local variable");
1308 return current_state
->locals
[index
];
1311 // Make sure ARRAY is an array type and that its elements are
1312 // compatible with type ELEMENT. Returns the actual element type.
1313 type
require_array_type (type array
, type element
)
1315 // An odd case. Here we just pretend that everything went ok. If
1316 // the requested element type is some kind of reference, return
1317 // the null type instead.
1318 if (array
.isnull ())
1319 return element
.isreference () ? type (null_type
) : element
;
1321 if (! array
.isarray ())
1322 verify_fail ("array required");
1324 type t
= array
.element_type (this);
1325 if (! element
.compatible (t
, this))
1327 // Special case for byte arrays, which must also be boolean
1330 if (element
.key
== byte_type
)
1332 type
e2 (boolean_type
);
1333 ok
= e2
.compatible (t
, this);
1336 verify_fail ("incompatible array element type");
1339 // Return T and not ELEMENT, because T might be specialized.
1345 if (PC
>= current_method
->code_length
)
1346 verify_fail ("premature end of bytecode");
1347 return (jint
) bytecode
[PC
++] & 0xff;
1352 jint b1
= get_byte ();
1353 jint b2
= get_byte ();
1354 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1359 jint b1
= get_byte ();
1360 jint b2
= get_byte ();
1361 jshort s
= (b1
<< 8) | b2
;
1367 jint b1
= get_byte ();
1368 jint b2
= get_byte ();
1369 jint b3
= get_byte ();
1370 jint b4
= get_byte ();
1371 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1374 int compute_jump (int offset
)
1376 int npc
= start_PC
+ offset
;
1377 if (npc
< 0 || npc
>= current_method
->code_length
)
1378 verify_fail ("branch out of range", start_PC
);
1382 // Add a new state to the state list at NPC.
1383 state
*add_new_state (int npc
, state
*old_state
)
1385 state
*new_state
= new state (old_state
, current_method
->max_stack
,
1386 current_method
->max_locals
);
1387 debug_print ("== New state in add_new_state\n");
1388 new_state
->print ("New", npc
, current_method
->max_stack
,
1389 current_method
->max_locals
);
1390 linked
<state
> *nlink
1391 = (linked
<state
> *) _Jv_Malloc (sizeof (linked
<state
>));
1392 nlink
->val
= new_state
;
1393 nlink
->next
= states
[npc
];
1394 states
[npc
] = nlink
;
1395 new_state
->set_pc (npc
);
1399 // Merge the indicated state into the state at the branch target and
1400 // schedule a new PC if there is a change. NPC is the PC of the
1401 // branch target, and FROM_STATE is the state at the source of the
1402 // branch. This method returns true if the destination state
1403 // changed and requires reverification, false otherwise.
1404 void merge_into (int npc
, state
*from_state
)
1406 // Iterate over all target states and merge our state into each,
1407 // if applicable. FIXME one improvement we could make here is
1408 // "state destruction". Merging a new state into an existing one
1409 // might cause a return_address_type to be merged to
1410 // unsuitable_type. In this case the resulting state may now be
1411 // mergeable with other states currently held in parallel at this
1412 // location. So in this situation we could pairwise compare and
1413 // reduce the number of parallel states.
1414 bool applicable
= false;
1415 for (linked
<state
> *iter
= states
[npc
]; iter
!= NULL
; iter
= iter
->next
)
1417 state
*new_state
= iter
->val
;
1418 if (new_state
->state_mergeable_p (from_state
,
1419 current_method
->max_locals
, this))
1423 debug_print ("== Merge states in merge_into\n");
1424 from_state
->print ("Frm", start_PC
, current_method
->max_stack
,
1425 current_method
->max_locals
);
1426 new_state
->print (" To", npc
, current_method
->max_stack
,
1427 current_method
->max_locals
);
1428 bool changed
= new_state
->merge (from_state
,
1429 current_method
->max_locals
,
1431 new_state
->print ("New", npc
, current_method
->max_stack
,
1432 current_method
->max_locals
);
1435 new_state
->reverify (this);
1441 // Either we don't yet have a state at NPC, or we have a
1442 // return-address type that is in conflict with all existing
1443 // state. So, we need to create a new entry.
1444 state
*new_state
= add_new_state (npc
, from_state
);
1445 // A new state added in this way must always be reverified.
1446 new_state
->reverify (this);
1450 void push_jump (int offset
)
1452 int npc
= compute_jump (offset
);
1453 // According to the JVM Spec, we need to check for uninitialized
1454 // objects here. However, this does not actually affect type
1455 // safety, and the Eclipse java compiler generates code that
1456 // violates this constraint.
1457 merge_into (npc
, current_state
);
1460 void push_exception_jump (type t
, int pc
)
1462 // According to the JVM Spec, we need to check for uninitialized
1463 // objects here. However, this does not actually affect type
1464 // safety, and the Eclipse java compiler generates code that
1465 // violates this constraint.
1466 state
s (current_state
, current_method
->max_stack
,
1467 current_method
->max_locals
);
1468 if (current_method
->max_stack
< 1)
1469 verify_fail ("stack overflow at exception handler");
1470 s
.set_exception (t
, current_method
->max_stack
);
1471 merge_into (pc
, &s
);
1476 state
*new_state
= next_verify_state
;
1477 if (new_state
== INVALID_STATE
)
1478 verify_fail ("programmer error in pop_jump");
1479 if (new_state
!= NULL
)
1481 next_verify_state
= new_state
->next
;
1482 new_state
->next
= INVALID_STATE
;
1487 void invalidate_pc ()
1489 PC
= state::NO_NEXT
;
1492 void note_branch_target (int pc
)
1494 // Don't check `pc <= PC', because we've advanced PC after
1495 // fetching the target and we haven't yet checked the next
1497 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1498 verify_fail ("branch not to instruction start", start_PC
);
1499 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1502 void skip_padding ()
1504 while ((PC
% 4) > 0)
1505 if (get_byte () != 0)
1506 verify_fail ("found nonzero padding byte");
1509 // Do the work for a `ret' instruction. INDEX is the index into the
1511 void handle_ret_insn (int index
)
1513 type ret_addr
= get_variable (index
, return_address_type
);
1514 // It would be nice if we could do this. However, the JVM Spec
1515 // doesn't say that this is what happens. It is implied that
1516 // reusing a return address is invalid, but there's no actual
1517 // prohibition against it.
1518 // set_variable (index, unsuitable_type);
1520 int npc
= ret_addr
.get_pc ();
1521 // We might be returning to a `jsr' that is at the end of the
1522 // bytecode. This is ok if we never return from the called
1523 // subroutine, but if we see this here it is an error.
1524 if (npc
>= current_method
->code_length
)
1525 verify_fail ("fell off end");
1527 // According to the JVM Spec, we need to check for uninitialized
1528 // objects here. However, this does not actually affect type
1529 // safety, and the Eclipse java compiler generates code that
1530 // violates this constraint.
1531 merge_into (npc
, current_state
);
1535 void handle_jsr_insn (int offset
)
1537 int npc
= compute_jump (offset
);
1539 // According to the JVM Spec, we need to check for uninitialized
1540 // objects here. However, this does not actually affect type
1541 // safety, and the Eclipse java compiler generates code that
1542 // violates this constraint.
1544 // Modify our state as appropriate for entry into a subroutine.
1545 type
ret_addr (return_address_type
);
1546 ret_addr
.set_return_address (PC
);
1547 push_type (ret_addr
);
1548 merge_into (npc
, current_state
);
1552 jclass
construct_primitive_array_type (type_val prim
)
1558 k
= JvPrimClass (boolean
);
1561 k
= JvPrimClass (char);
1564 k
= JvPrimClass (float);
1567 k
= JvPrimClass (double);
1570 k
= JvPrimClass (byte
);
1573 k
= JvPrimClass (short);
1576 k
= JvPrimClass (int);
1579 k
= JvPrimClass (long);
1582 // These aren't used here but we call them out to avoid
1585 case unsuitable_type
:
1586 case return_address_type
:
1587 case continuation_type
:
1588 case reference_type
:
1590 case uninitialized_reference_type
:
1592 verify_fail ("unknown type in construct_primitive_array_type");
1594 k
= _Jv_GetArrayClass (k
, NULL
);
1598 // This pass computes the location of branch targets and also
1599 // instruction starts.
1600 void branch_prepass ()
1602 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1604 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1608 while (PC
< current_method
->code_length
)
1610 // Set `start_PC' early so that error checking can have the
1613 flags
[PC
] |= FLAG_INSN_START
;
1615 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1619 case op_aconst_null
:
1755 case op_monitorenter
:
1756 case op_monitorexit
:
1764 case op_arraylength
:
1796 case op_invokespecial
:
1797 case op_invokestatic
:
1798 case op_invokevirtual
:
1802 case op_multianewarray
:
1825 note_branch_target (compute_jump (get_short ()));
1828 case op_tableswitch
:
1831 note_branch_target (compute_jump (get_int ()));
1832 jint low
= get_int ();
1833 jint hi
= get_int ();
1835 verify_fail ("invalid tableswitch", start_PC
);
1836 for (int i
= low
; i
<= hi
; ++i
)
1837 note_branch_target (compute_jump (get_int ()));
1841 case op_lookupswitch
:
1844 note_branch_target (compute_jump (get_int ()));
1845 int npairs
= get_int ();
1847 verify_fail ("too few pairs in lookupswitch", start_PC
);
1848 while (npairs
-- > 0)
1851 note_branch_target (compute_jump (get_int ()));
1856 case op_invokeinterface
:
1864 opcode
= (java_opcode
) get_byte ();
1866 if (opcode
== op_iinc
)
1873 note_branch_target (compute_jump (get_int ()));
1876 // These are unused here, but we call them out explicitly
1877 // so that -Wswitch-enum doesn't complain.
1883 case op_putstatic_1
:
1884 case op_putstatic_2
:
1885 case op_putstatic_4
:
1886 case op_putstatic_8
:
1887 case op_putstatic_a
:
1889 case op_getfield_2s
:
1890 case op_getfield_2u
:
1894 case op_getstatic_1
:
1895 case op_getstatic_2s
:
1896 case op_getstatic_2u
:
1897 case op_getstatic_4
:
1898 case op_getstatic_8
:
1899 case op_getstatic_a
:
1901 verify_fail ("unrecognized instruction in branch_prepass",
1905 // See if any previous branch tried to branch to the middle of
1906 // this instruction.
1907 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1909 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1910 verify_fail ("branch to middle of instruction", pc
);
1914 // Verify exception handlers.
1915 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1917 if (! (flags
[exception
[i
].handler_pc
.i
] & FLAG_INSN_START
))
1918 verify_fail ("exception handler not at instruction start",
1919 exception
[i
].handler_pc
.i
);
1920 if (! (flags
[exception
[i
].start_pc
.i
] & FLAG_INSN_START
))
1921 verify_fail ("exception start not at instruction start",
1922 exception
[i
].start_pc
.i
);
1923 if (exception
[i
].end_pc
.i
!= current_method
->code_length
1924 && ! (flags
[exception
[i
].end_pc
.i
] & FLAG_INSN_START
))
1925 verify_fail ("exception end not at instruction start",
1926 exception
[i
].end_pc
.i
);
1928 flags
[exception
[i
].handler_pc
.i
] |= FLAG_BRANCH_TARGET
;
1932 void check_pool_index (int index
)
1934 if (index
< 0 || index
>= current_class
->constants
.size
)
1935 verify_fail ("constant pool index out of range", start_PC
);
1938 type
check_class_constant (int index
)
1940 check_pool_index (index
);
1941 _Jv_Constants
*pool
= ¤t_class
->constants
;
1942 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1943 return type (pool
->data
[index
].clazz
, this);
1944 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1945 return type (pool
->data
[index
].utf8
, this);
1946 verify_fail ("expected class constant", start_PC
);
1949 type
check_constant (int index
)
1951 check_pool_index (index
);
1952 _Jv_Constants
*pool
= ¤t_class
->constants
;
1953 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1954 || pool
->tags
[index
] == JV_CONSTANT_String
)
1955 return type (&java::lang::String::class$
, this);
1956 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1957 return type (int_type
);
1958 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1959 return type (float_type
);
1960 verify_fail ("String, int, or float constant expected", start_PC
);
1963 type
check_wide_constant (int index
)
1965 check_pool_index (index
);
1966 _Jv_Constants
*pool
= ¤t_class
->constants
;
1967 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1968 return type (long_type
);
1969 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1970 return type (double_type
);
1971 verify_fail ("long or double constant expected", start_PC
);
1974 // Helper for both field and method. These are laid out the same in
1975 // the constant pool.
1976 type
handle_field_or_method (int index
, int expected
,
1977 _Jv_Utf8Const
**name
,
1978 _Jv_Utf8Const
**fmtype
)
1980 check_pool_index (index
);
1981 _Jv_Constants
*pool
= ¤t_class
->constants
;
1982 if (pool
->tags
[index
] != expected
)
1983 verify_fail ("didn't see expected constant", start_PC
);
1984 // Once we know we have a Fieldref or Methodref we assume that it
1985 // is correctly laid out in the constant pool. I think the code
1986 // in defineclass.cc guarantees this.
1987 _Jv_ushort class_index
, name_and_type_index
;
1988 _Jv_loadIndexes (&pool
->data
[index
],
1990 name_and_type_index
);
1991 _Jv_ushort name_index
, desc_index
;
1992 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1993 name_index
, desc_index
);
1995 *name
= pool
->data
[name_index
].utf8
;
1996 *fmtype
= pool
->data
[desc_index
].utf8
;
1998 return check_class_constant (class_index
);
2001 // Return field's type, compute class' type if requested.
2002 // If PUTFIELD is true, use the special 'putfield' semantics.
2003 type
check_field_constant (int index
, type
*class_type
= NULL
,
2004 bool putfield
= false)
2006 _Jv_Utf8Const
*name
, *field_type
;
2007 type ct
= handle_field_or_method (index
,
2008 JV_CONSTANT_Fieldref
,
2009 &name
, &field_type
);
2013 if (field_type
->first() == '[' || field_type
->first() == 'L')
2014 result
= type (field_type
, this);
2016 result
= get_type_val_for_signature (field_type
->first());
2018 // We have an obscure special case here: we can use `putfield' on
2019 // a field declared in this class, even if `this' has not yet been
2022 && ! current_state
->this_type
.isinitialized ()
2023 && current_state
->this_type
.pc
== type::SELF
2024 && current_state
->this_type
.equals (ct
, this)
2025 // We don't look at the signature, figuring that if it is
2026 // wrong we will fail during linking. FIXME?
2027 && _Jv_Linker::has_field_p (current_class
, name
))
2028 // Note that we don't actually know whether we're going to match
2029 // against 'this' or some other object of the same type. So,
2030 // here we set things up so that it doesn't matter. This relies
2031 // on knowing what our caller is up to.
2032 class_type
->set_uninitialized (type::EITHER
, this);
2037 type
check_method_constant (int index
, bool is_interface
,
2038 _Jv_Utf8Const
**method_name
,
2039 _Jv_Utf8Const
**method_signature
)
2041 return handle_field_or_method (index
,
2043 ? JV_CONSTANT_InterfaceMethodref
2044 : JV_CONSTANT_Methodref
),
2045 method_name
, method_signature
);
2048 type
get_one_type (char *&p
)
2066 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
2067 return type (name
, this);
2070 // Casting to jchar here is ok since we are looking at an ASCII
2072 type_val rt
= get_type_val_for_signature (jchar (v
));
2074 if (arraycount
== 0)
2076 // Callers of this function eventually push their arguments on
2077 // the stack. So, promote them here.
2078 return type (rt
).promote ();
2081 jclass k
= construct_primitive_array_type (rt
);
2082 while (--arraycount
> 0)
2083 k
= _Jv_GetArrayClass (k
, NULL
);
2084 return type (k
, this);
2087 void compute_argument_types (_Jv_Utf8Const
*signature
,
2090 char *p
= signature
->chars();
2097 types
[i
++] = get_one_type (p
);
2100 type
compute_return_type (_Jv_Utf8Const
*signature
)
2102 char *p
= signature
->chars();
2106 return get_one_type (p
);
2109 void check_return_type (type onstack
)
2111 type rt
= compute_return_type (current_method
->self
->signature
);
2112 if (! rt
.compatible (onstack
, this))
2113 verify_fail ("incompatible return type");
2116 // Initialize the stack for the new method. Returns true if this
2117 // method is an instance initializer.
2118 bool initialize_stack ()
2121 bool is_init
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2123 bool is_clinit
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2126 using namespace java::lang::reflect
;
2127 if (! Modifier::isStatic (current_method
->self
->accflags
))
2129 type
kurr (current_class
, this);
2132 kurr
.set_uninitialized (type::SELF
, this);
2136 verify_fail ("<clinit> method must be static");
2137 set_variable (0, kurr
);
2138 current_state
->set_this_type (kurr
);
2144 verify_fail ("<init> method must be non-static");
2147 // We have to handle wide arguments specially here.
2148 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2149 type arg_types
[arg_count
];
2150 compute_argument_types (current_method
->self
->signature
, arg_types
);
2151 for (int i
= 0; i
< arg_count
; ++i
)
2153 set_variable (var
, arg_types
[i
]);
2155 if (arg_types
[i
].iswide ())
2162 void verify_instructions_0 ()
2164 current_state
= new state (current_method
->max_stack
,
2165 current_method
->max_locals
);
2170 // True if we are verifying an instance initializer.
2171 bool this_is_init
= initialize_stack ();
2173 states
= (linked
<state
> **) _Jv_Malloc (sizeof (linked
<state
> *)
2174 * current_method
->code_length
);
2175 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2178 next_verify_state
= NULL
;
2182 // If the PC was invalidated, get a new one from the work list.
2183 if (PC
== state::NO_NEXT
)
2185 state
*new_state
= pop_jump ();
2186 // If it is null, we're done.
2187 if (new_state
== NULL
)
2190 PC
= new_state
->get_pc ();
2191 debug_print ("== State pop from pending list\n");
2192 // Set up the current state.
2193 current_state
->copy (new_state
, current_method
->max_stack
,
2194 current_method
->max_locals
);
2198 // We only have to do this checking in the situation where
2199 // control flow falls through from the previous
2200 // instruction. Otherwise merging is done at the time we
2201 // push the branch. Note that we'll catch the
2202 // off-the-end problem just below.
2203 if (PC
< current_method
->code_length
&& states
[PC
] != NULL
)
2205 // We've already visited this instruction. So merge
2206 // the states together. It is simplest, but not most
2207 // efficient, to just always invalidate the PC here.
2208 merge_into (PC
, current_state
);
2214 // Control can't fall off the end of the bytecode. We need to
2215 // check this in both cases, not just the fall-through case,
2216 // because we don't check to see whether a `jsr' appears at
2217 // the end of the bytecode until we process a `ret'.
2218 if (PC
>= current_method
->code_length
)
2219 verify_fail ("fell off end");
2221 // We only have to keep saved state at branch targets. If
2222 // we're at a branch target and the state here hasn't been set
2223 // yet, we set it now. You might notice that `ret' targets
2224 // won't necessarily have FLAG_BRANCH_TARGET set. This
2225 // doesn't matter, since those states will be filled in by
2227 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2228 add_new_state (PC
, current_state
);
2230 // Set this before handling exceptions so that debug output is
2234 // Update states for all active exception handlers. Ordinarily
2235 // there are not many exception handlers. So we simply run
2236 // through them all.
2237 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2239 if (PC
>= exception
[i
].start_pc
.i
&& PC
< exception
[i
].end_pc
.i
)
2241 type
handler (&java::lang::Throwable::class$
, this);
2242 if (exception
[i
].handler_type
.i
!= 0)
2243 handler
= check_class_constant (exception
[i
].handler_type
.i
);
2244 push_exception_jump (handler
, exception
[i
].handler_pc
.i
);
2248 current_state
->print (" ", PC
, current_method
->max_stack
,
2249 current_method
->max_locals
);
2250 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2256 case op_aconst_null
:
2257 push_type (null_type
);
2267 push_type (int_type
);
2272 push_type (long_type
);
2278 push_type (float_type
);
2283 push_type (double_type
);
2288 push_type (int_type
);
2293 push_type (int_type
);
2297 push_type (check_constant (get_byte ()));
2300 push_type (check_constant (get_ushort ()));
2303 push_type (check_wide_constant (get_ushort ()));
2307 push_type (get_variable (get_byte (), int_type
));
2310 push_type (get_variable (get_byte (), long_type
));
2313 push_type (get_variable (get_byte (), float_type
));
2316 push_type (get_variable (get_byte (), double_type
));
2319 push_type (get_variable (get_byte (), reference_type
));
2326 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2332 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2338 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2344 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2350 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2353 pop_type (int_type
);
2354 push_type (require_array_type (pop_init_ref (reference_type
),
2358 pop_type (int_type
);
2359 push_type (require_array_type (pop_init_ref (reference_type
),
2363 pop_type (int_type
);
2364 push_type (require_array_type (pop_init_ref (reference_type
),
2368 pop_type (int_type
);
2369 push_type (require_array_type (pop_init_ref (reference_type
),
2373 pop_type (int_type
);
2374 push_type (require_array_type (pop_init_ref (reference_type
),
2378 pop_type (int_type
);
2379 require_array_type (pop_init_ref (reference_type
), byte_type
);
2380 push_type (int_type
);
2383 pop_type (int_type
);
2384 require_array_type (pop_init_ref (reference_type
), char_type
);
2385 push_type (int_type
);
2388 pop_type (int_type
);
2389 require_array_type (pop_init_ref (reference_type
), short_type
);
2390 push_type (int_type
);
2393 set_variable (get_byte (), pop_type (int_type
));
2396 set_variable (get_byte (), pop_type (long_type
));
2399 set_variable (get_byte (), pop_type (float_type
));
2402 set_variable (get_byte (), pop_type (double_type
));
2405 set_variable (get_byte (), pop_ref_or_return ());
2411 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2417 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2423 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2429 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2435 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2438 pop_type (int_type
);
2439 pop_type (int_type
);
2440 require_array_type (pop_init_ref (reference_type
), int_type
);
2443 pop_type (long_type
);
2444 pop_type (int_type
);
2445 require_array_type (pop_init_ref (reference_type
), long_type
);
2448 pop_type (float_type
);
2449 pop_type (int_type
);
2450 require_array_type (pop_init_ref (reference_type
), float_type
);
2453 pop_type (double_type
);
2454 pop_type (int_type
);
2455 require_array_type (pop_init_ref (reference_type
), double_type
);
2458 pop_type (reference_type
);
2459 pop_type (int_type
);
2460 require_array_type (pop_init_ref (reference_type
), reference_type
);
2463 pop_type (int_type
);
2464 pop_type (int_type
);
2465 require_array_type (pop_init_ref (reference_type
), byte_type
);
2468 pop_type (int_type
);
2469 pop_type (int_type
);
2470 require_array_type (pop_init_ref (reference_type
), char_type
);
2473 pop_type (int_type
);
2474 pop_type (int_type
);
2475 require_array_type (pop_init_ref (reference_type
), short_type
);
2482 type t
= pop_raw ();
2506 type t2
= pop_raw ();
2521 type t
= pop_raw ();
2536 type t1
= pop_raw ();
2553 type t1
= pop_raw ();
2556 type t2
= pop_raw ();
2574 type t3
= pop_raw ();
2612 pop_type (int_type
);
2613 push_type (pop_type (int_type
));
2623 pop_type (long_type
);
2624 push_type (pop_type (long_type
));
2629 pop_type (int_type
);
2630 push_type (pop_type (long_type
));
2637 pop_type (float_type
);
2638 push_type (pop_type (float_type
));
2645 pop_type (double_type
);
2646 push_type (pop_type (double_type
));
2652 push_type (pop_type (int_type
));
2655 push_type (pop_type (long_type
));
2658 push_type (pop_type (float_type
));
2661 push_type (pop_type (double_type
));
2664 get_variable (get_byte (), int_type
);
2668 pop_type (int_type
);
2669 push_type (long_type
);
2672 pop_type (int_type
);
2673 push_type (float_type
);
2676 pop_type (int_type
);
2677 push_type (double_type
);
2680 pop_type (long_type
);
2681 push_type (int_type
);
2684 pop_type (long_type
);
2685 push_type (float_type
);
2688 pop_type (long_type
);
2689 push_type (double_type
);
2692 pop_type (float_type
);
2693 push_type (int_type
);
2696 pop_type (float_type
);
2697 push_type (long_type
);
2700 pop_type (float_type
);
2701 push_type (double_type
);
2704 pop_type (double_type
);
2705 push_type (int_type
);
2708 pop_type (double_type
);
2709 push_type (long_type
);
2712 pop_type (double_type
);
2713 push_type (float_type
);
2716 pop_type (long_type
);
2717 pop_type (long_type
);
2718 push_type (int_type
);
2722 pop_type (float_type
);
2723 pop_type (float_type
);
2724 push_type (int_type
);
2728 pop_type (double_type
);
2729 pop_type (double_type
);
2730 push_type (int_type
);
2738 pop_type (int_type
);
2739 push_jump (get_short ());
2747 pop_type (int_type
);
2748 pop_type (int_type
);
2749 push_jump (get_short ());
2753 pop_type (reference_type
);
2754 pop_type (reference_type
);
2755 push_jump (get_short ());
2758 push_jump (get_short ());
2762 handle_jsr_insn (get_short ());
2765 handle_ret_insn (get_byte ());
2767 case op_tableswitch
:
2769 pop_type (int_type
);
2771 push_jump (get_int ());
2772 jint low
= get_int ();
2773 jint high
= get_int ();
2774 // Already checked LOW -vs- HIGH.
2775 for (int i
= low
; i
<= high
; ++i
)
2776 push_jump (get_int ());
2781 case op_lookupswitch
:
2783 pop_type (int_type
);
2785 push_jump (get_int ());
2786 jint npairs
= get_int ();
2787 // Already checked NPAIRS >= 0.
2789 for (int i
= 0; i
< npairs
; ++i
)
2791 jint key
= get_int ();
2792 if (i
> 0 && key
<= lastkey
)
2793 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2795 push_jump (get_int ());
2801 check_return_type (pop_type (int_type
));
2805 check_return_type (pop_type (long_type
));
2809 check_return_type (pop_type (float_type
));
2813 check_return_type (pop_type (double_type
));
2817 check_return_type (pop_init_ref (reference_type
));
2821 // We only need to check this when the return type is
2822 // void, because all instance initializers return void.
2824 current_state
->check_this_initialized (this);
2825 check_return_type (void_type
);
2829 push_type (check_field_constant (get_ushort ()));
2832 pop_type (check_field_constant (get_ushort ()));
2837 type field
= check_field_constant (get_ushort (), &klass
);
2845 type field
= check_field_constant (get_ushort (), &klass
, true);
2851 case op_invokevirtual
:
2852 case op_invokespecial
:
2853 case op_invokestatic
:
2854 case op_invokeinterface
:
2856 _Jv_Utf8Const
*method_name
, *method_signature
;
2858 = check_method_constant (get_ushort (),
2859 opcode
== op_invokeinterface
,
2862 // NARGS is only used when we're processing
2863 // invokeinterface. It is simplest for us to compute it
2864 // here and then verify it later.
2866 if (opcode
== op_invokeinterface
)
2868 nargs
= get_byte ();
2869 if (get_byte () != 0)
2870 verify_fail ("invokeinterface dummy byte is wrong");
2873 bool is_init
= false;
2874 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2877 if (opcode
!= op_invokespecial
)
2878 verify_fail ("can't invoke <init>");
2880 else if (method_name
->first() == '<')
2881 verify_fail ("can't invoke method starting with `<'");
2883 // Pop arguments and check types.
2884 int arg_count
= _Jv_count_arguments (method_signature
);
2885 type arg_types
[arg_count
];
2886 compute_argument_types (method_signature
, arg_types
);
2887 for (int i
= arg_count
- 1; i
>= 0; --i
)
2889 // This is only used for verifying the byte for
2891 nargs
-= arg_types
[i
].depth ();
2892 pop_init_ref (arg_types
[i
]);
2895 if (opcode
== op_invokeinterface
2897 verify_fail ("wrong argument count for invokeinterface");
2899 if (opcode
!= op_invokestatic
)
2901 type t
= class_type
;
2904 // In this case the PC doesn't matter.
2905 t
.set_uninitialized (type::UNINIT
, this);
2906 // FIXME: check to make sure that the <init>
2907 // call is to the right class.
2908 // It must either be super or an exact class
2911 type raw
= pop_raw ();
2912 if (! t
.compatible (raw
, this))
2913 verify_fail ("incompatible type on stack");
2916 current_state
->set_initialized (raw
.get_pc (),
2917 current_method
->max_locals
);
2920 type rt
= compute_return_type (method_signature
);
2928 type t
= check_class_constant (get_ushort ());
2930 verify_fail ("type is array");
2931 t
.set_uninitialized (start_PC
, this);
2938 int atype
= get_byte ();
2939 // We intentionally have chosen constants to make this
2941 if (atype
< boolean_type
|| atype
> long_type
)
2942 verify_fail ("type not primitive", start_PC
);
2943 pop_type (int_type
);
2944 type
t (construct_primitive_array_type (type_val (atype
)), this);
2949 pop_type (int_type
);
2950 push_type (check_class_constant (get_ushort ()).to_array (this));
2952 case op_arraylength
:
2954 type t
= pop_init_ref (reference_type
);
2955 if (! t
.isarray () && ! t
.isnull ())
2956 verify_fail ("array type expected");
2957 push_type (int_type
);
2961 pop_type (type (&java::lang::Throwable::class$
, this));
2965 pop_init_ref (reference_type
);
2966 push_type (check_class_constant (get_ushort ()));
2969 pop_init_ref (reference_type
);
2970 check_class_constant (get_ushort ());
2971 push_type (int_type
);
2973 case op_monitorenter
:
2974 pop_init_ref (reference_type
);
2976 case op_monitorexit
:
2977 pop_init_ref (reference_type
);
2981 switch (get_byte ())
2984 push_type (get_variable (get_ushort (), int_type
));
2987 push_type (get_variable (get_ushort (), long_type
));
2990 push_type (get_variable (get_ushort (), float_type
));
2993 push_type (get_variable (get_ushort (), double_type
));
2996 push_type (get_variable (get_ushort (), reference_type
));
2999 set_variable (get_ushort (), pop_type (int_type
));
3002 set_variable (get_ushort (), pop_type (long_type
));
3005 set_variable (get_ushort (), pop_type (float_type
));
3008 set_variable (get_ushort (), pop_type (double_type
));
3011 set_variable (get_ushort (), pop_init_ref (reference_type
));
3014 handle_ret_insn (get_short ());
3017 get_variable (get_ushort (), int_type
);
3021 verify_fail ("unrecognized wide instruction", start_PC
);
3025 case op_multianewarray
:
3027 type atype
= check_class_constant (get_ushort ());
3028 int dim
= get_byte ();
3030 verify_fail ("too few dimensions to multianewarray", start_PC
);
3031 atype
.verify_dimensions (dim
, this);
3032 for (int i
= 0; i
< dim
; ++i
)
3033 pop_type (int_type
);
3039 pop_type (reference_type
);
3040 push_jump (get_short ());
3043 push_jump (get_int ());
3047 handle_jsr_insn (get_int ());
3050 // These are unused here, but we call them out explicitly
3051 // so that -Wswitch-enum doesn't complain.
3057 case op_putstatic_1
:
3058 case op_putstatic_2
:
3059 case op_putstatic_4
:
3060 case op_putstatic_8
:
3061 case op_putstatic_a
:
3063 case op_getfield_2s
:
3064 case op_getfield_2u
:
3068 case op_getstatic_1
:
3069 case op_getstatic_2s
:
3070 case op_getstatic_2u
:
3071 case op_getstatic_4
:
3072 case op_getstatic_8
:
3073 case op_getstatic_a
:
3075 // Unrecognized opcode.
3076 verify_fail ("unrecognized instruction in verify_instructions_0",
3084 void verify_instructions ()
3087 verify_instructions_0 ();
3090 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3092 // We just print the text as utf-8. This is just for debugging
3094 debug_print ("--------------------------------\n");
3095 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->chars());
3098 bytecode
= m
->bytecode ();
3099 exception
= m
->exceptions ();
3100 current_class
= m
->defining_class
;
3108 ~_Jv_BytecodeVerifier ()
3113 while (utf8_list
!= NULL
)
3115 linked
<_Jv_Utf8Const
> *n
= utf8_list
->next
;
3116 _Jv_Free (utf8_list
);
3120 while (isect_list
!= NULL
)
3122 ref_intersection
*next
= isect_list
->alloc_next
;
3129 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3131 linked
<state
> *iter
= states
[i
];
3132 while (iter
!= NULL
)
3134 linked
<state
> *next
= iter
->next
;
3146 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3148 _Jv_BytecodeVerifier
v (meth
);
3149 v
.verify_instructions ();
3152 #endif /* INTERPRETER */