1 // verify.cc - verify bytecode
3 /* Copyright (C) 2001, 2002, 2003, 2004 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>
37 #endif /* VERIFY_DEBUG */
40 // This is used to mark states which are not scheduled for
42 #define INVALID_STATE ((state *) -1)
44 static void debug_print (const char *fmt
, ...)
45 __attribute__ ((format (printf
, 1, 2)));
48 debug_print (MAYBE_UNUSED
const char *fmt
, ...)
53 vfprintf (stderr
, fmt
, ap
);
55 #endif /* VERIFY_DEBUG */
58 // This started as a fairly ordinary verifier, and for the most part
59 // it remains so. It works in the obvious way, by modeling the effect
60 // of each opcode as it is encountered. For most opcodes, this is a
61 // straightforward operation.
63 // This verifier does not do type merging. It used to, but this
64 // results in difficulty verifying some relatively simple code
65 // involving interfaces, and it pushed some verification work into the
68 // Instead of merging reference types, when we reach a point where two
69 // flows of control merge, we simply keep the union of reference types
70 // from each branch. Then, when we need to verify a fact about a
71 // reference on the stack (e.g., that it is compatible with the
72 // argument type of a method), we check to ensure that all possible
73 // types satisfy the requirement.
75 // Another area this verifier differs from the norm is in its handling
76 // of subroutines. The JVM specification has some confusing things to
77 // say about subroutines. For instance, it makes claims about not
78 // allowing subroutines to merge and it rejects recursive subroutines.
79 // For the most part these are red herrings; we used to try to follow
80 // these things but they lead to problems. For example, the notion of
81 // "being in a subroutine" is not well-defined: is an exception
82 // handler in a subroutine? If you never execute the `ret' but
83 // instead `goto 1' do you remain in the subroutine?
85 // For clarity on what is really required for type safety, read
86 // "Simple Verification Technique for Complex Java Bytecode
87 // Subroutines" by Alessandro Coglio. Among other things this paper
88 // shows that recursive subroutines are not harmful to type safety.
89 // We implement something similar to what he proposes. Note that this
90 // means that this verifier will accept code that is rejected by some
93 // For those not wanting to read the paper, the basic observation is
94 // that we can maintain split states in subroutines. We maintain one
95 // state for each calling `jsr'. In other words, we re-verify a
96 // subroutine once for each caller, using the exact types held by the
97 // callers (as opposed to the old approach of merging types and
98 // keeping a bitmap registering what did or did not change). This
99 // approach lets us continue to verify correctly even when a
100 // subroutine is exited via `goto' or `athrow' and not `ret'.
102 // In some other areas the JVM specification is (mildly) incorrect,
103 // so we diverge. For instance, you cannot
104 // violate type safety by allocating an object with `new' and then
105 // failing to initialize it, no matter how one branches or where one
106 // stores the uninitialized reference. See "Improving the official
107 // specification of Java bytecode verification" by Alessandro Coglio.
109 // Note that there's no real point in enforcing that padding bytes or
110 // the mystery byte of invokeinterface must be 0, but we do that
113 // The verifier is currently neither completely lazy nor eager when it
114 // comes to loading classes. It tries to represent types by name when
115 // possible, and then loads them when it needs to verify a fact about
116 // the type. Checking types by name is valid because we only use
117 // names which come from the current class' constant pool. Since all
118 // such names are looked up using the same class loader, there is no
119 // danger that we might be fooled into comparing different types with
122 // In the future we plan to allow for a completely lazy mode of
123 // operation, where the verifier will construct a list of type
124 // assertions to be checked later.
126 // Some test cases for the verifier live in the "verify" module of the
127 // Mauve test suite. However, some of these are presently
128 // (2004-01-20) believed to be incorrect. (More precisely the notion
129 // of "correct" is not well-defined, and this verifier differs from
130 // others while remaining type-safe.) Some other tests live in the
131 // libgcj test suite.
132 class _Jv_BytecodeVerifier
136 static const int FLAG_INSN_START
= 1;
137 static const int FLAG_BRANCH_TARGET
= 2;
142 struct ref_intersection
;
153 // The PC corresponding to the start of the current instruction.
156 // The current state of the stack, locals, etc.
157 state
*current_state
;
159 // At each branch target we keep a linked list of all the states we
160 // can process at that point. We'll only have multiple states at a
161 // given PC if they both have different return-address types in the
162 // same stack or local slot. This array is indexed by PC and holds
163 // the list of all such states.
164 linked
<state
> **states
;
166 // We keep a linked list of all the states which we must reverify.
167 // This is the head of the list.
168 state
*next_verify_state
;
170 // We keep some flags for each instruction. The values are the
171 // FLAG_* constants defined above. This is an array indexed by PC.
174 // The bytecode itself.
175 unsigned char *bytecode
;
177 _Jv_InterpException
*exception
;
180 jclass current_class
;
182 _Jv_InterpMethod
*current_method
;
184 // A linked list of utf8 objects we allocate.
185 linked
<_Jv_Utf8Const
> *utf8_list
;
187 // A linked list of all ref_intersection objects we allocate.
188 ref_intersection
*isect_list
;
190 // Create a new Utf-8 constant and return it. We do this to avoid
191 // having our Utf-8 constants prematurely collected.
192 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
194 linked
<_Jv_Utf8Const
> *lu
= (linked
<_Jv_Utf8Const
> *)
195 _Jv_Malloc (sizeof (linked
<_Jv_Utf8Const
>)
196 + _Jv_Utf8Const::space_needed(s
, len
));
197 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) (lu
+ 1);
200 lu
->next
= utf8_list
;
206 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
208 using namespace java::lang
;
209 StringBuffer
*buf
= new StringBuffer ();
211 buf
->append (JvNewStringLatin1 ("verification failed"));
216 buf
->append (JvNewStringLatin1 (" at PC "));
220 _Jv_InterpMethod
*method
= current_method
;
221 buf
->append (JvNewStringLatin1 (" in "));
222 buf
->append (current_class
->getName());
223 buf
->append ((jchar
) ':');
224 buf
->append (method
->get_method()->name
->toString());
225 buf
->append ((jchar
) '(');
226 buf
->append (method
->get_method()->signature
->toString());
227 buf
->append ((jchar
) ')');
229 buf
->append (JvNewStringLatin1 (": "));
230 buf
->append (JvNewStringLatin1 (s
));
231 throw new java::lang::VerifyError (buf
->toString ());
234 // This enum holds a list of tags for all the different types we
235 // need to handle. Reference types are treated specially by the
241 // The values for primitive types are chosen to correspond to values
242 // specified to newarray.
252 // Used when overwriting second word of a double or long in the
253 // local variables. Also used after merging local variable states
254 // to indicate an unusable value.
257 // This is the second word of a two-word value, i.e., a double or
261 // Everything after `reference_type' must be a reference type.
264 uninitialized_reference_type
267 // This represents a merged class type. Some verifiers (including
268 // earlier versions of this one) will compute the intersection of
269 // two class types when merging states. However, this loses
270 // critical information about interfaces implemented by the various
271 // classes. So instead we keep track of all the actual classes that
273 struct ref_intersection
275 // Whether or not this type has been resolved.
281 // For a resolved reference type, this is a pointer to the class.
283 // For other reference types, this it the name of the class.
287 // Link to the next reference in the intersection.
288 ref_intersection
*ref_next
;
290 // This is used to keep track of all the allocated
291 // ref_intersection objects, so we can free them.
292 // FIXME: we should allocate these in chunks.
293 ref_intersection
*alloc_next
;
295 ref_intersection (jclass klass
, _Jv_BytecodeVerifier
*verifier
)
300 alloc_next
= verifier
->isect_list
;
301 verifier
->isect_list
= this;
304 ref_intersection (_Jv_Utf8Const
*name
, _Jv_BytecodeVerifier
*verifier
)
309 alloc_next
= verifier
->isect_list
;
310 verifier
->isect_list
= this;
313 ref_intersection (ref_intersection
*dup
, ref_intersection
*tail
,
314 _Jv_BytecodeVerifier
*verifier
)
317 is_resolved
= dup
->is_resolved
;
319 alloc_next
= verifier
->isect_list
;
320 verifier
->isect_list
= this;
323 bool equals (ref_intersection
*other
, _Jv_BytecodeVerifier
*verifier
)
325 if (! is_resolved
&& ! other
->is_resolved
326 && _Jv_equalUtf8Consts (data
.name
, other
->data
.name
))
330 if (! other
->is_resolved
)
331 other
->resolve (verifier
);
332 return data
.klass
== other
->data
.klass
;
335 // Merge THIS type into OTHER, returning the result. This will
336 // return OTHER if all the classes in THIS already appear in
338 ref_intersection
*merge (ref_intersection
*other
,
339 _Jv_BytecodeVerifier
*verifier
)
341 ref_intersection
*tail
= other
;
342 for (ref_intersection
*self
= this; self
!= NULL
; self
= self
->ref_next
)
345 for (ref_intersection
*iter
= other
; iter
!= NULL
;
346 iter
= iter
->ref_next
)
348 if (iter
->equals (self
, verifier
))
356 tail
= new ref_intersection (self
, tail
, verifier
);
361 void resolve (_Jv_BytecodeVerifier
*verifier
)
366 using namespace java::lang
;
367 java::lang::ClassLoader
*loader
368 = verifier
->current_class
->getClassLoaderInternal();
369 // We might see either kind of name. Sigh.
370 if (data
.name
->first() == 'L' && data
.name
->limit()[-1] == ';')
371 data
.klass
= _Jv_FindClassFromSignature (data
.name
->chars(), loader
);
373 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
378 // See if an object of type OTHER can be assigned to an object of
379 // type *THIS. This might resolve classes in one chain or the
381 bool compatible (ref_intersection
*other
,
382 _Jv_BytecodeVerifier
*verifier
)
384 ref_intersection
*self
= this;
386 for (; self
!= NULL
; self
= self
->ref_next
)
388 ref_intersection
*other_iter
= other
;
390 for (; other_iter
!= NULL
; other_iter
= other_iter
->ref_next
)
392 // Avoid resolving if possible.
393 if (! self
->is_resolved
394 && ! other_iter
->is_resolved
395 && _Jv_equalUtf8Consts (self
->data
.name
,
396 other_iter
->data
.name
))
399 if (! self
->is_resolved
)
400 self
->resolve(verifier
);
401 if (! other_iter
->is_resolved
)
402 other_iter
->resolve(verifier
);
404 if (! is_assignable_from_slow (self
->data
.klass
,
405 other_iter
->data
.klass
))
415 // assert (ref_next == NULL);
417 return data
.klass
->isArray ();
419 return data
.name
->first() == '[';
422 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
424 // assert (ref_next == NULL);
427 return data
.klass
->isInterface ();
430 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
432 // assert (ref_next == NULL);
435 using namespace java::lang::reflect
;
436 return Modifier::isAbstract (data
.klass
->getModifiers ());
439 jclass
getclass (_Jv_BytecodeVerifier
*verifier
)
446 int count_dimensions ()
451 jclass k
= data
.klass
;
452 while (k
->isArray ())
454 k
= k
->getComponentType ();
460 char *p
= data
.name
->chars();
467 void *operator new (size_t bytes
)
469 return _Jv_Malloc (bytes
);
472 void operator delete (void *mem
)
478 // Return the type_val corresponding to a primitive signature
479 // character. For instance `I' returns `int.class'.
480 type_val
get_type_val_for_signature (jchar sig
)
513 verify_fail ("invalid signature");
518 // Return the type_val corresponding to a primitive class.
519 type_val
get_type_val_for_signature (jclass k
)
521 return get_type_val_for_signature ((jchar
) k
->method_count
);
524 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
525 // TARGET haven't been prepared.
526 static bool is_assignable_from_slow (jclass target
, jclass source
)
528 // First, strip arrays.
529 while (target
->isArray ())
531 // If target is array, source must be as well.
532 if (! source
->isArray ())
534 target
= target
->getComponentType ();
535 source
= source
->getComponentType ();
539 if (target
== &java::lang::Object::class$
)
544 if (source
== target
)
547 if (target
->isPrimitive () || source
->isPrimitive ())
550 if (target
->isInterface ())
552 for (int i
= 0; i
< source
->interface_count
; ++i
)
554 // We use a recursive call because we also need to
555 // check superinterfaces.
556 if (is_assignable_from_slow (target
, source
->getInterface (i
)))
560 source
= source
->getSuperclass ();
562 while (source
!= NULL
);
567 // The `type' class is used to represent a single type in the
574 // For reference types, the representation of the type.
575 ref_intersection
*klass
;
577 // This is used in two situations.
579 // First, when constructing a new object, it is the PC of the
580 // `new' instruction which created the object. We use the special
581 // value UNINIT to mean that this is uninitialized, and the
582 // special value SELF for the case where the current method is
583 // itself the <init> method.
585 // Second, when the key is return_address_type, this holds the PC
586 // of the instruction following the `jsr'.
589 static const int UNINIT
= -2;
590 static const int SELF
= -1;
592 // Basic constructor.
595 key
= unsuitable_type
;
600 // Make a new instance given the type tag. We assume a generic
601 // `reference_type' means Object.
605 // For reference_type, if KLASS==NULL then that means we are
606 // looking for a generic object of any kind, including an
607 // uninitialized reference.
612 // Make a new instance given a class.
613 type (jclass k
, _Jv_BytecodeVerifier
*verifier
)
615 key
= reference_type
;
616 klass
= new ref_intersection (k
, verifier
);
620 // Make a new instance given the name of a class.
621 type (_Jv_Utf8Const
*n
, _Jv_BytecodeVerifier
*verifier
)
623 key
= reference_type
;
624 klass
= new ref_intersection (n
, verifier
);
636 // These operators are required because libgcj can't link in
638 void *operator new[] (size_t bytes
)
640 return _Jv_Malloc (bytes
);
643 void operator delete[] (void *mem
)
648 type
& operator= (type_val k
)
656 type
& operator= (const type
& t
)
664 // Promote a numeric type.
667 if (key
== boolean_type
|| key
== char_type
668 || key
== byte_type
|| key
== short_type
)
673 // Mark this type as the uninitialized result of `new'.
674 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
676 if (key
== reference_type
)
677 key
= uninitialized_reference_type
;
679 verifier
->verify_fail ("internal error in type::uninitialized");
683 // Mark this type as now initialized.
684 void set_initialized (int npc
)
686 if (npc
!= UNINIT
&& pc
== npc
&& key
== uninitialized_reference_type
)
688 key
= reference_type
;
693 // Mark this type as a particular return address.
694 void set_return_address (int npc
)
699 // Return true if this type and type OTHER are considered
700 // mergeable for the purposes of state merging. This is related
701 // to subroutine handling. For this purpose two types are
702 // considered unmergeable if they are both return-addresses but
703 // have different PCs.
704 bool state_mergeable_p (const type
&other
) const
706 return (key
!= return_address_type
707 || other
.key
!= return_address_type
711 // Return true if an object of type K can be assigned to a variable
712 // of type *THIS. Handle various special cases too. Might modify
713 // *THIS or K. Note however that this does not perform numeric
715 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
717 // Any type is compatible with the unsuitable type.
718 if (key
== unsuitable_type
)
721 if (key
< reference_type
|| k
.key
< reference_type
)
724 // The `null' type is convertible to any initialized reference
726 if (key
== null_type
)
727 return k
.key
!= uninitialized_reference_type
;
728 if (k
.key
== null_type
)
729 return key
!= uninitialized_reference_type
;
731 // A special case for a generic reference.
735 verifier
->verify_fail ("programmer error in type::compatible");
737 // An initialized type and an uninitialized type are not
739 if (isinitialized () != k
.isinitialized ())
742 // Two uninitialized objects are compatible if either:
743 // * The PCs are identical, or
744 // * One PC is UNINIT.
745 if (! isinitialized ())
747 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
751 return klass
->compatible(k
.klass
, verifier
);
756 return key
== void_type
;
761 return key
== long_type
|| key
== double_type
;
764 // Return number of stack or local variable slots taken by this
768 return iswide () ? 2 : 1;
771 bool isarray () const
773 // We treat null_type as not an array. This is ok based on the
774 // current uses of this method.
775 if (key
== reference_type
)
776 return klass
->isarray ();
782 return key
== null_type
;
785 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
787 if (key
!= reference_type
)
789 return klass
->isinterface (verifier
);
792 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
794 if (key
!= reference_type
)
796 return klass
->isabstract (verifier
);
799 // Return the element type of an array.
800 type
element_type (_Jv_BytecodeVerifier
*verifier
)
802 if (key
!= reference_type
)
803 verifier
->verify_fail ("programmer error in type::element_type()", -1);
805 jclass k
= klass
->getclass (verifier
)->getComponentType ();
806 if (k
->isPrimitive ())
807 return type (verifier
->get_type_val_for_signature (k
));
808 return type (k
, verifier
);
811 // Return the array type corresponding to an initialized
812 // reference. We could expand this to work for other kinds of
813 // types, but currently we don't need to.
814 type
to_array (_Jv_BytecodeVerifier
*verifier
)
816 if (key
!= reference_type
)
817 verifier
->verify_fail ("internal error in type::to_array()");
819 jclass k
= klass
->getclass (verifier
);
820 return type (_Jv_GetArrayClass (k
, k
->getClassLoaderInternal()),
824 bool isreference () const
826 return key
>= reference_type
;
834 bool isinitialized () const
836 return key
== reference_type
|| key
== null_type
;
839 bool isresolved () const
841 return (key
== reference_type
843 || key
== uninitialized_reference_type
);
846 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
848 // The way this is written, we don't need to check isarray().
849 if (key
!= reference_type
)
850 verifier
->verify_fail ("internal error in verify_dimensions:"
851 " not a reference type");
853 if (klass
->count_dimensions () < ndims
)
854 verifier
->verify_fail ("array type has fewer dimensions"
858 // Merge OLD_TYPE into this. On error throw exception. Return
859 // true if the merge caused a type change.
860 bool merge (type
& old_type
, bool local_semantics
,
861 _Jv_BytecodeVerifier
*verifier
)
863 bool changed
= false;
864 bool refo
= old_type
.isreference ();
865 bool refn
= isreference ();
868 if (old_type
.key
== null_type
)
870 else if (key
== null_type
)
875 else if (isinitialized () != old_type
.isinitialized ())
876 verifier
->verify_fail ("merging initialized and uninitialized types");
879 if (! isinitialized ())
883 else if (old_type
.pc
== UNINIT
)
885 else if (pc
!= old_type
.pc
)
886 verifier
->verify_fail ("merging different uninitialized types");
889 ref_intersection
*merged
= old_type
.klass
->merge (klass
,
898 else if (refo
|| refn
|| key
!= old_type
.key
)
902 // If we already have an `unsuitable' type, then we
903 // don't need to change again.
904 if (key
!= unsuitable_type
)
906 key
= unsuitable_type
;
911 verifier
->verify_fail ("unmergeable type");
917 void print (void) const
922 case boolean_type
: c
= 'Z'; break;
923 case byte_type
: c
= 'B'; break;
924 case char_type
: c
= 'C'; break;
925 case short_type
: c
= 'S'; break;
926 case int_type
: c
= 'I'; break;
927 case long_type
: c
= 'J'; break;
928 case float_type
: c
= 'F'; break;
929 case double_type
: c
= 'D'; break;
930 case void_type
: c
= 'V'; break;
931 case unsuitable_type
: c
= '-'; break;
932 case return_address_type
: c
= 'r'; break;
933 case continuation_type
: c
= '+'; break;
934 case reference_type
: c
= 'L'; break;
935 case null_type
: c
= '@'; break;
936 case uninitialized_reference_type
: c
= 'U'; break;
938 debug_print ("%c", c
);
940 #endif /* VERIFY_DEBUG */
943 // This class holds all the state information we need for a given
947 // The current top of the stack, in terms of slots.
949 // The current depth of the stack. This will be larger than
950 // STACKTOP when wide types are on the stack.
954 // The local variables.
956 // We keep track of the type of `this' specially. This is used to
957 // ensure that an instance initializer invokes another initializer
958 // on `this' before returning. We must keep track of this
959 // specially because otherwise we might be confused by code which
960 // assigns to locals[0] (overwriting `this') and then returns
961 // without really initializing.
964 // The PC for this state. This is only valid on states which are
965 // permanently attached to a given PC. For an object like
966 // `current_state', which is used transiently, this has no
969 // We keep a linked list of all states requiring reverification.
970 // If this is the special value INVALID_STATE then this state is
971 // not on the list. NULL marks the end of the linked list.
974 // NO_NEXT is the PC value meaning that a new state must be
975 // acquired from the verification list.
976 static const int NO_NEXT
= -1;
983 next
= INVALID_STATE
;
986 state (int max_stack
, int max_locals
)
991 stack
= new type
[max_stack
];
992 for (int i
= 0; i
< max_stack
; ++i
)
993 stack
[i
] = unsuitable_type
;
994 locals
= new type
[max_locals
];
995 for (int i
= 0; i
< max_locals
; ++i
)
996 locals
[i
] = unsuitable_type
;
998 next
= INVALID_STATE
;
1001 state (const state
*orig
, int max_stack
, int max_locals
)
1003 stack
= new type
[max_stack
];
1004 locals
= new type
[max_locals
];
1005 copy (orig
, max_stack
, max_locals
);
1007 next
= INVALID_STATE
;
1018 void *operator new[] (size_t bytes
)
1020 return _Jv_Malloc (bytes
);
1023 void operator delete[] (void *mem
)
1028 void *operator new (size_t bytes
)
1030 return _Jv_Malloc (bytes
);
1033 void operator delete (void *mem
)
1038 void copy (const state
*copy
, int max_stack
, int max_locals
)
1040 stacktop
= copy
->stacktop
;
1041 stackdepth
= copy
->stackdepth
;
1042 for (int i
= 0; i
< max_stack
; ++i
)
1043 stack
[i
] = copy
->stack
[i
];
1044 for (int i
= 0; i
< max_locals
; ++i
)
1045 locals
[i
] = copy
->locals
[i
];
1047 this_type
= copy
->this_type
;
1048 // Don't modify `next' or `pc'.
1051 // Modify this state to reflect entry to an exception handler.
1052 void set_exception (type t
, int max_stack
)
1057 for (int i
= stacktop
; i
< max_stack
; ++i
)
1058 stack
[i
] = unsuitable_type
;
1061 inline int get_pc () const
1066 void set_pc (int npc
)
1071 // Merge STATE_OLD into this state. Destructively modifies this
1072 // state. Returns true if the new state was in fact changed.
1073 // Will throw an exception if the states are not mergeable.
1074 bool merge (state
*state_old
, int max_locals
,
1075 _Jv_BytecodeVerifier
*verifier
)
1077 bool changed
= false;
1079 // Special handling for `this'. If one or the other is
1080 // uninitialized, then the merge is uninitialized.
1081 if (this_type
.isinitialized ())
1082 this_type
= state_old
->this_type
;
1085 if (state_old
->stacktop
!= stacktop
) // FIXME stackdepth instead?
1086 verifier
->verify_fail ("stack sizes differ");
1087 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
1089 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
1093 // Merge local variables.
1094 for (int i
= 0; i
< max_locals
; ++i
)
1096 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
1103 // Ensure that `this' has been initialized.
1104 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1106 if (this_type
.isreference () && ! this_type
.isinitialized ())
1107 verifier
->verify_fail ("`this' is uninitialized");
1110 // Set type of `this'.
1111 void set_this_type (const type
&k
)
1116 // Mark each `new'd object we know of that was allocated at PC as
1118 void set_initialized (int pc
, int max_locals
)
1120 for (int i
= 0; i
< stacktop
; ++i
)
1121 stack
[i
].set_initialized (pc
);
1122 for (int i
= 0; i
< max_locals
; ++i
)
1123 locals
[i
].set_initialized (pc
);
1124 this_type
.set_initialized (pc
);
1127 // This tests to see whether two states can be considered "merge
1128 // compatible". If both states have a return-address in the same
1129 // slot, and the return addresses are different, then they are not
1130 // compatible and we must not try to merge them.
1131 bool state_mergeable_p (state
*other
, int max_locals
,
1132 _Jv_BytecodeVerifier
*verifier
)
1134 // This is tricky: if the stack sizes differ, then not only are
1135 // these not mergeable, but in fact we should give an error, as
1136 // we've found two execution paths that reach a branch target
1137 // with different stack depths. FIXME stackdepth instead?
1138 if (stacktop
!= other
->stacktop
)
1139 verifier
->verify_fail ("stack sizes differ");
1141 for (int i
= 0; i
< stacktop
; ++i
)
1142 if (! stack
[i
].state_mergeable_p (other
->stack
[i
]))
1144 for (int i
= 0; i
< max_locals
; ++i
)
1145 if (! locals
[i
].state_mergeable_p (other
->locals
[i
]))
1150 void reverify (_Jv_BytecodeVerifier
*verifier
)
1152 if (next
== INVALID_STATE
)
1154 next
= verifier
->next_verify_state
;
1155 verifier
->next_verify_state
= this;
1160 void print (const char *leader
, int pc
,
1161 int max_stack
, int max_locals
) const
1163 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1165 for (i
= 0; i
< stacktop
; ++i
)
1167 for (; i
< max_stack
; ++i
)
1169 debug_print (" [local] ");
1170 for (i
= 0; i
< max_locals
; ++i
)
1172 debug_print (" | %p\n", this);
1175 inline void print (const char *, int, int, int) const
1178 #endif /* VERIFY_DEBUG */
1183 if (current_state
->stacktop
<= 0)
1184 verify_fail ("stack empty");
1185 type r
= current_state
->stack
[--current_state
->stacktop
];
1186 current_state
->stackdepth
-= r
.depth ();
1187 if (current_state
->stackdepth
< 0)
1188 verify_fail ("stack empty", start_PC
);
1194 type r
= pop_raw ();
1196 verify_fail ("narrow pop of wide type");
1200 type
pop_type (type match
)
1203 type t
= pop_raw ();
1204 if (! match
.compatible (t
, this))
1205 verify_fail ("incompatible type on stack");
1209 // Pop a reference which is guaranteed to be initialized. MATCH
1210 // doesn't have to be a reference type; in this case this acts like
1212 type
pop_init_ref (type match
)
1214 type t
= pop_raw ();
1215 if (t
.isreference () && ! t
.isinitialized ())
1216 verify_fail ("initialized reference required");
1217 else if (! match
.compatible (t
, this))
1218 verify_fail ("incompatible type on stack");
1222 // Pop a reference type or a return address.
1223 type
pop_ref_or_return ()
1225 type t
= pop_raw ();
1226 if (! t
.isreference () && t
.key
!= return_address_type
)
1227 verify_fail ("expected reference or return address on stack");
1231 void push_type (type t
)
1233 // If T is a numeric type like short, promote it to int.
1236 int depth
= t
.depth ();
1237 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1238 verify_fail ("stack overflow");
1239 current_state
->stack
[current_state
->stacktop
++] = t
;
1240 current_state
->stackdepth
+= depth
;
1243 void set_variable (int index
, type t
)
1245 // If T is a numeric type like short, promote it to int.
1248 int depth
= t
.depth ();
1249 if (index
> current_method
->max_locals
- depth
)
1250 verify_fail ("invalid local variable");
1251 current_state
->locals
[index
] = t
;
1254 current_state
->locals
[index
+ 1] = continuation_type
;
1255 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1256 current_state
->locals
[index
- 1] = unsuitable_type
;
1259 type
get_variable (int index
, type t
)
1261 int depth
= t
.depth ();
1262 if (index
> current_method
->max_locals
- depth
)
1263 verify_fail ("invalid local variable");
1264 if (! t
.compatible (current_state
->locals
[index
], this))
1265 verify_fail ("incompatible type in local variable");
1268 type
t (continuation_type
);
1269 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1270 verify_fail ("invalid local variable");
1272 return current_state
->locals
[index
];
1275 // Make sure ARRAY is an array type and that its elements are
1276 // compatible with type ELEMENT. Returns the actual element type.
1277 type
require_array_type (type array
, type element
)
1279 // An odd case. Here we just pretend that everything went ok. If
1280 // the requested element type is some kind of reference, return
1281 // the null type instead.
1282 if (array
.isnull ())
1283 return element
.isreference () ? type (null_type
) : element
;
1285 if (! array
.isarray ())
1286 verify_fail ("array required");
1288 type t
= array
.element_type (this);
1289 if (! element
.compatible (t
, this))
1291 // Special case for byte arrays, which must also be boolean
1294 if (element
.key
== byte_type
)
1296 type
e2 (boolean_type
);
1297 ok
= e2
.compatible (t
, this);
1300 verify_fail ("incompatible array element type");
1303 // Return T and not ELEMENT, because T might be specialized.
1309 if (PC
>= current_method
->code_length
)
1310 verify_fail ("premature end of bytecode");
1311 return (jint
) bytecode
[PC
++] & 0xff;
1316 jint b1
= get_byte ();
1317 jint b2
= get_byte ();
1318 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1323 jint b1
= get_byte ();
1324 jint b2
= get_byte ();
1325 jshort s
= (b1
<< 8) | b2
;
1331 jint b1
= get_byte ();
1332 jint b2
= get_byte ();
1333 jint b3
= get_byte ();
1334 jint b4
= get_byte ();
1335 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1338 int compute_jump (int offset
)
1340 int npc
= start_PC
+ offset
;
1341 if (npc
< 0 || npc
>= current_method
->code_length
)
1342 verify_fail ("branch out of range", start_PC
);
1346 // Add a new state to the state list at NPC.
1347 state
*add_new_state (int npc
, state
*old_state
)
1349 state
*new_state
= new state (old_state
, current_method
->max_stack
,
1350 current_method
->max_locals
);
1351 debug_print ("== New state in add_new_state\n");
1352 new_state
->print ("New", npc
, current_method
->max_stack
,
1353 current_method
->max_locals
);
1354 linked
<state
> *nlink
1355 = (linked
<state
> *) _Jv_Malloc (sizeof (linked
<state
>));
1356 nlink
->val
= new_state
;
1357 nlink
->next
= states
[npc
];
1358 states
[npc
] = nlink
;
1359 new_state
->set_pc (npc
);
1363 // Merge the indicated state into the state at the branch target and
1364 // schedule a new PC if there is a change. NPC is the PC of the
1365 // branch target, and FROM_STATE is the state at the source of the
1366 // branch. This method returns true if the destination state
1367 // changed and requires reverification, false otherwise.
1368 void merge_into (int npc
, state
*from_state
)
1370 // Iterate over all target states and merge our state into each,
1371 // if applicable. FIXME one improvement we could make here is
1372 // "state destruction". Merging a new state into an existing one
1373 // might cause a return_address_type to be merged to
1374 // unsuitable_type. In this case the resulting state may now be
1375 // mergeable with other states currently held in parallel at this
1376 // location. So in this situation we could pairwise compare and
1377 // reduce the number of parallel states.
1378 bool applicable
= false;
1379 for (linked
<state
> *iter
= states
[npc
]; iter
!= NULL
; iter
= iter
->next
)
1381 state
*new_state
= iter
->val
;
1382 if (new_state
->state_mergeable_p (from_state
,
1383 current_method
->max_locals
, this))
1387 debug_print ("== Merge states in merge_into\n");
1388 from_state
->print ("Frm", start_PC
, current_method
->max_stack
,
1389 current_method
->max_locals
);
1390 new_state
->print (" To", npc
, current_method
->max_stack
,
1391 current_method
->max_locals
);
1392 bool changed
= new_state
->merge (from_state
,
1393 current_method
->max_locals
,
1395 new_state
->print ("New", npc
, current_method
->max_stack
,
1396 current_method
->max_locals
);
1399 new_state
->reverify (this);
1405 // Either we don't yet have a state at NPC, or we have a
1406 // return-address type that is in conflict with all existing
1407 // state. So, we need to create a new entry.
1408 state
*new_state
= add_new_state (npc
, from_state
);
1409 // A new state added in this way must always be reverified.
1410 new_state
->reverify (this);
1414 void push_jump (int offset
)
1416 int npc
= compute_jump (offset
);
1417 // According to the JVM Spec, we need to check for uninitialized
1418 // objects here. However, this does not actually affect type
1419 // safety, and the Eclipse java compiler generates code that
1420 // violates this constraint.
1421 merge_into (npc
, current_state
);
1424 void push_exception_jump (type t
, int pc
)
1426 // According to the JVM Spec, we need to check for uninitialized
1427 // objects here. However, this does not actually affect type
1428 // safety, and the Eclipse java compiler generates code that
1429 // violates this constraint.
1430 state
s (current_state
, current_method
->max_stack
,
1431 current_method
->max_locals
);
1432 if (current_method
->max_stack
< 1)
1433 verify_fail ("stack overflow at exception handler");
1434 s
.set_exception (t
, current_method
->max_stack
);
1435 merge_into (pc
, &s
);
1440 state
*new_state
= next_verify_state
;
1441 if (new_state
== INVALID_STATE
)
1442 verify_fail ("programmer error in pop_jump");
1443 if (new_state
!= NULL
)
1445 next_verify_state
= new_state
->next
;
1446 new_state
->next
= INVALID_STATE
;
1451 void invalidate_pc ()
1453 PC
= state::NO_NEXT
;
1456 void note_branch_target (int pc
)
1458 // Don't check `pc <= PC', because we've advanced PC after
1459 // fetching the target and we haven't yet checked the next
1461 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1462 verify_fail ("branch not to instruction start", start_PC
);
1463 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1466 void skip_padding ()
1468 while ((PC
% 4) > 0)
1469 if (get_byte () != 0)
1470 verify_fail ("found nonzero padding byte");
1473 // Do the work for a `ret' instruction. INDEX is the index into the
1475 void handle_ret_insn (int index
)
1477 type ret_addr
= get_variable (index
, return_address_type
);
1478 // It would be nice if we could do this. However, the JVM Spec
1479 // doesn't say that this is what happens. It is implied that
1480 // reusing a return address is invalid, but there's no actual
1481 // prohibition against it.
1482 // set_variable (index, unsuitable_type);
1484 int npc
= ret_addr
.get_pc ();
1485 // We might be returning to a `jsr' that is at the end of the
1486 // bytecode. This is ok if we never return from the called
1487 // subroutine, but if we see this here it is an error.
1488 if (npc
>= current_method
->code_length
)
1489 verify_fail ("fell off end");
1491 // According to the JVM Spec, we need to check for uninitialized
1492 // objects here. However, this does not actually affect type
1493 // safety, and the Eclipse java compiler generates code that
1494 // violates this constraint.
1495 merge_into (npc
, current_state
);
1499 void handle_jsr_insn (int offset
)
1501 int npc
= compute_jump (offset
);
1503 // According to the JVM Spec, we need to check for uninitialized
1504 // objects here. However, this does not actually affect type
1505 // safety, and the Eclipse java compiler generates code that
1506 // violates this constraint.
1508 // Modify our state as appropriate for entry into a subroutine.
1509 type
ret_addr (return_address_type
);
1510 ret_addr
.set_return_address (PC
);
1511 push_type (ret_addr
);
1512 merge_into (npc
, current_state
);
1516 jclass
construct_primitive_array_type (type_val prim
)
1522 k
= JvPrimClass (boolean
);
1525 k
= JvPrimClass (char);
1528 k
= JvPrimClass (float);
1531 k
= JvPrimClass (double);
1534 k
= JvPrimClass (byte
);
1537 k
= JvPrimClass (short);
1540 k
= JvPrimClass (int);
1543 k
= JvPrimClass (long);
1546 // These aren't used here but we call them out to avoid
1549 case unsuitable_type
:
1550 case return_address_type
:
1551 case continuation_type
:
1552 case reference_type
:
1554 case uninitialized_reference_type
:
1556 verify_fail ("unknown type in construct_primitive_array_type");
1558 k
= _Jv_GetArrayClass (k
, NULL
);
1562 // This pass computes the location of branch targets and also
1563 // instruction starts.
1564 void branch_prepass ()
1566 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1568 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1572 while (PC
< current_method
->code_length
)
1574 // Set `start_PC' early so that error checking can have the
1577 flags
[PC
] |= FLAG_INSN_START
;
1579 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1583 case op_aconst_null
:
1719 case op_monitorenter
:
1720 case op_monitorexit
:
1728 case op_arraylength
:
1760 case op_invokespecial
:
1761 case op_invokestatic
:
1762 case op_invokevirtual
:
1766 case op_multianewarray
:
1789 note_branch_target (compute_jump (get_short ()));
1792 case op_tableswitch
:
1795 note_branch_target (compute_jump (get_int ()));
1796 jint low
= get_int ();
1797 jint hi
= get_int ();
1799 verify_fail ("invalid tableswitch", start_PC
);
1800 for (int i
= low
; i
<= hi
; ++i
)
1801 note_branch_target (compute_jump (get_int ()));
1805 case op_lookupswitch
:
1808 note_branch_target (compute_jump (get_int ()));
1809 int npairs
= get_int ();
1811 verify_fail ("too few pairs in lookupswitch", start_PC
);
1812 while (npairs
-- > 0)
1815 note_branch_target (compute_jump (get_int ()));
1820 case op_invokeinterface
:
1828 opcode
= (java_opcode
) get_byte ();
1830 if (opcode
== op_iinc
)
1837 note_branch_target (compute_jump (get_int ()));
1840 // These are unused here, but we call them out explicitly
1841 // so that -Wswitch-enum doesn't complain.
1847 case op_putstatic_1
:
1848 case op_putstatic_2
:
1849 case op_putstatic_4
:
1850 case op_putstatic_8
:
1851 case op_putstatic_a
:
1853 case op_getfield_2s
:
1854 case op_getfield_2u
:
1858 case op_getstatic_1
:
1859 case op_getstatic_2s
:
1860 case op_getstatic_2u
:
1861 case op_getstatic_4
:
1862 case op_getstatic_8
:
1863 case op_getstatic_a
:
1865 verify_fail ("unrecognized instruction in branch_prepass",
1869 // See if any previous branch tried to branch to the middle of
1870 // this instruction.
1871 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1873 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1874 verify_fail ("branch to middle of instruction", pc
);
1878 // Verify exception handlers.
1879 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1881 if (! (flags
[exception
[i
].handler_pc
.i
] & FLAG_INSN_START
))
1882 verify_fail ("exception handler not at instruction start",
1883 exception
[i
].handler_pc
.i
);
1884 if (! (flags
[exception
[i
].start_pc
.i
] & FLAG_INSN_START
))
1885 verify_fail ("exception start not at instruction start",
1886 exception
[i
].start_pc
.i
);
1887 if (exception
[i
].end_pc
.i
!= current_method
->code_length
1888 && ! (flags
[exception
[i
].end_pc
.i
] & FLAG_INSN_START
))
1889 verify_fail ("exception end not at instruction start",
1890 exception
[i
].end_pc
.i
);
1892 flags
[exception
[i
].handler_pc
.i
] |= FLAG_BRANCH_TARGET
;
1896 void check_pool_index (int index
)
1898 if (index
< 0 || index
>= current_class
->constants
.size
)
1899 verify_fail ("constant pool index out of range", start_PC
);
1902 type
check_class_constant (int index
)
1904 check_pool_index (index
);
1905 _Jv_Constants
*pool
= ¤t_class
->constants
;
1906 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1907 return type (pool
->data
[index
].clazz
, this);
1908 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1909 return type (pool
->data
[index
].utf8
, this);
1910 verify_fail ("expected class constant", start_PC
);
1913 type
check_constant (int index
)
1915 check_pool_index (index
);
1916 _Jv_Constants
*pool
= ¤t_class
->constants
;
1917 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1918 || pool
->tags
[index
] == JV_CONSTANT_String
)
1919 return type (&java::lang::String::class$
, this);
1920 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1921 return type (int_type
);
1922 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1923 return type (float_type
);
1924 verify_fail ("String, int, or float constant expected", start_PC
);
1927 type
check_wide_constant (int index
)
1929 check_pool_index (index
);
1930 _Jv_Constants
*pool
= ¤t_class
->constants
;
1931 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1932 return type (long_type
);
1933 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1934 return type (double_type
);
1935 verify_fail ("long or double constant expected", start_PC
);
1938 // Helper for both field and method. These are laid out the same in
1939 // the constant pool.
1940 type
handle_field_or_method (int index
, int expected
,
1941 _Jv_Utf8Const
**name
,
1942 _Jv_Utf8Const
**fmtype
)
1944 check_pool_index (index
);
1945 _Jv_Constants
*pool
= ¤t_class
->constants
;
1946 if (pool
->tags
[index
] != expected
)
1947 verify_fail ("didn't see expected constant", start_PC
);
1948 // Once we know we have a Fieldref or Methodref we assume that it
1949 // is correctly laid out in the constant pool. I think the code
1950 // in defineclass.cc guarantees this.
1951 _Jv_ushort class_index
, name_and_type_index
;
1952 _Jv_loadIndexes (&pool
->data
[index
],
1954 name_and_type_index
);
1955 _Jv_ushort name_index
, desc_index
;
1956 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1957 name_index
, desc_index
);
1959 *name
= pool
->data
[name_index
].utf8
;
1960 *fmtype
= pool
->data
[desc_index
].utf8
;
1962 return check_class_constant (class_index
);
1965 // Return field's type, compute class' type if requested.
1966 type
check_field_constant (int index
, type
*class_type
= NULL
)
1968 _Jv_Utf8Const
*name
, *field_type
;
1969 type ct
= handle_field_or_method (index
,
1970 JV_CONSTANT_Fieldref
,
1971 &name
, &field_type
);
1974 if (field_type
->first() == '[' || field_type
->first() == 'L')
1975 return type (field_type
, this);
1976 return get_type_val_for_signature (field_type
->first());
1979 type
check_method_constant (int index
, bool is_interface
,
1980 _Jv_Utf8Const
**method_name
,
1981 _Jv_Utf8Const
**method_signature
)
1983 return handle_field_or_method (index
,
1985 ? JV_CONSTANT_InterfaceMethodref
1986 : JV_CONSTANT_Methodref
),
1987 method_name
, method_signature
);
1990 type
get_one_type (char *&p
)
2008 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
2009 return type (name
, this);
2012 // Casting to jchar here is ok since we are looking at an ASCII
2014 type_val rt
= get_type_val_for_signature (jchar (v
));
2016 if (arraycount
== 0)
2018 // Callers of this function eventually push their arguments on
2019 // the stack. So, promote them here.
2020 return type (rt
).promote ();
2023 jclass k
= construct_primitive_array_type (rt
);
2024 while (--arraycount
> 0)
2025 k
= _Jv_GetArrayClass (k
, NULL
);
2026 return type (k
, this);
2029 void compute_argument_types (_Jv_Utf8Const
*signature
,
2032 char *p
= signature
->chars();
2039 types
[i
++] = get_one_type (p
);
2042 type
compute_return_type (_Jv_Utf8Const
*signature
)
2044 char *p
= signature
->chars();
2048 return get_one_type (p
);
2051 void check_return_type (type onstack
)
2053 type rt
= compute_return_type (current_method
->self
->signature
);
2054 if (! rt
.compatible (onstack
, this))
2055 verify_fail ("incompatible return type");
2058 // Initialize the stack for the new method. Returns true if this
2059 // method is an instance initializer.
2060 bool initialize_stack ()
2063 bool is_init
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2065 bool is_clinit
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2068 using namespace java::lang::reflect
;
2069 if (! Modifier::isStatic (current_method
->self
->accflags
))
2071 type
kurr (current_class
, this);
2074 kurr
.set_uninitialized (type::SELF
, this);
2078 verify_fail ("<clinit> method must be static");
2079 set_variable (0, kurr
);
2080 current_state
->set_this_type (kurr
);
2086 verify_fail ("<init> method must be non-static");
2089 // We have to handle wide arguments specially here.
2090 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2091 type arg_types
[arg_count
];
2092 compute_argument_types (current_method
->self
->signature
, arg_types
);
2093 for (int i
= 0; i
< arg_count
; ++i
)
2095 set_variable (var
, arg_types
[i
]);
2097 if (arg_types
[i
].iswide ())
2104 void verify_instructions_0 ()
2106 current_state
= new state (current_method
->max_stack
,
2107 current_method
->max_locals
);
2112 // True if we are verifying an instance initializer.
2113 bool this_is_init
= initialize_stack ();
2115 states
= (linked
<state
> **) _Jv_Malloc (sizeof (linked
<state
> *)
2116 * current_method
->code_length
);
2117 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2120 next_verify_state
= NULL
;
2124 // If the PC was invalidated, get a new one from the work list.
2125 if (PC
== state::NO_NEXT
)
2127 state
*new_state
= pop_jump ();
2128 // If it is null, we're done.
2129 if (new_state
== NULL
)
2132 PC
= new_state
->get_pc ();
2133 debug_print ("== State pop from pending list\n");
2134 // Set up the current state.
2135 current_state
->copy (new_state
, current_method
->max_stack
,
2136 current_method
->max_locals
);
2140 // We only have to do this checking in the situation where
2141 // control flow falls through from the previous
2142 // instruction. Otherwise merging is done at the time we
2144 if (states
[PC
] != NULL
)
2146 // We've already visited this instruction. So merge
2147 // the states together. It is simplest, but not most
2148 // efficient, to just always invalidate the PC here.
2149 merge_into (PC
, current_state
);
2155 // Control can't fall off the end of the bytecode. We need to
2156 // check this in both cases, not just the fall-through case,
2157 // because we don't check to see whether a `jsr' appears at
2158 // the end of the bytecode until we process a `ret'.
2159 if (PC
>= current_method
->code_length
)
2160 verify_fail ("fell off end");
2162 // We only have to keep saved state at branch targets. If
2163 // we're at a branch target and the state here hasn't been set
2164 // yet, we set it now. You might notice that `ret' targets
2165 // won't necessarily have FLAG_BRANCH_TARGET set. This
2166 // doesn't matter, since those states will be filled in by
2168 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2169 add_new_state (PC
, current_state
);
2171 // Set this before handling exceptions so that debug output is
2175 // Update states for all active exception handlers. Ordinarily
2176 // there are not many exception handlers. So we simply run
2177 // through them all.
2178 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2180 if (PC
>= exception
[i
].start_pc
.i
&& PC
< exception
[i
].end_pc
.i
)
2182 type
handler (&java::lang::Throwable::class$
, this);
2183 if (exception
[i
].handler_type
.i
!= 0)
2184 handler
= check_class_constant (exception
[i
].handler_type
.i
);
2185 push_exception_jump (handler
, exception
[i
].handler_pc
.i
);
2189 current_state
->print (" ", PC
, current_method
->max_stack
,
2190 current_method
->max_locals
);
2191 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2197 case op_aconst_null
:
2198 push_type (null_type
);
2208 push_type (int_type
);
2213 push_type (long_type
);
2219 push_type (float_type
);
2224 push_type (double_type
);
2229 push_type (int_type
);
2234 push_type (int_type
);
2238 push_type (check_constant (get_byte ()));
2241 push_type (check_constant (get_ushort ()));
2244 push_type (check_wide_constant (get_ushort ()));
2248 push_type (get_variable (get_byte (), int_type
));
2251 push_type (get_variable (get_byte (), long_type
));
2254 push_type (get_variable (get_byte (), float_type
));
2257 push_type (get_variable (get_byte (), double_type
));
2260 push_type (get_variable (get_byte (), reference_type
));
2267 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2273 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2279 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2285 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2291 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2294 pop_type (int_type
);
2295 push_type (require_array_type (pop_init_ref (reference_type
),
2299 pop_type (int_type
);
2300 push_type (require_array_type (pop_init_ref (reference_type
),
2304 pop_type (int_type
);
2305 push_type (require_array_type (pop_init_ref (reference_type
),
2309 pop_type (int_type
);
2310 push_type (require_array_type (pop_init_ref (reference_type
),
2314 pop_type (int_type
);
2315 push_type (require_array_type (pop_init_ref (reference_type
),
2319 pop_type (int_type
);
2320 require_array_type (pop_init_ref (reference_type
), byte_type
);
2321 push_type (int_type
);
2324 pop_type (int_type
);
2325 require_array_type (pop_init_ref (reference_type
), char_type
);
2326 push_type (int_type
);
2329 pop_type (int_type
);
2330 require_array_type (pop_init_ref (reference_type
), short_type
);
2331 push_type (int_type
);
2334 set_variable (get_byte (), pop_type (int_type
));
2337 set_variable (get_byte (), pop_type (long_type
));
2340 set_variable (get_byte (), pop_type (float_type
));
2343 set_variable (get_byte (), pop_type (double_type
));
2346 set_variable (get_byte (), pop_ref_or_return ());
2352 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2358 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2364 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2370 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2376 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2379 pop_type (int_type
);
2380 pop_type (int_type
);
2381 require_array_type (pop_init_ref (reference_type
), int_type
);
2384 pop_type (long_type
);
2385 pop_type (int_type
);
2386 require_array_type (pop_init_ref (reference_type
), long_type
);
2389 pop_type (float_type
);
2390 pop_type (int_type
);
2391 require_array_type (pop_init_ref (reference_type
), float_type
);
2394 pop_type (double_type
);
2395 pop_type (int_type
);
2396 require_array_type (pop_init_ref (reference_type
), double_type
);
2399 pop_type (reference_type
);
2400 pop_type (int_type
);
2401 require_array_type (pop_init_ref (reference_type
), reference_type
);
2404 pop_type (int_type
);
2405 pop_type (int_type
);
2406 require_array_type (pop_init_ref (reference_type
), byte_type
);
2409 pop_type (int_type
);
2410 pop_type (int_type
);
2411 require_array_type (pop_init_ref (reference_type
), char_type
);
2414 pop_type (int_type
);
2415 pop_type (int_type
);
2416 require_array_type (pop_init_ref (reference_type
), short_type
);
2423 type t
= pop_raw ();
2447 type t2
= pop_raw ();
2462 type t
= pop_raw ();
2477 type t1
= pop_raw ();
2494 type t1
= pop_raw ();
2497 type t2
= pop_raw ();
2515 type t3
= pop_raw ();
2553 pop_type (int_type
);
2554 push_type (pop_type (int_type
));
2564 pop_type (long_type
);
2565 push_type (pop_type (long_type
));
2570 pop_type (int_type
);
2571 push_type (pop_type (long_type
));
2578 pop_type (float_type
);
2579 push_type (pop_type (float_type
));
2586 pop_type (double_type
);
2587 push_type (pop_type (double_type
));
2593 push_type (pop_type (int_type
));
2596 push_type (pop_type (long_type
));
2599 push_type (pop_type (float_type
));
2602 push_type (pop_type (double_type
));
2605 get_variable (get_byte (), int_type
);
2609 pop_type (int_type
);
2610 push_type (long_type
);
2613 pop_type (int_type
);
2614 push_type (float_type
);
2617 pop_type (int_type
);
2618 push_type (double_type
);
2621 pop_type (long_type
);
2622 push_type (int_type
);
2625 pop_type (long_type
);
2626 push_type (float_type
);
2629 pop_type (long_type
);
2630 push_type (double_type
);
2633 pop_type (float_type
);
2634 push_type (int_type
);
2637 pop_type (float_type
);
2638 push_type (long_type
);
2641 pop_type (float_type
);
2642 push_type (double_type
);
2645 pop_type (double_type
);
2646 push_type (int_type
);
2649 pop_type (double_type
);
2650 push_type (long_type
);
2653 pop_type (double_type
);
2654 push_type (float_type
);
2657 pop_type (long_type
);
2658 pop_type (long_type
);
2659 push_type (int_type
);
2663 pop_type (float_type
);
2664 pop_type (float_type
);
2665 push_type (int_type
);
2669 pop_type (double_type
);
2670 pop_type (double_type
);
2671 push_type (int_type
);
2679 pop_type (int_type
);
2680 push_jump (get_short ());
2688 pop_type (int_type
);
2689 pop_type (int_type
);
2690 push_jump (get_short ());
2694 pop_type (reference_type
);
2695 pop_type (reference_type
);
2696 push_jump (get_short ());
2699 push_jump (get_short ());
2703 handle_jsr_insn (get_short ());
2706 handle_ret_insn (get_byte ());
2708 case op_tableswitch
:
2710 pop_type (int_type
);
2712 push_jump (get_int ());
2713 jint low
= get_int ();
2714 jint high
= get_int ();
2715 // Already checked LOW -vs- HIGH.
2716 for (int i
= low
; i
<= high
; ++i
)
2717 push_jump (get_int ());
2722 case op_lookupswitch
:
2724 pop_type (int_type
);
2726 push_jump (get_int ());
2727 jint npairs
= get_int ();
2728 // Already checked NPAIRS >= 0.
2730 for (int i
= 0; i
< npairs
; ++i
)
2732 jint key
= get_int ();
2733 if (i
> 0 && key
<= lastkey
)
2734 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2736 push_jump (get_int ());
2742 check_return_type (pop_type (int_type
));
2746 check_return_type (pop_type (long_type
));
2750 check_return_type (pop_type (float_type
));
2754 check_return_type (pop_type (double_type
));
2758 check_return_type (pop_init_ref (reference_type
));
2762 // We only need to check this when the return type is
2763 // void, because all instance initializers return void.
2765 current_state
->check_this_initialized (this);
2766 check_return_type (void_type
);
2770 push_type (check_field_constant (get_ushort ()));
2773 pop_type (check_field_constant (get_ushort ()));
2778 type field
= check_field_constant (get_ushort (), &klass
);
2786 type field
= check_field_constant (get_ushort (), &klass
);
2789 // We have an obscure special case here: we can use
2790 // `putfield' on a field declared in this class, even if
2791 // `this' has not yet been initialized.
2792 if (! current_state
->this_type
.isinitialized ()
2793 && current_state
->this_type
.pc
== type::SELF
)
2794 klass
.set_uninitialized (type::SELF
, this);
2799 case op_invokevirtual
:
2800 case op_invokespecial
:
2801 case op_invokestatic
:
2802 case op_invokeinterface
:
2804 _Jv_Utf8Const
*method_name
, *method_signature
;
2806 = check_method_constant (get_ushort (),
2807 opcode
== op_invokeinterface
,
2810 // NARGS is only used when we're processing
2811 // invokeinterface. It is simplest for us to compute it
2812 // here and then verify it later.
2814 if (opcode
== op_invokeinterface
)
2816 nargs
= get_byte ();
2817 if (get_byte () != 0)
2818 verify_fail ("invokeinterface dummy byte is wrong");
2821 bool is_init
= false;
2822 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2825 if (opcode
!= op_invokespecial
)
2826 verify_fail ("can't invoke <init>");
2828 else if (method_name
->first() == '<')
2829 verify_fail ("can't invoke method starting with `<'");
2831 // Pop arguments and check types.
2832 int arg_count
= _Jv_count_arguments (method_signature
);
2833 type arg_types
[arg_count
];
2834 compute_argument_types (method_signature
, arg_types
);
2835 for (int i
= arg_count
- 1; i
>= 0; --i
)
2837 // This is only used for verifying the byte for
2839 nargs
-= arg_types
[i
].depth ();
2840 pop_init_ref (arg_types
[i
]);
2843 if (opcode
== op_invokeinterface
2845 verify_fail ("wrong argument count for invokeinterface");
2847 if (opcode
!= op_invokestatic
)
2849 type t
= class_type
;
2852 // In this case the PC doesn't matter.
2853 t
.set_uninitialized (type::UNINIT
, this);
2854 // FIXME: check to make sure that the <init>
2855 // call is to the right class.
2856 // It must either be super or an exact class
2859 type raw
= pop_raw ();
2860 if (! t
.compatible (raw
, this))
2861 verify_fail ("incompatible type on stack");
2864 current_state
->set_initialized (raw
.get_pc (),
2865 current_method
->max_locals
);
2868 type rt
= compute_return_type (method_signature
);
2876 type t
= check_class_constant (get_ushort ());
2877 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2878 verify_fail ("type is array, interface, or abstract");
2879 t
.set_uninitialized (start_PC
, this);
2886 int atype
= get_byte ();
2887 // We intentionally have chosen constants to make this
2889 if (atype
< boolean_type
|| atype
> long_type
)
2890 verify_fail ("type not primitive", start_PC
);
2891 pop_type (int_type
);
2892 type
t (construct_primitive_array_type (type_val (atype
)), this);
2897 pop_type (int_type
);
2898 push_type (check_class_constant (get_ushort ()).to_array (this));
2900 case op_arraylength
:
2902 type t
= pop_init_ref (reference_type
);
2903 if (! t
.isarray () && ! t
.isnull ())
2904 verify_fail ("array type expected");
2905 push_type (int_type
);
2909 pop_type (type (&java::lang::Throwable::class$
, this));
2913 pop_init_ref (reference_type
);
2914 push_type (check_class_constant (get_ushort ()));
2917 pop_init_ref (reference_type
);
2918 check_class_constant (get_ushort ());
2919 push_type (int_type
);
2921 case op_monitorenter
:
2922 pop_init_ref (reference_type
);
2924 case op_monitorexit
:
2925 pop_init_ref (reference_type
);
2929 switch (get_byte ())
2932 push_type (get_variable (get_ushort (), int_type
));
2935 push_type (get_variable (get_ushort (), long_type
));
2938 push_type (get_variable (get_ushort (), float_type
));
2941 push_type (get_variable (get_ushort (), double_type
));
2944 push_type (get_variable (get_ushort (), reference_type
));
2947 set_variable (get_ushort (), pop_type (int_type
));
2950 set_variable (get_ushort (), pop_type (long_type
));
2953 set_variable (get_ushort (), pop_type (float_type
));
2956 set_variable (get_ushort (), pop_type (double_type
));
2959 set_variable (get_ushort (), pop_init_ref (reference_type
));
2962 handle_ret_insn (get_short ());
2965 get_variable (get_ushort (), int_type
);
2969 verify_fail ("unrecognized wide instruction", start_PC
);
2973 case op_multianewarray
:
2975 type atype
= check_class_constant (get_ushort ());
2976 int dim
= get_byte ();
2978 verify_fail ("too few dimensions to multianewarray", start_PC
);
2979 atype
.verify_dimensions (dim
, this);
2980 for (int i
= 0; i
< dim
; ++i
)
2981 pop_type (int_type
);
2987 pop_type (reference_type
);
2988 push_jump (get_short ());
2991 push_jump (get_int ());
2995 handle_jsr_insn (get_int ());
2998 // These are unused here, but we call them out explicitly
2999 // so that -Wswitch-enum doesn't complain.
3005 case op_putstatic_1
:
3006 case op_putstatic_2
:
3007 case op_putstatic_4
:
3008 case op_putstatic_8
:
3009 case op_putstatic_a
:
3011 case op_getfield_2s
:
3012 case op_getfield_2u
:
3016 case op_getstatic_1
:
3017 case op_getstatic_2s
:
3018 case op_getstatic_2u
:
3019 case op_getstatic_4
:
3020 case op_getstatic_8
:
3021 case op_getstatic_a
:
3023 // Unrecognized opcode.
3024 verify_fail ("unrecognized instruction in verify_instructions_0",
3032 void verify_instructions ()
3035 verify_instructions_0 ();
3038 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3040 // We just print the text as utf-8. This is just for debugging
3042 debug_print ("--------------------------------\n");
3043 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->chars());
3046 bytecode
= m
->bytecode ();
3047 exception
= m
->exceptions ();
3048 current_class
= m
->defining_class
;
3056 ~_Jv_BytecodeVerifier ()
3061 while (utf8_list
!= NULL
)
3063 linked
<_Jv_Utf8Const
> *n
= utf8_list
->next
;
3064 _Jv_Free (utf8_list
);
3068 while (isect_list
!= NULL
)
3070 ref_intersection
*next
= isect_list
->alloc_next
;
3077 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3079 linked
<state
> *iter
= states
[i
];
3080 while (iter
!= NULL
)
3082 linked
<state
> *next
= iter
->next
;
3094 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3096 _Jv_BytecodeVerifier
v (meth
);
3097 v
.verify_instructions ();
3100 #endif /* INTERPRETER */