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>
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
32 #endif /* VERIFY_DEBUG */
35 // This is used to mark states which are not scheduled for
37 #define INVALID_STATE ((state *) -1)
39 static void debug_print (const char *fmt
, ...)
40 __attribute__ ((format (printf
, 1, 2)));
43 debug_print (MAYBE_UNUSED
const char *fmt
, ...)
48 vfprintf (stderr
, fmt
, ap
);
50 #endif /* VERIFY_DEBUG */
53 // This started as a fairly ordinary verifier, and for the most part
54 // it remains so. It works in the obvious way, by modeling the effect
55 // of each opcode as it is encountered. For most opcodes, this is a
56 // straightforward operation.
58 // This verifier does not do type merging. It used to, but this
59 // results in difficulty verifying some relatively simple code
60 // involving interfaces, and it pushed some verification work into the
63 // Instead of merging reference types, when we reach a point where two
64 // flows of control merge, we simply keep the union of reference types
65 // from each branch. Then, when we need to verify a fact about a
66 // reference on the stack (e.g., that it is compatible with the
67 // argument type of a method), we check to ensure that all possible
68 // types satisfy the requirement.
70 // Another area this verifier differs from the norm is in its handling
71 // of subroutines. The JVM specification has some confusing things to
72 // say about subroutines. For instance, it makes claims about not
73 // allowing subroutines to merge and it rejects recursive subroutines.
74 // For the most part these are red herrings; we used to try to follow
75 // these things but they lead to problems. For example, the notion of
76 // "being in a subroutine" is not well-defined: is an exception
77 // handler in a subroutine? If you never execute the `ret' but
78 // instead `goto 1' do you remain in the subroutine?
80 // For clarity on what is really required for type safety, read
81 // "Simple Verification Technique for Complex Java Bytecode
82 // Subroutines" by Alessandro Coglio. Among other things this paper
83 // shows that recursive subroutines are not harmful to type safety.
84 // We implement something similar to what he proposes. Note that this
85 // means that this verifier will accept code that is rejected by some
88 // For those not wanting to read the paper, the basic observation is
89 // that we can maintain split states in subroutines. We maintain one
90 // state for each calling `jsr'. In other words, we re-verify a
91 // subroutine once for each caller, using the exact types held by the
92 // callers (as opposed to the old approach of merging types and
93 // keeping a bitmap registering what did or did not change). This
94 // approach lets us continue to verify correctly even when a
95 // subroutine is exited via `goto' or `athrow' and not `ret'.
97 // In some other areas the JVM specification is (mildly) incorrect,
98 // but we still implement what is specified. For instance, you cannot
99 // violate type safety by allocating an object with `new' and then
100 // failing to initialize it, no matter how one branches or where one
101 // stores the uninitialized reference. See "Improving the official
102 // specification of Java bytecode verification" by Alessandro Coglio.
103 // Similarly, there's no real point in enforcing that padding bytes or
104 // the mystery byte of invokeinterface must be 0, but we do that too.
106 // The verifier is currently neither completely lazy nor eager when it
107 // comes to loading classes. It tries to represent types by name when
108 // possible, and then loads them when it needs to verify a fact about
109 // the type. Checking types by name is valid because we only use
110 // names which come from the current class' constant pool. Since all
111 // such names are looked up using the same class loader, there is no
112 // danger that we might be fooled into comparing different types with
115 // In the future we plan to allow for a completely lazy mode of
116 // operation, where the verifier will construct a list of type
117 // assertions to be checked later.
119 // Some test cases for the verifier live in the "verify" module of the
120 // Mauve test suite. However, some of these are presently
121 // (2004-01-20) believed to be incorrect. (More precisely the notion
122 // of "correct" is not well-defined, and this verifier differs from
123 // others while remaining type-safe.) Some other tests live in the
124 // libgcj test suite.
125 class _Jv_BytecodeVerifier
129 static const int FLAG_INSN_START
= 1;
130 static const int FLAG_BRANCH_TARGET
= 2;
135 struct ref_intersection
;
146 // The PC corresponding to the start of the current instruction.
149 // The current state of the stack, locals, etc.
150 state
*current_state
;
152 // At each branch target we keep a linked list of all the states we
153 // can process at that point. We'll only have multiple states at a
154 // given PC if they both have different return-address types in the
155 // same stack or local slot. This array is indexed by PC and holds
156 // the list of all such states.
157 linked
<state
> **states
;
159 // We keep a linked list of all the states which we must reverify.
160 // This is the head of the list.
161 state
*next_verify_state
;
163 // We keep some flags for each instruction. The values are the
164 // FLAG_* constants defined above. This is an array indexed by PC.
167 // The bytecode itself.
168 unsigned char *bytecode
;
170 _Jv_InterpException
*exception
;
173 jclass current_class
;
175 _Jv_InterpMethod
*current_method
;
177 // A linked list of utf8 objects we allocate. This is really ugly,
178 // but without this our utf8 objects would be collected.
179 linked
<_Jv_Utf8Const
> *utf8_list
;
181 // A linked list of all ref_intersection objects we allocate.
182 ref_intersection
*isect_list
;
184 // Create a new Utf-8 constant and return it. We do this to avoid
185 // having our Utf-8 constants prematurely collected. FIXME this is
187 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
189 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
190 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
193 r
->length
= val
->length
;
195 memcpy (r
->data
, val
->data
, val
->length
+ 1);
197 linked
<_Jv_Utf8Const
> *lu
198 = (linked
<_Jv_Utf8Const
> *) _Jv_Malloc (sizeof (linked
<_Jv_Utf8Const
>));
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 (JvNewStringUTF (method
->get_method()->name
->data
));
225 buf
->append ((jchar
) '(');
226 buf
->append (JvNewStringUTF (method
->get_method()->signature
->data
));
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
->data
[0] == 'L'
371 && data
.name
->data
[data
.name
->length
- 1] == ';')
372 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
374 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
379 // See if an object of type OTHER can be assigned to an object of
380 // type *THIS. This might resolve classes in one chain or the
382 bool compatible (ref_intersection
*other
,
383 _Jv_BytecodeVerifier
*verifier
)
385 ref_intersection
*self
= this;
387 for (; self
!= NULL
; self
= self
->ref_next
)
389 ref_intersection
*other_iter
= other
;
391 for (; other_iter
!= NULL
; other_iter
= other_iter
->ref_next
)
393 // Avoid resolving if possible.
394 if (! self
->is_resolved
395 && ! other_iter
->is_resolved
396 && _Jv_equalUtf8Consts (self
->data
.name
,
397 other_iter
->data
.name
))
400 if (! self
->is_resolved
)
401 self
->resolve(verifier
);
402 if (! other_iter
->is_resolved
)
403 other_iter
->resolve(verifier
);
405 if (! is_assignable_from_slow (self
->data
.klass
,
406 other_iter
->data
.klass
))
416 // assert (ref_next == NULL);
418 return data
.klass
->isArray ();
420 return data
.name
->data
[0] == '[';
423 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
425 // assert (ref_next == NULL);
428 return data
.klass
->isInterface ();
431 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
433 // assert (ref_next == NULL);
436 using namespace java::lang::reflect
;
437 return Modifier::isAbstract (data
.klass
->getModifiers ());
440 jclass
getclass (_Jv_BytecodeVerifier
*verifier
)
447 int count_dimensions ()
452 jclass k
= data
.klass
;
453 while (k
->isArray ())
455 k
= k
->getComponentType ();
461 char *p
= data
.name
->data
;
468 void *operator new (size_t bytes
)
470 return _Jv_Malloc (bytes
);
473 void operator delete (void *mem
)
479 // Return the type_val corresponding to a primitive signature
480 // character. For instance `I' returns `int.class'.
481 type_val
get_type_val_for_signature (jchar sig
)
514 verify_fail ("invalid signature");
519 // Return the type_val corresponding to a primitive class.
520 type_val
get_type_val_for_signature (jclass k
)
522 return get_type_val_for_signature ((jchar
) k
->method_count
);
525 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
526 // TARGET haven't been prepared.
527 static bool is_assignable_from_slow (jclass target
, jclass source
)
529 // First, strip arrays.
530 while (target
->isArray ())
532 // If target is array, source must be as well.
533 if (! source
->isArray ())
535 target
= target
->getComponentType ();
536 source
= source
->getComponentType ();
540 if (target
== &java::lang::Object::class$
)
545 if (source
== target
)
548 if (target
->isPrimitive () || source
->isPrimitive ())
551 if (target
->isInterface ())
553 for (int i
= 0; i
< source
->interface_count
; ++i
)
555 // We use a recursive call because we also need to
556 // check superinterfaces.
557 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
561 source
= source
->getSuperclass ();
563 while (source
!= NULL
);
568 // The `type' class is used to represent a single type in the
575 // For reference types, the representation of the type.
576 ref_intersection
*klass
;
578 // This is used in two situations.
580 // First, when constructing a new object, it is the PC of the
581 // `new' instruction which created the object. We use the special
582 // value UNINIT to mean that this is uninitialized, and the
583 // special value SELF for the case where the current method is
584 // itself the <init> method.
586 // Second, when the key is return_address_type, this holds the PC
587 // of the instruction following the `jsr'.
590 static const int UNINIT
= -2;
591 static const int SELF
= -1;
593 // Basic constructor.
596 key
= unsuitable_type
;
601 // Make a new instance given the type tag. We assume a generic
602 // `reference_type' means Object.
606 // For reference_type, if KLASS==NULL then that means we are
607 // looking for a generic object of any kind, including an
608 // uninitialized reference.
613 // Make a new instance given a class.
614 type (jclass k
, _Jv_BytecodeVerifier
*verifier
)
616 key
= reference_type
;
617 klass
= new ref_intersection (k
, verifier
);
621 // Make a new instance given the name of a class.
622 type (_Jv_Utf8Const
*n
, _Jv_BytecodeVerifier
*verifier
)
624 key
= reference_type
;
625 klass
= new ref_intersection (n
, verifier
);
637 // These operators are required because libgcj can't link in
639 void *operator new[] (size_t bytes
)
641 return _Jv_Malloc (bytes
);
644 void operator delete[] (void *mem
)
649 type
& operator= (type_val k
)
657 type
& operator= (const type
& t
)
665 // Promote a numeric type.
668 if (key
== boolean_type
|| key
== char_type
669 || key
== byte_type
|| key
== short_type
)
674 // Mark this type as the uninitialized result of `new'.
675 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
677 if (key
== reference_type
)
678 key
= uninitialized_reference_type
;
680 verifier
->verify_fail ("internal error in type::uninitialized");
684 // Mark this type as now initialized.
685 void set_initialized (int npc
)
687 if (npc
!= UNINIT
&& pc
== npc
&& key
== uninitialized_reference_type
)
689 key
= reference_type
;
694 // Mark this type as a particular return address.
695 void set_return_address (int npc
)
700 // Return true if this type and type OTHER are considered
701 // mergeable for the purposes of state merging. This is related
702 // to subroutine handling. For this purpose two types are
703 // considered unmergeable if they are both return-addresses but
704 // have different PCs.
705 bool state_mergeable_p (const type
&other
) const
707 return (key
!= return_address_type
708 || other
.key
!= return_address_type
712 // Return true if an object of type K can be assigned to a variable
713 // of type *THIS. Handle various special cases too. Might modify
714 // *THIS or K. Note however that this does not perform numeric
716 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
718 // Any type is compatible with the unsuitable type.
719 if (key
== unsuitable_type
)
722 if (key
< reference_type
|| k
.key
< reference_type
)
725 // The `null' type is convertible to any initialized reference
727 if (key
== null_type
)
728 return k
.key
!= uninitialized_reference_type
;
729 if (k
.key
== null_type
)
730 return key
!= uninitialized_reference_type
;
732 // A special case for a generic reference.
736 verifier
->verify_fail ("programmer error in type::compatible");
738 // An initialized type and an uninitialized type are not
740 if (isinitialized () != k
.isinitialized ())
743 // Two uninitialized objects are compatible if either:
744 // * The PCs are identical, or
745 // * One PC is UNINIT.
746 if (! isinitialized ())
748 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
752 return klass
->compatible(k
.klass
, verifier
);
757 return key
== void_type
;
762 return key
== long_type
|| key
== double_type
;
765 // Return number of stack or local variable slots taken by this
769 return iswide () ? 2 : 1;
772 bool isarray () const
774 // We treat null_type as not an array. This is ok based on the
775 // current uses of this method.
776 if (key
== reference_type
)
777 return klass
->isarray ();
783 return key
== null_type
;
786 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
788 if (key
!= reference_type
)
790 return klass
->isinterface (verifier
);
793 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
795 if (key
!= reference_type
)
797 return klass
->isabstract (verifier
);
800 // Return the element type of an array.
801 type
element_type (_Jv_BytecodeVerifier
*verifier
)
803 if (key
!= reference_type
)
804 verifier
->verify_fail ("programmer error in type::element_type()", -1);
806 jclass k
= klass
->getclass (verifier
)->getComponentType ();
807 if (k
->isPrimitive ())
808 return type (verifier
->get_type_val_for_signature (k
));
809 return type (k
, verifier
);
812 // Return the array type corresponding to an initialized
813 // reference. We could expand this to work for other kinds of
814 // types, but currently we don't need to.
815 type
to_array (_Jv_BytecodeVerifier
*verifier
)
817 if (key
!= reference_type
)
818 verifier
->verify_fail ("internal error in type::to_array()");
820 jclass k
= klass
->getclass (verifier
);
821 return type (_Jv_GetArrayClass (k
, k
->getClassLoaderInternal()),
825 bool isreference () const
827 return key
>= reference_type
;
835 bool isinitialized () const
837 return key
== reference_type
|| key
== null_type
;
840 bool isresolved () const
842 return (key
== reference_type
844 || key
== uninitialized_reference_type
);
847 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
849 // The way this is written, we don't need to check isarray().
850 if (key
!= reference_type
)
851 verifier
->verify_fail ("internal error in verify_dimensions:"
852 " not a reference type");
854 if (klass
->count_dimensions () < ndims
)
855 verifier
->verify_fail ("array type has fewer dimensions"
859 // Merge OLD_TYPE into this. On error throw exception. Return
860 // true if the merge caused a type change.
861 bool merge (type
& old_type
, bool local_semantics
,
862 _Jv_BytecodeVerifier
*verifier
)
864 bool changed
= false;
865 bool refo
= old_type
.isreference ();
866 bool refn
= isreference ();
869 if (old_type
.key
== null_type
)
871 else if (key
== null_type
)
876 else if (isinitialized () != old_type
.isinitialized ())
877 verifier
->verify_fail ("merging initialized and uninitialized types");
880 if (! isinitialized ())
884 else if (old_type
.pc
== UNINIT
)
886 else if (pc
!= old_type
.pc
)
887 verifier
->verify_fail ("merging different uninitialized types");
890 ref_intersection
*merged
= old_type
.klass
->merge (klass
,
899 else if (refo
|| refn
|| key
!= old_type
.key
)
903 // If we already have an `unsuitable' type, then we
904 // don't need to change again.
905 if (key
!= unsuitable_type
)
907 key
= unsuitable_type
;
912 verifier
->verify_fail ("unmergeable type");
918 void print (void) const
923 case boolean_type
: c
= 'Z'; break;
924 case byte_type
: c
= 'B'; break;
925 case char_type
: c
= 'C'; break;
926 case short_type
: c
= 'S'; break;
927 case int_type
: c
= 'I'; break;
928 case long_type
: c
= 'J'; break;
929 case float_type
: c
= 'F'; break;
930 case double_type
: c
= 'D'; break;
931 case void_type
: c
= 'V'; break;
932 case unsuitable_type
: c
= '-'; break;
933 case return_address_type
: c
= 'r'; break;
934 case continuation_type
: c
= '+'; break;
935 case reference_type
: c
= 'L'; break;
936 case null_type
: c
= '@'; break;
937 case uninitialized_reference_type
: c
= 'U'; break;
939 debug_print ("%c", c
);
941 #endif /* VERIFY_DEBUG */
944 // This class holds all the state information we need for a given
948 // The current top of the stack, in terms of slots.
950 // The current depth of the stack. This will be larger than
951 // STACKTOP when wide types are on the stack.
955 // The local variables.
957 // We keep track of the type of `this' specially. This is used to
958 // ensure that an instance initializer invokes another initializer
959 // on `this' before returning. We must keep track of this
960 // specially because otherwise we might be confused by code which
961 // assigns to locals[0] (overwriting `this') and then returns
962 // without really initializing.
965 // The PC for this state. This is only valid on states which are
966 // permanently attached to a given PC. For an object like
967 // `current_state', which is used transiently, this has no
970 // We keep a linked list of all states requiring reverification.
971 // If this is the special value INVALID_STATE then this state is
972 // not on the list. NULL marks the end of the linked list.
975 // NO_NEXT is the PC value meaning that a new state must be
976 // acquired from the verification list.
977 static const int NO_NEXT
= -1;
984 next
= INVALID_STATE
;
987 state (int max_stack
, int max_locals
)
992 stack
= new type
[max_stack
];
993 for (int i
= 0; i
< max_stack
; ++i
)
994 stack
[i
] = unsuitable_type
;
995 locals
= new type
[max_locals
];
996 for (int i
= 0; i
< max_locals
; ++i
)
997 locals
[i
] = unsuitable_type
;
999 next
= INVALID_STATE
;
1002 state (const state
*orig
, int max_stack
, int max_locals
)
1004 stack
= new type
[max_stack
];
1005 locals
= new type
[max_locals
];
1006 copy (orig
, max_stack
, max_locals
);
1008 next
= INVALID_STATE
;
1019 void *operator new[] (size_t bytes
)
1021 return _Jv_Malloc (bytes
);
1024 void operator delete[] (void *mem
)
1029 void *operator new (size_t bytes
)
1031 return _Jv_Malloc (bytes
);
1034 void operator delete (void *mem
)
1039 void copy (const state
*copy
, int max_stack
, int max_locals
)
1041 stacktop
= copy
->stacktop
;
1042 stackdepth
= copy
->stackdepth
;
1043 for (int i
= 0; i
< max_stack
; ++i
)
1044 stack
[i
] = copy
->stack
[i
];
1045 for (int i
= 0; i
< max_locals
; ++i
)
1046 locals
[i
] = copy
->locals
[i
];
1048 this_type
= copy
->this_type
;
1049 // Don't modify `next' or `pc'.
1052 // Modify this state to reflect entry to an exception handler.
1053 void set_exception (type t
, int max_stack
)
1058 for (int i
= stacktop
; i
< max_stack
; ++i
)
1059 stack
[i
] = unsuitable_type
;
1062 inline int get_pc () const
1067 void set_pc (int npc
)
1072 // Merge STATE_OLD into this state. Destructively modifies this
1073 // state. Returns true if the new state was in fact changed.
1074 // Will throw an exception if the states are not mergeable.
1075 bool merge (state
*state_old
, int max_locals
,
1076 _Jv_BytecodeVerifier
*verifier
)
1078 bool changed
= false;
1080 // Special handling for `this'. If one or the other is
1081 // uninitialized, then the merge is uninitialized.
1082 if (this_type
.isinitialized ())
1083 this_type
= state_old
->this_type
;
1086 if (state_old
->stacktop
!= stacktop
) // FIXME stackdepth instead?
1087 verifier
->verify_fail ("stack sizes differ");
1088 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
1090 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
1094 // Merge local variables.
1095 for (int i
= 0; i
< max_locals
; ++i
)
1097 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
1104 // Throw an exception if there is an uninitialized object on the
1105 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1106 // whether we're using backwards-branch or exception-handing
1108 void check_no_uninitialized_objects (int max_locals
,
1109 _Jv_BytecodeVerifier
*verifier
,
1110 bool exception_semantics
= false)
1112 if (! exception_semantics
)
1114 for (int i
= 0; i
< stacktop
; ++i
)
1115 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1116 verifier
->verify_fail ("uninitialized object on stack");
1119 for (int i
= 0; i
< max_locals
; ++i
)
1120 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1121 verifier
->verify_fail ("uninitialized object in local variable");
1123 check_this_initialized (verifier
);
1126 // Ensure that `this' has been initialized.
1127 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1129 if (this_type
.isreference () && ! this_type
.isinitialized ())
1130 verifier
->verify_fail ("`this' is uninitialized");
1133 // Set type of `this'.
1134 void set_this_type (const type
&k
)
1139 // Mark each `new'd object we know of that was allocated at PC as
1141 void set_initialized (int pc
, int max_locals
)
1143 for (int i
= 0; i
< stacktop
; ++i
)
1144 stack
[i
].set_initialized (pc
);
1145 for (int i
= 0; i
< max_locals
; ++i
)
1146 locals
[i
].set_initialized (pc
);
1147 this_type
.set_initialized (pc
);
1150 // This tests to see whether two states can be considered "merge
1151 // compatible". If both states have a return-address in the same
1152 // slot, and the return addresses are different, then they are not
1153 // compatible and we must not try to merge them.
1154 bool state_mergeable_p (state
*other
, int max_locals
,
1155 _Jv_BytecodeVerifier
*verifier
)
1157 // This is tricky: if the stack sizes differ, then not only are
1158 // these not mergeable, but in fact we should give an error, as
1159 // we've found two execution paths that reach a branch target
1160 // with different stack depths. FIXME stackdepth instead?
1161 if (stacktop
!= other
->stacktop
)
1162 verifier
->verify_fail ("stack sizes differ");
1164 for (int i
= 0; i
< stacktop
; ++i
)
1165 if (! stack
[i
].state_mergeable_p (other
->stack
[i
]))
1167 for (int i
= 0; i
< max_locals
; ++i
)
1168 if (! locals
[i
].state_mergeable_p (other
->locals
[i
]))
1173 void reverify (_Jv_BytecodeVerifier
*verifier
)
1175 if (next
== INVALID_STATE
)
1177 next
= verifier
->next_verify_state
;
1178 verifier
->next_verify_state
= this;
1183 void print (const char *leader
, int pc
,
1184 int max_stack
, int max_locals
) const
1186 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1188 for (i
= 0; i
< stacktop
; ++i
)
1190 for (; i
< max_stack
; ++i
)
1192 debug_print (" [local] ");
1193 for (i
= 0; i
< max_locals
; ++i
)
1195 debug_print (" | %p\n", this);
1198 inline void print (const char *, int, int, int) const
1201 #endif /* VERIFY_DEBUG */
1206 if (current_state
->stacktop
<= 0)
1207 verify_fail ("stack empty");
1208 type r
= current_state
->stack
[--current_state
->stacktop
];
1209 current_state
->stackdepth
-= r
.depth ();
1210 if (current_state
->stackdepth
< 0)
1211 verify_fail ("stack empty", start_PC
);
1217 type r
= pop_raw ();
1219 verify_fail ("narrow pop of wide type");
1223 type
pop_type (type match
)
1226 type t
= pop_raw ();
1227 if (! match
.compatible (t
, this))
1228 verify_fail ("incompatible type on stack");
1232 // Pop a reference which is guaranteed to be initialized. MATCH
1233 // doesn't have to be a reference type; in this case this acts like
1235 type
pop_init_ref (type match
)
1237 type t
= pop_raw ();
1238 if (t
.isreference () && ! t
.isinitialized ())
1239 verify_fail ("initialized reference required");
1240 else if (! match
.compatible (t
, this))
1241 verify_fail ("incompatible type on stack");
1245 // Pop a reference type or a return address.
1246 type
pop_ref_or_return ()
1248 type t
= pop_raw ();
1249 if (! t
.isreference () && t
.key
!= return_address_type
)
1250 verify_fail ("expected reference or return address on stack");
1254 void push_type (type t
)
1256 // If T is a numeric type like short, promote it to int.
1259 int depth
= t
.depth ();
1260 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1261 verify_fail ("stack overflow");
1262 current_state
->stack
[current_state
->stacktop
++] = t
;
1263 current_state
->stackdepth
+= depth
;
1266 void set_variable (int index
, type t
)
1268 // If T is a numeric type like short, promote it to int.
1271 int depth
= t
.depth ();
1272 if (index
> current_method
->max_locals
- depth
)
1273 verify_fail ("invalid local variable");
1274 current_state
->locals
[index
] = t
;
1277 current_state
->locals
[index
+ 1] = continuation_type
;
1278 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1279 current_state
->locals
[index
- 1] = unsuitable_type
;
1282 type
get_variable (int index
, type t
)
1284 int depth
= t
.depth ();
1285 if (index
> current_method
->max_locals
- depth
)
1286 verify_fail ("invalid local variable");
1287 if (! t
.compatible (current_state
->locals
[index
], this))
1288 verify_fail ("incompatible type in local variable");
1291 type
t (continuation_type
);
1292 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1293 verify_fail ("invalid local variable");
1295 return current_state
->locals
[index
];
1298 // Make sure ARRAY is an array type and that its elements are
1299 // compatible with type ELEMENT. Returns the actual element type.
1300 type
require_array_type (type array
, type element
)
1302 // An odd case. Here we just pretend that everything went ok. If
1303 // the requested element type is some kind of reference, return
1304 // the null type instead.
1305 if (array
.isnull ())
1306 return element
.isreference () ? type (null_type
) : element
;
1308 if (! array
.isarray ())
1309 verify_fail ("array required");
1311 type t
= array
.element_type (this);
1312 if (! element
.compatible (t
, this))
1314 // Special case for byte arrays, which must also be boolean
1317 if (element
.key
== byte_type
)
1319 type
e2 (boolean_type
);
1320 ok
= e2
.compatible (t
, this);
1323 verify_fail ("incompatible array element type");
1326 // Return T and not ELEMENT, because T might be specialized.
1332 if (PC
>= current_method
->code_length
)
1333 verify_fail ("premature end of bytecode");
1334 return (jint
) bytecode
[PC
++] & 0xff;
1339 jint b1
= get_byte ();
1340 jint b2
= get_byte ();
1341 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1346 jint b1
= get_byte ();
1347 jint b2
= get_byte ();
1348 jshort s
= (b1
<< 8) | b2
;
1354 jint b1
= get_byte ();
1355 jint b2
= get_byte ();
1356 jint b3
= get_byte ();
1357 jint b4
= get_byte ();
1358 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1361 int compute_jump (int offset
)
1363 int npc
= start_PC
+ offset
;
1364 if (npc
< 0 || npc
>= current_method
->code_length
)
1365 verify_fail ("branch out of range", start_PC
);
1369 // Add a new state to the state list at NPC.
1370 state
*add_new_state (int npc
, state
*old_state
)
1372 state
*new_state
= new state (old_state
, current_method
->max_stack
,
1373 current_method
->max_locals
);
1374 debug_print ("== New state in add_new_state\n");
1375 new_state
->print ("New", npc
, current_method
->max_stack
,
1376 current_method
->max_locals
);
1377 linked
<state
> *nlink
1378 = (linked
<state
> *) _Jv_Malloc (sizeof (linked
<state
>));
1379 nlink
->val
= new_state
;
1380 nlink
->next
= states
[npc
];
1381 states
[npc
] = nlink
;
1382 new_state
->set_pc (npc
);
1386 // Merge the indicated state into the state at the branch target and
1387 // schedule a new PC if there is a change. NPC is the PC of the
1388 // branch target, and FROM_STATE is the state at the source of the
1389 // branch. This method returns true if the destination state
1390 // changed and requires reverification, false otherwise.
1391 void merge_into (int npc
, state
*from_state
)
1393 // Iterate over all target states and merge our state into each,
1394 // if applicable. FIXME one improvement we could make here is
1395 // "state destruction". Merging a new state into an existing one
1396 // might cause a return_address_type to be merged to
1397 // unsuitable_type. In this case the resulting state may now be
1398 // mergeable with other states currently held in parallel at this
1399 // location. So in this situation we could pairwise compare and
1400 // reduce the number of parallel states.
1401 bool applicable
= false;
1402 for (linked
<state
> *iter
= states
[npc
]; iter
!= NULL
; iter
= iter
->next
)
1404 state
*new_state
= iter
->val
;
1405 if (new_state
->state_mergeable_p (from_state
,
1406 current_method
->max_locals
, this))
1410 debug_print ("== Merge states in merge_into\n");
1411 from_state
->print ("Frm", start_PC
, current_method
->max_stack
,
1412 current_method
->max_locals
);
1413 new_state
->print (" To", npc
, current_method
->max_stack
,
1414 current_method
->max_locals
);
1415 bool changed
= new_state
->merge (from_state
,
1416 current_method
->max_locals
,
1418 new_state
->print ("New", npc
, current_method
->max_stack
,
1419 current_method
->max_locals
);
1422 new_state
->reverify (this);
1428 // Either we don't yet have a state at NPC, or we have a
1429 // return-address type that is in conflict with all existing
1430 // state. So, we need to create a new entry.
1431 state
*new_state
= add_new_state (npc
, from_state
);
1432 // A new state added in this way must always be reverified.
1433 new_state
->reverify (this);
1437 void push_jump (int offset
)
1439 int npc
= compute_jump (offset
);
1441 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1442 merge_into (npc
, current_state
);
1445 void push_exception_jump (type t
, int pc
)
1447 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1449 state
s (current_state
, current_method
->max_stack
,
1450 current_method
->max_locals
);
1451 if (current_method
->max_stack
< 1)
1452 verify_fail ("stack overflow at exception handler");
1453 s
.set_exception (t
, current_method
->max_stack
);
1454 merge_into (pc
, &s
);
1459 state
*new_state
= next_verify_state
;
1460 if (new_state
== INVALID_STATE
)
1461 verify_fail ("programmer error in pop_jump");
1462 if (new_state
!= NULL
)
1464 next_verify_state
= new_state
->next
;
1465 new_state
->next
= INVALID_STATE
;
1470 void invalidate_pc ()
1472 PC
= state::NO_NEXT
;
1475 void note_branch_target (int pc
)
1477 // Don't check `pc <= PC', because we've advanced PC after
1478 // fetching the target and we haven't yet checked the next
1480 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1481 verify_fail ("branch not to instruction start", start_PC
);
1482 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1485 void skip_padding ()
1487 while ((PC
% 4) > 0)
1488 if (get_byte () != 0)
1489 verify_fail ("found nonzero padding byte");
1492 // Do the work for a `ret' instruction. INDEX is the index into the
1494 void handle_ret_insn (int index
)
1496 type ret_addr
= get_variable (index
, return_address_type
);
1497 // It would be nice if we could do this. However, the JVM Spec
1498 // doesn't say that this is what happens. It is implied that
1499 // reusing a return address is invalid, but there's no actual
1500 // prohibition against it.
1501 // set_variable (index, unsuitable_type);
1503 int npc
= ret_addr
.get_pc ();
1504 // We might be returning to a `jsr' that is at the end of the
1505 // bytecode. This is ok if we never return from the called
1506 // subroutine, but if we see this here it is an error.
1507 if (npc
>= current_method
->code_length
)
1508 verify_fail ("fell off end");
1511 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1513 merge_into (npc
, current_state
);
1517 void handle_jsr_insn (int offset
)
1519 int npc
= compute_jump (offset
);
1522 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1524 // Modify our state as appropriate for entry into a subroutine.
1525 type
ret_addr (return_address_type
);
1526 ret_addr
.set_return_address (PC
);
1527 push_type (ret_addr
);
1528 merge_into (npc
, current_state
);
1532 jclass
construct_primitive_array_type (type_val prim
)
1538 k
= JvPrimClass (boolean
);
1541 k
= JvPrimClass (char);
1544 k
= JvPrimClass (float);
1547 k
= JvPrimClass (double);
1550 k
= JvPrimClass (byte
);
1553 k
= JvPrimClass (short);
1556 k
= JvPrimClass (int);
1559 k
= JvPrimClass (long);
1562 // These aren't used here but we call them out to avoid
1565 case unsuitable_type
:
1566 case return_address_type
:
1567 case continuation_type
:
1568 case reference_type
:
1570 case uninitialized_reference_type
:
1572 verify_fail ("unknown type in construct_primitive_array_type");
1574 k
= _Jv_GetArrayClass (k
, NULL
);
1578 // This pass computes the location of branch targets and also
1579 // instruction starts.
1580 void branch_prepass ()
1582 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1584 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1588 while (PC
< current_method
->code_length
)
1590 // Set `start_PC' early so that error checking can have the
1593 flags
[PC
] |= FLAG_INSN_START
;
1595 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1599 case op_aconst_null
:
1735 case op_monitorenter
:
1736 case op_monitorexit
:
1744 case op_arraylength
:
1776 case op_invokespecial
:
1777 case op_invokestatic
:
1778 case op_invokevirtual
:
1782 case op_multianewarray
:
1805 note_branch_target (compute_jump (get_short ()));
1808 case op_tableswitch
:
1811 note_branch_target (compute_jump (get_int ()));
1812 jint low
= get_int ();
1813 jint hi
= get_int ();
1815 verify_fail ("invalid tableswitch", start_PC
);
1816 for (int i
= low
; i
<= hi
; ++i
)
1817 note_branch_target (compute_jump (get_int ()));
1821 case op_lookupswitch
:
1824 note_branch_target (compute_jump (get_int ()));
1825 int npairs
= get_int ();
1827 verify_fail ("too few pairs in lookupswitch", start_PC
);
1828 while (npairs
-- > 0)
1831 note_branch_target (compute_jump (get_int ()));
1836 case op_invokeinterface
:
1844 opcode
= (java_opcode
) get_byte ();
1846 if (opcode
== op_iinc
)
1853 note_branch_target (compute_jump (get_int ()));
1856 // These are unused here, but we call them out explicitly
1857 // so that -Wswitch-enum doesn't complain.
1863 case op_putstatic_1
:
1864 case op_putstatic_2
:
1865 case op_putstatic_4
:
1866 case op_putstatic_8
:
1867 case op_putstatic_a
:
1869 case op_getfield_2s
:
1870 case op_getfield_2u
:
1874 case op_getstatic_1
:
1875 case op_getstatic_2s
:
1876 case op_getstatic_2u
:
1877 case op_getstatic_4
:
1878 case op_getstatic_8
:
1879 case op_getstatic_a
:
1881 verify_fail ("unrecognized instruction in branch_prepass",
1885 // See if any previous branch tried to branch to the middle of
1886 // this instruction.
1887 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1889 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1890 verify_fail ("branch to middle of instruction", pc
);
1894 // Verify exception handlers.
1895 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1897 if (! (flags
[exception
[i
].handler_pc
.i
] & FLAG_INSN_START
))
1898 verify_fail ("exception handler not at instruction start",
1899 exception
[i
].handler_pc
.i
);
1900 if (! (flags
[exception
[i
].start_pc
.i
] & FLAG_INSN_START
))
1901 verify_fail ("exception start not at instruction start",
1902 exception
[i
].start_pc
.i
);
1903 if (exception
[i
].end_pc
.i
!= current_method
->code_length
1904 && ! (flags
[exception
[i
].end_pc
.i
] & FLAG_INSN_START
))
1905 verify_fail ("exception end not at instruction start",
1906 exception
[i
].end_pc
.i
);
1908 flags
[exception
[i
].handler_pc
.i
] |= FLAG_BRANCH_TARGET
;
1912 void check_pool_index (int index
)
1914 if (index
< 0 || index
>= current_class
->constants
.size
)
1915 verify_fail ("constant pool index out of range", start_PC
);
1918 type
check_class_constant (int index
)
1920 check_pool_index (index
);
1921 _Jv_Constants
*pool
= ¤t_class
->constants
;
1922 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1923 return type (pool
->data
[index
].clazz
, this);
1924 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1925 return type (pool
->data
[index
].utf8
, this);
1926 verify_fail ("expected class constant", start_PC
);
1929 type
check_constant (int index
)
1931 check_pool_index (index
);
1932 _Jv_Constants
*pool
= ¤t_class
->constants
;
1933 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1934 || pool
->tags
[index
] == JV_CONSTANT_String
)
1935 return type (&java::lang::String::class$
, this);
1936 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1937 return type (int_type
);
1938 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1939 return type (float_type
);
1940 verify_fail ("String, int, or float constant expected", start_PC
);
1943 type
check_wide_constant (int index
)
1945 check_pool_index (index
);
1946 _Jv_Constants
*pool
= ¤t_class
->constants
;
1947 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1948 return type (long_type
);
1949 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1950 return type (double_type
);
1951 verify_fail ("long or double constant expected", start_PC
);
1954 // Helper for both field and method. These are laid out the same in
1955 // the constant pool.
1956 type
handle_field_or_method (int index
, int expected
,
1957 _Jv_Utf8Const
**name
,
1958 _Jv_Utf8Const
**fmtype
)
1960 check_pool_index (index
);
1961 _Jv_Constants
*pool
= ¤t_class
->constants
;
1962 if (pool
->tags
[index
] != expected
)
1963 verify_fail ("didn't see expected constant", start_PC
);
1964 // Once we know we have a Fieldref or Methodref we assume that it
1965 // is correctly laid out in the constant pool. I think the code
1966 // in defineclass.cc guarantees this.
1967 _Jv_ushort class_index
, name_and_type_index
;
1968 _Jv_loadIndexes (&pool
->data
[index
],
1970 name_and_type_index
);
1971 _Jv_ushort name_index
, desc_index
;
1972 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1973 name_index
, desc_index
);
1975 *name
= pool
->data
[name_index
].utf8
;
1976 *fmtype
= pool
->data
[desc_index
].utf8
;
1978 return check_class_constant (class_index
);
1981 // Return field's type, compute class' type if requested.
1982 type
check_field_constant (int index
, type
*class_type
= NULL
)
1984 _Jv_Utf8Const
*name
, *field_type
;
1985 type ct
= handle_field_or_method (index
,
1986 JV_CONSTANT_Fieldref
,
1987 &name
, &field_type
);
1990 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1991 return type (field_type
, this);
1992 return get_type_val_for_signature (field_type
->data
[0]);
1995 type
check_method_constant (int index
, bool is_interface
,
1996 _Jv_Utf8Const
**method_name
,
1997 _Jv_Utf8Const
**method_signature
)
1999 return handle_field_or_method (index
,
2001 ? JV_CONSTANT_InterfaceMethodref
2002 : JV_CONSTANT_Methodref
),
2003 method_name
, method_signature
);
2006 type
get_one_type (char *&p
)
2024 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
2025 return type (name
, this);
2028 // Casting to jchar here is ok since we are looking at an ASCII
2030 type_val rt
= get_type_val_for_signature (jchar (v
));
2032 if (arraycount
== 0)
2034 // Callers of this function eventually push their arguments on
2035 // the stack. So, promote them here.
2036 return type (rt
).promote ();
2039 jclass k
= construct_primitive_array_type (rt
);
2040 while (--arraycount
> 0)
2041 k
= _Jv_GetArrayClass (k
, NULL
);
2042 return type (k
, this);
2045 void compute_argument_types (_Jv_Utf8Const
*signature
,
2048 char *p
= signature
->data
;
2054 types
[i
++] = get_one_type (p
);
2057 type
compute_return_type (_Jv_Utf8Const
*signature
)
2059 char *p
= signature
->data
;
2063 return get_one_type (p
);
2066 void check_return_type (type onstack
)
2068 type rt
= compute_return_type (current_method
->self
->signature
);
2069 if (! rt
.compatible (onstack
, this))
2070 verify_fail ("incompatible return type");
2073 // Initialize the stack for the new method. Returns true if this
2074 // method is an instance initializer.
2075 bool initialize_stack ()
2078 bool is_init
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2080 bool is_clinit
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2083 using namespace java::lang::reflect
;
2084 if (! Modifier::isStatic (current_method
->self
->accflags
))
2086 type
kurr (current_class
, this);
2089 kurr
.set_uninitialized (type::SELF
, this);
2093 verify_fail ("<clinit> method must be static");
2094 set_variable (0, kurr
);
2095 current_state
->set_this_type (kurr
);
2101 verify_fail ("<init> method must be non-static");
2104 // We have to handle wide arguments specially here.
2105 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2106 type arg_types
[arg_count
];
2107 compute_argument_types (current_method
->self
->signature
, arg_types
);
2108 for (int i
= 0; i
< arg_count
; ++i
)
2110 set_variable (var
, arg_types
[i
]);
2112 if (arg_types
[i
].iswide ())
2119 void verify_instructions_0 ()
2121 current_state
= new state (current_method
->max_stack
,
2122 current_method
->max_locals
);
2127 // True if we are verifying an instance initializer.
2128 bool this_is_init
= initialize_stack ();
2130 states
= (linked
<state
> **) _Jv_Malloc (sizeof (linked
<state
> *)
2131 * current_method
->code_length
);
2132 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2135 next_verify_state
= NULL
;
2139 // If the PC was invalidated, get a new one from the work list.
2140 if (PC
== state::NO_NEXT
)
2142 state
*new_state
= pop_jump ();
2143 // If it is null, we're done.
2144 if (new_state
== NULL
)
2147 PC
= new_state
->get_pc ();
2148 debug_print ("== State pop from pending list\n");
2149 // Set up the current state.
2150 current_state
->copy (new_state
, current_method
->max_stack
,
2151 current_method
->max_locals
);
2155 // We only have to do this checking in the situation where
2156 // control flow falls through from the previous
2157 // instruction. Otherwise merging is done at the time we
2159 if (states
[PC
] != NULL
)
2161 // We've already visited this instruction. So merge
2162 // the states together. It is simplest, but not most
2163 // efficient, to just always invalidate the PC here.
2164 merge_into (PC
, current_state
);
2170 // Control can't fall off the end of the bytecode. We need to
2171 // check this in both cases, not just the fall-through case,
2172 // because we don't check to see whether a `jsr' appears at
2173 // the end of the bytecode until we process a `ret'.
2174 if (PC
>= current_method
->code_length
)
2175 verify_fail ("fell off end");
2177 // We only have to keep saved state at branch targets. If
2178 // we're at a branch target and the state here hasn't been set
2179 // yet, we set it now. You might notice that `ret' targets
2180 // won't necessarily have FLAG_BRANCH_TARGET set. This
2181 // doesn't matter, since those states will be filled in by
2183 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2184 add_new_state (PC
, current_state
);
2186 // Set this before handling exceptions so that debug output is
2190 // Update states for all active exception handlers. Ordinarily
2191 // there are not many exception handlers. So we simply run
2192 // through them all.
2193 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2195 if (PC
>= exception
[i
].start_pc
.i
&& PC
< exception
[i
].end_pc
.i
)
2197 type
handler (&java::lang::Throwable::class$
, this);
2198 if (exception
[i
].handler_type
.i
!= 0)
2199 handler
= check_class_constant (exception
[i
].handler_type
.i
);
2200 push_exception_jump (handler
, exception
[i
].handler_pc
.i
);
2204 current_state
->print (" ", PC
, current_method
->max_stack
,
2205 current_method
->max_locals
);
2206 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2212 case op_aconst_null
:
2213 push_type (null_type
);
2223 push_type (int_type
);
2228 push_type (long_type
);
2234 push_type (float_type
);
2239 push_type (double_type
);
2244 push_type (int_type
);
2249 push_type (int_type
);
2253 push_type (check_constant (get_byte ()));
2256 push_type (check_constant (get_ushort ()));
2259 push_type (check_wide_constant (get_ushort ()));
2263 push_type (get_variable (get_byte (), int_type
));
2266 push_type (get_variable (get_byte (), long_type
));
2269 push_type (get_variable (get_byte (), float_type
));
2272 push_type (get_variable (get_byte (), double_type
));
2275 push_type (get_variable (get_byte (), reference_type
));
2282 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2288 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2294 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2300 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2306 push_type (get_variable (opcode
- op_aload_0
, 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 push_type (require_array_type (pop_init_ref (reference_type
),
2324 pop_type (int_type
);
2325 push_type (require_array_type (pop_init_ref (reference_type
),
2329 pop_type (int_type
);
2330 push_type (require_array_type (pop_init_ref (reference_type
),
2334 pop_type (int_type
);
2335 require_array_type (pop_init_ref (reference_type
), byte_type
);
2336 push_type (int_type
);
2339 pop_type (int_type
);
2340 require_array_type (pop_init_ref (reference_type
), char_type
);
2341 push_type (int_type
);
2344 pop_type (int_type
);
2345 require_array_type (pop_init_ref (reference_type
), short_type
);
2346 push_type (int_type
);
2349 set_variable (get_byte (), pop_type (int_type
));
2352 set_variable (get_byte (), pop_type (long_type
));
2355 set_variable (get_byte (), pop_type (float_type
));
2358 set_variable (get_byte (), pop_type (double_type
));
2361 set_variable (get_byte (), pop_ref_or_return ());
2367 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2373 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2379 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2385 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2391 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2394 pop_type (int_type
);
2395 pop_type (int_type
);
2396 require_array_type (pop_init_ref (reference_type
), int_type
);
2399 pop_type (long_type
);
2400 pop_type (int_type
);
2401 require_array_type (pop_init_ref (reference_type
), long_type
);
2404 pop_type (float_type
);
2405 pop_type (int_type
);
2406 require_array_type (pop_init_ref (reference_type
), float_type
);
2409 pop_type (double_type
);
2410 pop_type (int_type
);
2411 require_array_type (pop_init_ref (reference_type
), double_type
);
2414 pop_type (reference_type
);
2415 pop_type (int_type
);
2416 require_array_type (pop_init_ref (reference_type
), reference_type
);
2419 pop_type (int_type
);
2420 pop_type (int_type
);
2421 require_array_type (pop_init_ref (reference_type
), byte_type
);
2424 pop_type (int_type
);
2425 pop_type (int_type
);
2426 require_array_type (pop_init_ref (reference_type
), char_type
);
2429 pop_type (int_type
);
2430 pop_type (int_type
);
2431 require_array_type (pop_init_ref (reference_type
), short_type
);
2438 type t
= pop_raw ();
2462 type t2
= pop_raw ();
2477 type t
= pop_raw ();
2492 type t1
= pop_raw ();
2509 type t1
= pop_raw ();
2512 type t2
= pop_raw ();
2530 type t3
= pop_raw ();
2568 pop_type (int_type
);
2569 push_type (pop_type (int_type
));
2579 pop_type (long_type
);
2580 push_type (pop_type (long_type
));
2585 pop_type (int_type
);
2586 push_type (pop_type (long_type
));
2593 pop_type (float_type
);
2594 push_type (pop_type (float_type
));
2601 pop_type (double_type
);
2602 push_type (pop_type (double_type
));
2608 push_type (pop_type (int_type
));
2611 push_type (pop_type (long_type
));
2614 push_type (pop_type (float_type
));
2617 push_type (pop_type (double_type
));
2620 get_variable (get_byte (), int_type
);
2624 pop_type (int_type
);
2625 push_type (long_type
);
2628 pop_type (int_type
);
2629 push_type (float_type
);
2632 pop_type (int_type
);
2633 push_type (double_type
);
2636 pop_type (long_type
);
2637 push_type (int_type
);
2640 pop_type (long_type
);
2641 push_type (float_type
);
2644 pop_type (long_type
);
2645 push_type (double_type
);
2648 pop_type (float_type
);
2649 push_type (int_type
);
2652 pop_type (float_type
);
2653 push_type (long_type
);
2656 pop_type (float_type
);
2657 push_type (double_type
);
2660 pop_type (double_type
);
2661 push_type (int_type
);
2664 pop_type (double_type
);
2665 push_type (long_type
);
2668 pop_type (double_type
);
2669 push_type (float_type
);
2672 pop_type (long_type
);
2673 pop_type (long_type
);
2674 push_type (int_type
);
2678 pop_type (float_type
);
2679 pop_type (float_type
);
2680 push_type (int_type
);
2684 pop_type (double_type
);
2685 pop_type (double_type
);
2686 push_type (int_type
);
2694 pop_type (int_type
);
2695 push_jump (get_short ());
2703 pop_type (int_type
);
2704 pop_type (int_type
);
2705 push_jump (get_short ());
2709 pop_type (reference_type
);
2710 pop_type (reference_type
);
2711 push_jump (get_short ());
2714 push_jump (get_short ());
2718 handle_jsr_insn (get_short ());
2721 handle_ret_insn (get_byte ());
2723 case op_tableswitch
:
2725 pop_type (int_type
);
2727 push_jump (get_int ());
2728 jint low
= get_int ();
2729 jint high
= get_int ();
2730 // Already checked LOW -vs- HIGH.
2731 for (int i
= low
; i
<= high
; ++i
)
2732 push_jump (get_int ());
2737 case op_lookupswitch
:
2739 pop_type (int_type
);
2741 push_jump (get_int ());
2742 jint npairs
= get_int ();
2743 // Already checked NPAIRS >= 0.
2745 for (int i
= 0; i
< npairs
; ++i
)
2747 jint key
= get_int ();
2748 if (i
> 0 && key
<= lastkey
)
2749 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2751 push_jump (get_int ());
2757 check_return_type (pop_type (int_type
));
2761 check_return_type (pop_type (long_type
));
2765 check_return_type (pop_type (float_type
));
2769 check_return_type (pop_type (double_type
));
2773 check_return_type (pop_init_ref (reference_type
));
2777 // We only need to check this when the return type is
2778 // void, because all instance initializers return void.
2780 current_state
->check_this_initialized (this);
2781 check_return_type (void_type
);
2785 push_type (check_field_constant (get_ushort ()));
2788 pop_type (check_field_constant (get_ushort ()));
2793 type field
= check_field_constant (get_ushort (), &klass
);
2801 type field
= check_field_constant (get_ushort (), &klass
);
2804 // We have an obscure special case here: we can use
2805 // `putfield' on a field declared in this class, even if
2806 // `this' has not yet been initialized.
2807 if (! current_state
->this_type
.isinitialized ()
2808 && current_state
->this_type
.pc
== type::SELF
)
2809 klass
.set_uninitialized (type::SELF
, this);
2814 case op_invokevirtual
:
2815 case op_invokespecial
:
2816 case op_invokestatic
:
2817 case op_invokeinterface
:
2819 _Jv_Utf8Const
*method_name
, *method_signature
;
2821 = check_method_constant (get_ushort (),
2822 opcode
== op_invokeinterface
,
2825 // NARGS is only used when we're processing
2826 // invokeinterface. It is simplest for us to compute it
2827 // here and then verify it later.
2829 if (opcode
== op_invokeinterface
)
2831 nargs
= get_byte ();
2832 if (get_byte () != 0)
2833 verify_fail ("invokeinterface dummy byte is wrong");
2836 bool is_init
= false;
2837 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2840 if (opcode
!= op_invokespecial
)
2841 verify_fail ("can't invoke <init>");
2843 else if (method_name
->data
[0] == '<')
2844 verify_fail ("can't invoke method starting with `<'");
2846 // Pop arguments and check types.
2847 int arg_count
= _Jv_count_arguments (method_signature
);
2848 type arg_types
[arg_count
];
2849 compute_argument_types (method_signature
, arg_types
);
2850 for (int i
= arg_count
- 1; i
>= 0; --i
)
2852 // This is only used for verifying the byte for
2854 nargs
-= arg_types
[i
].depth ();
2855 pop_init_ref (arg_types
[i
]);
2858 if (opcode
== op_invokeinterface
2860 verify_fail ("wrong argument count for invokeinterface");
2862 if (opcode
!= op_invokestatic
)
2864 type t
= class_type
;
2867 // In this case the PC doesn't matter.
2868 t
.set_uninitialized (type::UNINIT
, this);
2869 // FIXME: check to make sure that the <init>
2870 // call is to the right class.
2871 // It must either be super or an exact class
2874 type raw
= pop_raw ();
2875 if (! t
.compatible (raw
, this))
2876 verify_fail ("incompatible type on stack");
2879 current_state
->set_initialized (raw
.get_pc (),
2880 current_method
->max_locals
);
2883 type rt
= compute_return_type (method_signature
);
2891 type t
= check_class_constant (get_ushort ());
2892 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2893 verify_fail ("type is array, interface, or abstract");
2894 t
.set_uninitialized (start_PC
, this);
2901 int atype
= get_byte ();
2902 // We intentionally have chosen constants to make this
2904 if (atype
< boolean_type
|| atype
> long_type
)
2905 verify_fail ("type not primitive", start_PC
);
2906 pop_type (int_type
);
2907 type
t (construct_primitive_array_type (type_val (atype
)), this);
2912 pop_type (int_type
);
2913 push_type (check_class_constant (get_ushort ()).to_array (this));
2915 case op_arraylength
:
2917 type t
= pop_init_ref (reference_type
);
2918 if (! t
.isarray () && ! t
.isnull ())
2919 verify_fail ("array type expected");
2920 push_type (int_type
);
2924 pop_type (type (&java::lang::Throwable::class$
, this));
2928 pop_init_ref (reference_type
);
2929 push_type (check_class_constant (get_ushort ()));
2932 pop_init_ref (reference_type
);
2933 check_class_constant (get_ushort ());
2934 push_type (int_type
);
2936 case op_monitorenter
:
2937 pop_init_ref (reference_type
);
2939 case op_monitorexit
:
2940 pop_init_ref (reference_type
);
2944 switch (get_byte ())
2947 push_type (get_variable (get_ushort (), int_type
));
2950 push_type (get_variable (get_ushort (), long_type
));
2953 push_type (get_variable (get_ushort (), float_type
));
2956 push_type (get_variable (get_ushort (), double_type
));
2959 push_type (get_variable (get_ushort (), reference_type
));
2962 set_variable (get_ushort (), pop_type (int_type
));
2965 set_variable (get_ushort (), pop_type (long_type
));
2968 set_variable (get_ushort (), pop_type (float_type
));
2971 set_variable (get_ushort (), pop_type (double_type
));
2974 set_variable (get_ushort (), pop_init_ref (reference_type
));
2977 handle_ret_insn (get_short ());
2980 get_variable (get_ushort (), int_type
);
2984 verify_fail ("unrecognized wide instruction", start_PC
);
2988 case op_multianewarray
:
2990 type atype
= check_class_constant (get_ushort ());
2991 int dim
= get_byte ();
2993 verify_fail ("too few dimensions to multianewarray", start_PC
);
2994 atype
.verify_dimensions (dim
, this);
2995 for (int i
= 0; i
< dim
; ++i
)
2996 pop_type (int_type
);
3002 pop_type (reference_type
);
3003 push_jump (get_short ());
3006 push_jump (get_int ());
3010 handle_jsr_insn (get_int ());
3013 // These are unused here, but we call them out explicitly
3014 // so that -Wswitch-enum doesn't complain.
3020 case op_putstatic_1
:
3021 case op_putstatic_2
:
3022 case op_putstatic_4
:
3023 case op_putstatic_8
:
3024 case op_putstatic_a
:
3026 case op_getfield_2s
:
3027 case op_getfield_2u
:
3031 case op_getstatic_1
:
3032 case op_getstatic_2s
:
3033 case op_getstatic_2u
:
3034 case op_getstatic_4
:
3035 case op_getstatic_8
:
3036 case op_getstatic_a
:
3038 // Unrecognized opcode.
3039 verify_fail ("unrecognized instruction in verify_instructions_0",
3047 void verify_instructions ()
3050 verify_instructions_0 ();
3053 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3055 // We just print the text as utf-8. This is just for debugging
3057 debug_print ("--------------------------------\n");
3058 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
3061 bytecode
= m
->bytecode ();
3062 exception
= m
->exceptions ();
3063 current_class
= m
->defining_class
;
3071 ~_Jv_BytecodeVerifier ()
3076 while (utf8_list
!= NULL
)
3078 linked
<_Jv_Utf8Const
> *n
= utf8_list
->next
;
3079 _Jv_Free (utf8_list
->val
);
3080 _Jv_Free (utf8_list
);
3084 while (isect_list
!= NULL
)
3086 ref_intersection
*next
= isect_list
->alloc_next
;
3093 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3095 linked
<state
> *iter
= states
[i
];
3096 while (iter
!= NULL
)
3098 linked
<state
> *next
= iter
->next
;
3110 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3112 _Jv_BytecodeVerifier
v (meth
);
3113 v
.verify_instructions ();
3116 #endif /* INTERPRETER */