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 // but we still implement what is specified. 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.
108 // Similarly, there's no real point in enforcing that padding bytes or
109 // the mystery byte of invokeinterface must be 0, but we do that too.
111 // The verifier is currently neither completely lazy nor eager when it
112 // comes to loading classes. It tries to represent types by name when
113 // possible, and then loads them when it needs to verify a fact about
114 // the type. Checking types by name is valid because we only use
115 // names which come from the current class' constant pool. Since all
116 // such names are looked up using the same class loader, there is no
117 // danger that we might be fooled into comparing different types with
120 // In the future we plan to allow for a completely lazy mode of
121 // operation, where the verifier will construct a list of type
122 // assertions to be checked later.
124 // Some test cases for the verifier live in the "verify" module of the
125 // Mauve test suite. However, some of these are presently
126 // (2004-01-20) believed to be incorrect. (More precisely the notion
127 // of "correct" is not well-defined, and this verifier differs from
128 // others while remaining type-safe.) Some other tests live in the
129 // libgcj test suite.
130 class _Jv_BytecodeVerifier
134 static const int FLAG_INSN_START
= 1;
135 static const int FLAG_BRANCH_TARGET
= 2;
140 struct ref_intersection
;
151 // The PC corresponding to the start of the current instruction.
154 // The current state of the stack, locals, etc.
155 state
*current_state
;
157 // At each branch target we keep a linked list of all the states we
158 // can process at that point. We'll only have multiple states at a
159 // given PC if they both have different return-address types in the
160 // same stack or local slot. This array is indexed by PC and holds
161 // the list of all such states.
162 linked
<state
> **states
;
164 // We keep a linked list of all the states which we must reverify.
165 // This is the head of the list.
166 state
*next_verify_state
;
168 // We keep some flags for each instruction. The values are the
169 // FLAG_* constants defined above. This is an array indexed by PC.
172 // The bytecode itself.
173 unsigned char *bytecode
;
175 _Jv_InterpException
*exception
;
178 jclass current_class
;
180 _Jv_InterpMethod
*current_method
;
182 // A linked list of utf8 objects we allocate. This is really ugly,
183 // but without this our utf8 objects would be collected.
184 linked
<_Jv_Utf8Const
> *utf8_list
;
186 // A linked list of all ref_intersection objects we allocate.
187 ref_intersection
*isect_list
;
189 // Create a new Utf-8 constant and return it. We do this to avoid
190 // having our Utf-8 constants prematurely collected. FIXME this is
192 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
194 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
195 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
198 r
->length
= val
->length
;
200 memcpy (r
->data
, val
->data
, val
->length
+ 1);
202 linked
<_Jv_Utf8Const
> *lu
203 = (linked
<_Jv_Utf8Const
> *) _Jv_Malloc (sizeof (linked
<_Jv_Utf8Const
>));
205 lu
->next
= utf8_list
;
211 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
213 using namespace java::lang
;
214 StringBuffer
*buf
= new StringBuffer ();
216 buf
->append (JvNewStringLatin1 ("verification failed"));
221 buf
->append (JvNewStringLatin1 (" at PC "));
225 _Jv_InterpMethod
*method
= current_method
;
226 buf
->append (JvNewStringLatin1 (" in "));
227 buf
->append (current_class
->getName());
228 buf
->append ((jchar
) ':');
229 buf
->append (JvNewStringUTF (method
->get_method()->name
->data
));
230 buf
->append ((jchar
) '(');
231 buf
->append (JvNewStringUTF (method
->get_method()->signature
->data
));
232 buf
->append ((jchar
) ')');
234 buf
->append (JvNewStringLatin1 (": "));
235 buf
->append (JvNewStringLatin1 (s
));
236 throw new java::lang::VerifyError (buf
->toString ());
239 // This enum holds a list of tags for all the different types we
240 // need to handle. Reference types are treated specially by the
246 // The values for primitive types are chosen to correspond to values
247 // specified to newarray.
257 // Used when overwriting second word of a double or long in the
258 // local variables. Also used after merging local variable states
259 // to indicate an unusable value.
262 // This is the second word of a two-word value, i.e., a double or
266 // Everything after `reference_type' must be a reference type.
269 uninitialized_reference_type
272 // This represents a merged class type. Some verifiers (including
273 // earlier versions of this one) will compute the intersection of
274 // two class types when merging states. However, this loses
275 // critical information about interfaces implemented by the various
276 // classes. So instead we keep track of all the actual classes that
278 struct ref_intersection
280 // Whether or not this type has been resolved.
286 // For a resolved reference type, this is a pointer to the class.
288 // For other reference types, this it the name of the class.
292 // Link to the next reference in the intersection.
293 ref_intersection
*ref_next
;
295 // This is used to keep track of all the allocated
296 // ref_intersection objects, so we can free them.
297 // FIXME: we should allocate these in chunks.
298 ref_intersection
*alloc_next
;
300 ref_intersection (jclass klass
, _Jv_BytecodeVerifier
*verifier
)
305 alloc_next
= verifier
->isect_list
;
306 verifier
->isect_list
= this;
309 ref_intersection (_Jv_Utf8Const
*name
, _Jv_BytecodeVerifier
*verifier
)
314 alloc_next
= verifier
->isect_list
;
315 verifier
->isect_list
= this;
318 ref_intersection (ref_intersection
*dup
, ref_intersection
*tail
,
319 _Jv_BytecodeVerifier
*verifier
)
322 is_resolved
= dup
->is_resolved
;
324 alloc_next
= verifier
->isect_list
;
325 verifier
->isect_list
= this;
328 bool equals (ref_intersection
*other
, _Jv_BytecodeVerifier
*verifier
)
330 if (! is_resolved
&& ! other
->is_resolved
331 && _Jv_equalUtf8Consts (data
.name
, other
->data
.name
))
335 if (! other
->is_resolved
)
336 other
->resolve (verifier
);
337 return data
.klass
== other
->data
.klass
;
340 // Merge THIS type into OTHER, returning the result. This will
341 // return OTHER if all the classes in THIS already appear in
343 ref_intersection
*merge (ref_intersection
*other
,
344 _Jv_BytecodeVerifier
*verifier
)
346 ref_intersection
*tail
= other
;
347 for (ref_intersection
*self
= this; self
!= NULL
; self
= self
->ref_next
)
350 for (ref_intersection
*iter
= other
; iter
!= NULL
;
351 iter
= iter
->ref_next
)
353 if (iter
->equals (self
, verifier
))
361 tail
= new ref_intersection (self
, tail
, verifier
);
366 void resolve (_Jv_BytecodeVerifier
*verifier
)
371 using namespace java::lang
;
372 java::lang::ClassLoader
*loader
373 = verifier
->current_class
->getClassLoaderInternal();
374 // We might see either kind of name. Sigh.
375 if (data
.name
->data
[0] == 'L'
376 && data
.name
->data
[data
.name
->length
- 1] == ';')
377 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
379 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
384 // See if an object of type OTHER can be assigned to an object of
385 // type *THIS. This might resolve classes in one chain or the
387 bool compatible (ref_intersection
*other
,
388 _Jv_BytecodeVerifier
*verifier
)
390 ref_intersection
*self
= this;
392 for (; self
!= NULL
; self
= self
->ref_next
)
394 ref_intersection
*other_iter
= other
;
396 for (; other_iter
!= NULL
; other_iter
= other_iter
->ref_next
)
398 // Avoid resolving if possible.
399 if (! self
->is_resolved
400 && ! other_iter
->is_resolved
401 && _Jv_equalUtf8Consts (self
->data
.name
,
402 other_iter
->data
.name
))
405 if (! self
->is_resolved
)
406 self
->resolve(verifier
);
407 if (! other_iter
->is_resolved
)
408 other_iter
->resolve(verifier
);
410 if (! is_assignable_from_slow (self
->data
.klass
,
411 other_iter
->data
.klass
))
421 // assert (ref_next == NULL);
423 return data
.klass
->isArray ();
425 return data
.name
->data
[0] == '[';
428 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
430 // assert (ref_next == NULL);
433 return data
.klass
->isInterface ();
436 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
438 // assert (ref_next == NULL);
441 using namespace java::lang::reflect
;
442 return Modifier::isAbstract (data
.klass
->getModifiers ());
445 jclass
getclass (_Jv_BytecodeVerifier
*verifier
)
452 int count_dimensions ()
457 jclass k
= data
.klass
;
458 while (k
->isArray ())
460 k
= k
->getComponentType ();
466 char *p
= data
.name
->data
;
473 void *operator new (size_t bytes
)
475 return _Jv_Malloc (bytes
);
478 void operator delete (void *mem
)
484 // Return the type_val corresponding to a primitive signature
485 // character. For instance `I' returns `int.class'.
486 type_val
get_type_val_for_signature (jchar sig
)
519 verify_fail ("invalid signature");
524 // Return the type_val corresponding to a primitive class.
525 type_val
get_type_val_for_signature (jclass k
)
527 return get_type_val_for_signature ((jchar
) k
->method_count
);
530 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
531 // TARGET haven't been prepared.
532 static bool is_assignable_from_slow (jclass target
, jclass source
)
534 // First, strip arrays.
535 while (target
->isArray ())
537 // If target is array, source must be as well.
538 if (! source
->isArray ())
540 target
= target
->getComponentType ();
541 source
= source
->getComponentType ();
545 if (target
== &java::lang::Object::class$
)
550 if (source
== target
)
553 if (target
->isPrimitive () || source
->isPrimitive ())
556 if (target
->isInterface ())
558 for (int i
= 0; i
< source
->interface_count
; ++i
)
560 // We use a recursive call because we also need to
561 // check superinterfaces.
562 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
566 source
= source
->getSuperclass ();
568 while (source
!= NULL
);
573 // The `type' class is used to represent a single type in the
580 // For reference types, the representation of the type.
581 ref_intersection
*klass
;
583 // This is used in two situations.
585 // First, when constructing a new object, it is the PC of the
586 // `new' instruction which created the object. We use the special
587 // value UNINIT to mean that this is uninitialized, and the
588 // special value SELF for the case where the current method is
589 // itself the <init> method.
591 // Second, when the key is return_address_type, this holds the PC
592 // of the instruction following the `jsr'.
595 static const int UNINIT
= -2;
596 static const int SELF
= -1;
598 // Basic constructor.
601 key
= unsuitable_type
;
606 // Make a new instance given the type tag. We assume a generic
607 // `reference_type' means Object.
611 // For reference_type, if KLASS==NULL then that means we are
612 // looking for a generic object of any kind, including an
613 // uninitialized reference.
618 // Make a new instance given a class.
619 type (jclass k
, _Jv_BytecodeVerifier
*verifier
)
621 key
= reference_type
;
622 klass
= new ref_intersection (k
, verifier
);
626 // Make a new instance given the name of a class.
627 type (_Jv_Utf8Const
*n
, _Jv_BytecodeVerifier
*verifier
)
629 key
= reference_type
;
630 klass
= new ref_intersection (n
, verifier
);
642 // These operators are required because libgcj can't link in
644 void *operator new[] (size_t bytes
)
646 return _Jv_Malloc (bytes
);
649 void operator delete[] (void *mem
)
654 type
& operator= (type_val k
)
662 type
& operator= (const type
& t
)
670 // Promote a numeric type.
673 if (key
== boolean_type
|| key
== char_type
674 || key
== byte_type
|| key
== short_type
)
679 // Mark this type as the uninitialized result of `new'.
680 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
682 if (key
== reference_type
)
683 key
= uninitialized_reference_type
;
685 verifier
->verify_fail ("internal error in type::uninitialized");
689 // Mark this type as now initialized.
690 void set_initialized (int npc
)
692 if (npc
!= UNINIT
&& pc
== npc
&& key
== uninitialized_reference_type
)
694 key
= reference_type
;
699 // Mark this type as a particular return address.
700 void set_return_address (int npc
)
705 // Return true if this type and type OTHER are considered
706 // mergeable for the purposes of state merging. This is related
707 // to subroutine handling. For this purpose two types are
708 // considered unmergeable if they are both return-addresses but
709 // have different PCs.
710 bool state_mergeable_p (const type
&other
) const
712 return (key
!= return_address_type
713 || other
.key
!= return_address_type
717 // Return true if an object of type K can be assigned to a variable
718 // of type *THIS. Handle various special cases too. Might modify
719 // *THIS or K. Note however that this does not perform numeric
721 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
723 // Any type is compatible with the unsuitable type.
724 if (key
== unsuitable_type
)
727 if (key
< reference_type
|| k
.key
< reference_type
)
730 // The `null' type is convertible to any initialized reference
732 if (key
== null_type
)
733 return k
.key
!= uninitialized_reference_type
;
734 if (k
.key
== null_type
)
735 return key
!= uninitialized_reference_type
;
737 // A special case for a generic reference.
741 verifier
->verify_fail ("programmer error in type::compatible");
743 // An initialized type and an uninitialized type are not
745 if (isinitialized () != k
.isinitialized ())
748 // Two uninitialized objects are compatible if either:
749 // * The PCs are identical, or
750 // * One PC is UNINIT.
751 if (! isinitialized ())
753 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
757 return klass
->compatible(k
.klass
, verifier
);
762 return key
== void_type
;
767 return key
== long_type
|| key
== double_type
;
770 // Return number of stack or local variable slots taken by this
774 return iswide () ? 2 : 1;
777 bool isarray () const
779 // We treat null_type as not an array. This is ok based on the
780 // current uses of this method.
781 if (key
== reference_type
)
782 return klass
->isarray ();
788 return key
== null_type
;
791 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
793 if (key
!= reference_type
)
795 return klass
->isinterface (verifier
);
798 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
800 if (key
!= reference_type
)
802 return klass
->isabstract (verifier
);
805 // Return the element type of an array.
806 type
element_type (_Jv_BytecodeVerifier
*verifier
)
808 if (key
!= reference_type
)
809 verifier
->verify_fail ("programmer error in type::element_type()", -1);
811 jclass k
= klass
->getclass (verifier
)->getComponentType ();
812 if (k
->isPrimitive ())
813 return type (verifier
->get_type_val_for_signature (k
));
814 return type (k
, verifier
);
817 // Return the array type corresponding to an initialized
818 // reference. We could expand this to work for other kinds of
819 // types, but currently we don't need to.
820 type
to_array (_Jv_BytecodeVerifier
*verifier
)
822 if (key
!= reference_type
)
823 verifier
->verify_fail ("internal error in type::to_array()");
825 jclass k
= klass
->getclass (verifier
);
826 return type (_Jv_GetArrayClass (k
, k
->getClassLoaderInternal()),
830 bool isreference () const
832 return key
>= reference_type
;
840 bool isinitialized () const
842 return key
== reference_type
|| key
== null_type
;
845 bool isresolved () const
847 return (key
== reference_type
849 || key
== uninitialized_reference_type
);
852 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
854 // The way this is written, we don't need to check isarray().
855 if (key
!= reference_type
)
856 verifier
->verify_fail ("internal error in verify_dimensions:"
857 " not a reference type");
859 if (klass
->count_dimensions () < ndims
)
860 verifier
->verify_fail ("array type has fewer dimensions"
864 // Merge OLD_TYPE into this. On error throw exception. Return
865 // true if the merge caused a type change.
866 bool merge (type
& old_type
, bool local_semantics
,
867 _Jv_BytecodeVerifier
*verifier
)
869 bool changed
= false;
870 bool refo
= old_type
.isreference ();
871 bool refn
= isreference ();
874 if (old_type
.key
== null_type
)
876 else if (key
== null_type
)
881 else if (isinitialized () != old_type
.isinitialized ())
882 verifier
->verify_fail ("merging initialized and uninitialized types");
885 if (! isinitialized ())
889 else if (old_type
.pc
== UNINIT
)
891 else if (pc
!= old_type
.pc
)
892 verifier
->verify_fail ("merging different uninitialized types");
895 ref_intersection
*merged
= old_type
.klass
->merge (klass
,
904 else if (refo
|| refn
|| key
!= old_type
.key
)
908 // If we already have an `unsuitable' type, then we
909 // don't need to change again.
910 if (key
!= unsuitable_type
)
912 key
= unsuitable_type
;
917 verifier
->verify_fail ("unmergeable type");
923 void print (void) const
928 case boolean_type
: c
= 'Z'; break;
929 case byte_type
: c
= 'B'; break;
930 case char_type
: c
= 'C'; break;
931 case short_type
: c
= 'S'; break;
932 case int_type
: c
= 'I'; break;
933 case long_type
: c
= 'J'; break;
934 case float_type
: c
= 'F'; break;
935 case double_type
: c
= 'D'; break;
936 case void_type
: c
= 'V'; break;
937 case unsuitable_type
: c
= '-'; break;
938 case return_address_type
: c
= 'r'; break;
939 case continuation_type
: c
= '+'; break;
940 case reference_type
: c
= 'L'; break;
941 case null_type
: c
= '@'; break;
942 case uninitialized_reference_type
: c
= 'U'; break;
944 debug_print ("%c", c
);
946 #endif /* VERIFY_DEBUG */
949 // This class holds all the state information we need for a given
953 // The current top of the stack, in terms of slots.
955 // The current depth of the stack. This will be larger than
956 // STACKTOP when wide types are on the stack.
960 // The local variables.
962 // We keep track of the type of `this' specially. This is used to
963 // ensure that an instance initializer invokes another initializer
964 // on `this' before returning. We must keep track of this
965 // specially because otherwise we might be confused by code which
966 // assigns to locals[0] (overwriting `this') and then returns
967 // without really initializing.
970 // The PC for this state. This is only valid on states which are
971 // permanently attached to a given PC. For an object like
972 // `current_state', which is used transiently, this has no
975 // We keep a linked list of all states requiring reverification.
976 // If this is the special value INVALID_STATE then this state is
977 // not on the list. NULL marks the end of the linked list.
980 // NO_NEXT is the PC value meaning that a new state must be
981 // acquired from the verification list.
982 static const int NO_NEXT
= -1;
989 next
= INVALID_STATE
;
992 state (int max_stack
, int max_locals
)
997 stack
= new type
[max_stack
];
998 for (int i
= 0; i
< max_stack
; ++i
)
999 stack
[i
] = unsuitable_type
;
1000 locals
= new type
[max_locals
];
1001 for (int i
= 0; i
< max_locals
; ++i
)
1002 locals
[i
] = unsuitable_type
;
1004 next
= INVALID_STATE
;
1007 state (const state
*orig
, int max_stack
, int max_locals
)
1009 stack
= new type
[max_stack
];
1010 locals
= new type
[max_locals
];
1011 copy (orig
, max_stack
, max_locals
);
1013 next
= INVALID_STATE
;
1024 void *operator new[] (size_t bytes
)
1026 return _Jv_Malloc (bytes
);
1029 void operator delete[] (void *mem
)
1034 void *operator new (size_t bytes
)
1036 return _Jv_Malloc (bytes
);
1039 void operator delete (void *mem
)
1044 void copy (const state
*copy
, int max_stack
, int max_locals
)
1046 stacktop
= copy
->stacktop
;
1047 stackdepth
= copy
->stackdepth
;
1048 for (int i
= 0; i
< max_stack
; ++i
)
1049 stack
[i
] = copy
->stack
[i
];
1050 for (int i
= 0; i
< max_locals
; ++i
)
1051 locals
[i
] = copy
->locals
[i
];
1053 this_type
= copy
->this_type
;
1054 // Don't modify `next' or `pc'.
1057 // Modify this state to reflect entry to an exception handler.
1058 void set_exception (type t
, int max_stack
)
1063 for (int i
= stacktop
; i
< max_stack
; ++i
)
1064 stack
[i
] = unsuitable_type
;
1067 inline int get_pc () const
1072 void set_pc (int npc
)
1077 // Merge STATE_OLD into this state. Destructively modifies this
1078 // state. Returns true if the new state was in fact changed.
1079 // Will throw an exception if the states are not mergeable.
1080 bool merge (state
*state_old
, int max_locals
,
1081 _Jv_BytecodeVerifier
*verifier
)
1083 bool changed
= false;
1085 // Special handling for `this'. If one or the other is
1086 // uninitialized, then the merge is uninitialized.
1087 if (this_type
.isinitialized ())
1088 this_type
= state_old
->this_type
;
1091 if (state_old
->stacktop
!= stacktop
) // FIXME stackdepth instead?
1092 verifier
->verify_fail ("stack sizes differ");
1093 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
1095 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
1099 // Merge local variables.
1100 for (int i
= 0; i
< max_locals
; ++i
)
1102 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
1109 // Throw an exception if there is an uninitialized object on the
1110 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1111 // whether we're using backwards-branch or exception-handing
1113 void check_no_uninitialized_objects (int max_locals
,
1114 _Jv_BytecodeVerifier
*verifier
,
1115 bool exception_semantics
= false)
1117 if (! exception_semantics
)
1119 for (int i
= 0; i
< stacktop
; ++i
)
1120 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1121 verifier
->verify_fail ("uninitialized object on stack");
1124 for (int i
= 0; i
< max_locals
; ++i
)
1125 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1126 verifier
->verify_fail ("uninitialized object in local variable");
1128 check_this_initialized (verifier
);
1131 // Ensure that `this' has been initialized.
1132 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1134 if (this_type
.isreference () && ! this_type
.isinitialized ())
1135 verifier
->verify_fail ("`this' is uninitialized");
1138 // Set type of `this'.
1139 void set_this_type (const type
&k
)
1144 // Mark each `new'd object we know of that was allocated at PC as
1146 void set_initialized (int pc
, int max_locals
)
1148 for (int i
= 0; i
< stacktop
; ++i
)
1149 stack
[i
].set_initialized (pc
);
1150 for (int i
= 0; i
< max_locals
; ++i
)
1151 locals
[i
].set_initialized (pc
);
1152 this_type
.set_initialized (pc
);
1155 // This tests to see whether two states can be considered "merge
1156 // compatible". If both states have a return-address in the same
1157 // slot, and the return addresses are different, then they are not
1158 // compatible and we must not try to merge them.
1159 bool state_mergeable_p (state
*other
, int max_locals
,
1160 _Jv_BytecodeVerifier
*verifier
)
1162 // This is tricky: if the stack sizes differ, then not only are
1163 // these not mergeable, but in fact we should give an error, as
1164 // we've found two execution paths that reach a branch target
1165 // with different stack depths. FIXME stackdepth instead?
1166 if (stacktop
!= other
->stacktop
)
1167 verifier
->verify_fail ("stack sizes differ");
1169 for (int i
= 0; i
< stacktop
; ++i
)
1170 if (! stack
[i
].state_mergeable_p (other
->stack
[i
]))
1172 for (int i
= 0; i
< max_locals
; ++i
)
1173 if (! locals
[i
].state_mergeable_p (other
->locals
[i
]))
1178 void reverify (_Jv_BytecodeVerifier
*verifier
)
1180 if (next
== INVALID_STATE
)
1182 next
= verifier
->next_verify_state
;
1183 verifier
->next_verify_state
= this;
1188 void print (const char *leader
, int pc
,
1189 int max_stack
, int max_locals
) const
1191 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1193 for (i
= 0; i
< stacktop
; ++i
)
1195 for (; i
< max_stack
; ++i
)
1197 debug_print (" [local] ");
1198 for (i
= 0; i
< max_locals
; ++i
)
1200 debug_print (" | %p\n", this);
1203 inline void print (const char *, int, int, int) const
1206 #endif /* VERIFY_DEBUG */
1211 if (current_state
->stacktop
<= 0)
1212 verify_fail ("stack empty");
1213 type r
= current_state
->stack
[--current_state
->stacktop
];
1214 current_state
->stackdepth
-= r
.depth ();
1215 if (current_state
->stackdepth
< 0)
1216 verify_fail ("stack empty", start_PC
);
1222 type r
= pop_raw ();
1224 verify_fail ("narrow pop of wide type");
1228 type
pop_type (type match
)
1231 type t
= pop_raw ();
1232 if (! match
.compatible (t
, this))
1233 verify_fail ("incompatible type on stack");
1237 // Pop a reference which is guaranteed to be initialized. MATCH
1238 // doesn't have to be a reference type; in this case this acts like
1240 type
pop_init_ref (type match
)
1242 type t
= pop_raw ();
1243 if (t
.isreference () && ! t
.isinitialized ())
1244 verify_fail ("initialized reference required");
1245 else if (! match
.compatible (t
, this))
1246 verify_fail ("incompatible type on stack");
1250 // Pop a reference type or a return address.
1251 type
pop_ref_or_return ()
1253 type t
= pop_raw ();
1254 if (! t
.isreference () && t
.key
!= return_address_type
)
1255 verify_fail ("expected reference or return address on stack");
1259 void push_type (type t
)
1261 // If T is a numeric type like short, promote it to int.
1264 int depth
= t
.depth ();
1265 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1266 verify_fail ("stack overflow");
1267 current_state
->stack
[current_state
->stacktop
++] = t
;
1268 current_state
->stackdepth
+= depth
;
1271 void set_variable (int index
, type t
)
1273 // If T is a numeric type like short, promote it to int.
1276 int depth
= t
.depth ();
1277 if (index
> current_method
->max_locals
- depth
)
1278 verify_fail ("invalid local variable");
1279 current_state
->locals
[index
] = t
;
1282 current_state
->locals
[index
+ 1] = continuation_type
;
1283 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1284 current_state
->locals
[index
- 1] = unsuitable_type
;
1287 type
get_variable (int index
, type t
)
1289 int depth
= t
.depth ();
1290 if (index
> current_method
->max_locals
- depth
)
1291 verify_fail ("invalid local variable");
1292 if (! t
.compatible (current_state
->locals
[index
], this))
1293 verify_fail ("incompatible type in local variable");
1296 type
t (continuation_type
);
1297 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1298 verify_fail ("invalid local variable");
1300 return current_state
->locals
[index
];
1303 // Make sure ARRAY is an array type and that its elements are
1304 // compatible with type ELEMENT. Returns the actual element type.
1305 type
require_array_type (type array
, type element
)
1307 // An odd case. Here we just pretend that everything went ok. If
1308 // the requested element type is some kind of reference, return
1309 // the null type instead.
1310 if (array
.isnull ())
1311 return element
.isreference () ? type (null_type
) : element
;
1313 if (! array
.isarray ())
1314 verify_fail ("array required");
1316 type t
= array
.element_type (this);
1317 if (! element
.compatible (t
, this))
1319 // Special case for byte arrays, which must also be boolean
1322 if (element
.key
== byte_type
)
1324 type
e2 (boolean_type
);
1325 ok
= e2
.compatible (t
, this);
1328 verify_fail ("incompatible array element type");
1331 // Return T and not ELEMENT, because T might be specialized.
1337 if (PC
>= current_method
->code_length
)
1338 verify_fail ("premature end of bytecode");
1339 return (jint
) bytecode
[PC
++] & 0xff;
1344 jint b1
= get_byte ();
1345 jint b2
= get_byte ();
1346 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1351 jint b1
= get_byte ();
1352 jint b2
= get_byte ();
1353 jshort s
= (b1
<< 8) | b2
;
1359 jint b1
= get_byte ();
1360 jint b2
= get_byte ();
1361 jint b3
= get_byte ();
1362 jint b4
= get_byte ();
1363 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1366 int compute_jump (int offset
)
1368 int npc
= start_PC
+ offset
;
1369 if (npc
< 0 || npc
>= current_method
->code_length
)
1370 verify_fail ("branch out of range", start_PC
);
1374 // Add a new state to the state list at NPC.
1375 state
*add_new_state (int npc
, state
*old_state
)
1377 state
*new_state
= new state (old_state
, current_method
->max_stack
,
1378 current_method
->max_locals
);
1379 debug_print ("== New state in add_new_state\n");
1380 new_state
->print ("New", npc
, current_method
->max_stack
,
1381 current_method
->max_locals
);
1382 linked
<state
> *nlink
1383 = (linked
<state
> *) _Jv_Malloc (sizeof (linked
<state
>));
1384 nlink
->val
= new_state
;
1385 nlink
->next
= states
[npc
];
1386 states
[npc
] = nlink
;
1387 new_state
->set_pc (npc
);
1391 // Merge the indicated state into the state at the branch target and
1392 // schedule a new PC if there is a change. NPC is the PC of the
1393 // branch target, and FROM_STATE is the state at the source of the
1394 // branch. This method returns true if the destination state
1395 // changed and requires reverification, false otherwise.
1396 void merge_into (int npc
, state
*from_state
)
1398 // Iterate over all target states and merge our state into each,
1399 // if applicable. FIXME one improvement we could make here is
1400 // "state destruction". Merging a new state into an existing one
1401 // might cause a return_address_type to be merged to
1402 // unsuitable_type. In this case the resulting state may now be
1403 // mergeable with other states currently held in parallel at this
1404 // location. So in this situation we could pairwise compare and
1405 // reduce the number of parallel states.
1406 bool applicable
= false;
1407 for (linked
<state
> *iter
= states
[npc
]; iter
!= NULL
; iter
= iter
->next
)
1409 state
*new_state
= iter
->val
;
1410 if (new_state
->state_mergeable_p (from_state
,
1411 current_method
->max_locals
, this))
1415 debug_print ("== Merge states in merge_into\n");
1416 from_state
->print ("Frm", start_PC
, current_method
->max_stack
,
1417 current_method
->max_locals
);
1418 new_state
->print (" To", npc
, current_method
->max_stack
,
1419 current_method
->max_locals
);
1420 bool changed
= new_state
->merge (from_state
,
1421 current_method
->max_locals
,
1423 new_state
->print ("New", npc
, current_method
->max_stack
,
1424 current_method
->max_locals
);
1427 new_state
->reverify (this);
1433 // Either we don't yet have a state at NPC, or we have a
1434 // return-address type that is in conflict with all existing
1435 // state. So, we need to create a new entry.
1436 state
*new_state
= add_new_state (npc
, from_state
);
1437 // A new state added in this way must always be reverified.
1438 new_state
->reverify (this);
1442 void push_jump (int offset
)
1444 int npc
= compute_jump (offset
);
1446 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1447 merge_into (npc
, current_state
);
1450 void push_exception_jump (type t
, int pc
)
1452 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1454 state
s (current_state
, current_method
->max_stack
,
1455 current_method
->max_locals
);
1456 if (current_method
->max_stack
< 1)
1457 verify_fail ("stack overflow at exception handler");
1458 s
.set_exception (t
, current_method
->max_stack
);
1459 merge_into (pc
, &s
);
1464 state
*new_state
= next_verify_state
;
1465 if (new_state
== INVALID_STATE
)
1466 verify_fail ("programmer error in pop_jump");
1467 if (new_state
!= NULL
)
1469 next_verify_state
= new_state
->next
;
1470 new_state
->next
= INVALID_STATE
;
1475 void invalidate_pc ()
1477 PC
= state::NO_NEXT
;
1480 void note_branch_target (int pc
)
1482 // Don't check `pc <= PC', because we've advanced PC after
1483 // fetching the target and we haven't yet checked the next
1485 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1486 verify_fail ("branch not to instruction start", start_PC
);
1487 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1490 void skip_padding ()
1492 while ((PC
% 4) > 0)
1493 if (get_byte () != 0)
1494 verify_fail ("found nonzero padding byte");
1497 // Do the work for a `ret' instruction. INDEX is the index into the
1499 void handle_ret_insn (int index
)
1501 type ret_addr
= get_variable (index
, return_address_type
);
1502 // It would be nice if we could do this. However, the JVM Spec
1503 // doesn't say that this is what happens. It is implied that
1504 // reusing a return address is invalid, but there's no actual
1505 // prohibition against it.
1506 // set_variable (index, unsuitable_type);
1508 int npc
= ret_addr
.get_pc ();
1509 // We might be returning to a `jsr' that is at the end of the
1510 // bytecode. This is ok if we never return from the called
1511 // subroutine, but if we see this here it is an error.
1512 if (npc
>= current_method
->code_length
)
1513 verify_fail ("fell off end");
1516 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1518 merge_into (npc
, current_state
);
1522 void handle_jsr_insn (int offset
)
1524 int npc
= compute_jump (offset
);
1527 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1529 // Modify our state as appropriate for entry into a subroutine.
1530 type
ret_addr (return_address_type
);
1531 ret_addr
.set_return_address (PC
);
1532 push_type (ret_addr
);
1533 merge_into (npc
, current_state
);
1537 jclass
construct_primitive_array_type (type_val prim
)
1543 k
= JvPrimClass (boolean
);
1546 k
= JvPrimClass (char);
1549 k
= JvPrimClass (float);
1552 k
= JvPrimClass (double);
1555 k
= JvPrimClass (byte
);
1558 k
= JvPrimClass (short);
1561 k
= JvPrimClass (int);
1564 k
= JvPrimClass (long);
1567 // These aren't used here but we call them out to avoid
1570 case unsuitable_type
:
1571 case return_address_type
:
1572 case continuation_type
:
1573 case reference_type
:
1575 case uninitialized_reference_type
:
1577 verify_fail ("unknown type in construct_primitive_array_type");
1579 k
= _Jv_GetArrayClass (k
, NULL
);
1583 // This pass computes the location of branch targets and also
1584 // instruction starts.
1585 void branch_prepass ()
1587 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1589 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1593 while (PC
< current_method
->code_length
)
1595 // Set `start_PC' early so that error checking can have the
1598 flags
[PC
] |= FLAG_INSN_START
;
1600 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1604 case op_aconst_null
:
1740 case op_monitorenter
:
1741 case op_monitorexit
:
1749 case op_arraylength
:
1781 case op_invokespecial
:
1782 case op_invokestatic
:
1783 case op_invokevirtual
:
1787 case op_multianewarray
:
1810 note_branch_target (compute_jump (get_short ()));
1813 case op_tableswitch
:
1816 note_branch_target (compute_jump (get_int ()));
1817 jint low
= get_int ();
1818 jint hi
= get_int ();
1820 verify_fail ("invalid tableswitch", start_PC
);
1821 for (int i
= low
; i
<= hi
; ++i
)
1822 note_branch_target (compute_jump (get_int ()));
1826 case op_lookupswitch
:
1829 note_branch_target (compute_jump (get_int ()));
1830 int npairs
= get_int ();
1832 verify_fail ("too few pairs in lookupswitch", start_PC
);
1833 while (npairs
-- > 0)
1836 note_branch_target (compute_jump (get_int ()));
1841 case op_invokeinterface
:
1849 opcode
= (java_opcode
) get_byte ();
1851 if (opcode
== op_iinc
)
1858 note_branch_target (compute_jump (get_int ()));
1861 // These are unused here, but we call them out explicitly
1862 // so that -Wswitch-enum doesn't complain.
1868 case op_putstatic_1
:
1869 case op_putstatic_2
:
1870 case op_putstatic_4
:
1871 case op_putstatic_8
:
1872 case op_putstatic_a
:
1874 case op_getfield_2s
:
1875 case op_getfield_2u
:
1879 case op_getstatic_1
:
1880 case op_getstatic_2s
:
1881 case op_getstatic_2u
:
1882 case op_getstatic_4
:
1883 case op_getstatic_8
:
1884 case op_getstatic_a
:
1886 verify_fail ("unrecognized instruction in branch_prepass",
1890 // See if any previous branch tried to branch to the middle of
1891 // this instruction.
1892 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1894 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1895 verify_fail ("branch to middle of instruction", pc
);
1899 // Verify exception handlers.
1900 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1902 if (! (flags
[exception
[i
].handler_pc
.i
] & FLAG_INSN_START
))
1903 verify_fail ("exception handler not at instruction start",
1904 exception
[i
].handler_pc
.i
);
1905 if (! (flags
[exception
[i
].start_pc
.i
] & FLAG_INSN_START
))
1906 verify_fail ("exception start not at instruction start",
1907 exception
[i
].start_pc
.i
);
1908 if (exception
[i
].end_pc
.i
!= current_method
->code_length
1909 && ! (flags
[exception
[i
].end_pc
.i
] & FLAG_INSN_START
))
1910 verify_fail ("exception end not at instruction start",
1911 exception
[i
].end_pc
.i
);
1913 flags
[exception
[i
].handler_pc
.i
] |= FLAG_BRANCH_TARGET
;
1917 void check_pool_index (int index
)
1919 if (index
< 0 || index
>= current_class
->constants
.size
)
1920 verify_fail ("constant pool index out of range", start_PC
);
1923 type
check_class_constant (int index
)
1925 check_pool_index (index
);
1926 _Jv_Constants
*pool
= ¤t_class
->constants
;
1927 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1928 return type (pool
->data
[index
].clazz
, this);
1929 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1930 return type (pool
->data
[index
].utf8
, this);
1931 verify_fail ("expected class constant", start_PC
);
1934 type
check_constant (int index
)
1936 check_pool_index (index
);
1937 _Jv_Constants
*pool
= ¤t_class
->constants
;
1938 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1939 || pool
->tags
[index
] == JV_CONSTANT_String
)
1940 return type (&java::lang::String::class$
, this);
1941 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1942 return type (int_type
);
1943 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1944 return type (float_type
);
1945 verify_fail ("String, int, or float constant expected", start_PC
);
1948 type
check_wide_constant (int index
)
1950 check_pool_index (index
);
1951 _Jv_Constants
*pool
= ¤t_class
->constants
;
1952 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1953 return type (long_type
);
1954 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1955 return type (double_type
);
1956 verify_fail ("long or double constant expected", start_PC
);
1959 // Helper for both field and method. These are laid out the same in
1960 // the constant pool.
1961 type
handle_field_or_method (int index
, int expected
,
1962 _Jv_Utf8Const
**name
,
1963 _Jv_Utf8Const
**fmtype
)
1965 check_pool_index (index
);
1966 _Jv_Constants
*pool
= ¤t_class
->constants
;
1967 if (pool
->tags
[index
] != expected
)
1968 verify_fail ("didn't see expected constant", start_PC
);
1969 // Once we know we have a Fieldref or Methodref we assume that it
1970 // is correctly laid out in the constant pool. I think the code
1971 // in defineclass.cc guarantees this.
1972 _Jv_ushort class_index
, name_and_type_index
;
1973 _Jv_loadIndexes (&pool
->data
[index
],
1975 name_and_type_index
);
1976 _Jv_ushort name_index
, desc_index
;
1977 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1978 name_index
, desc_index
);
1980 *name
= pool
->data
[name_index
].utf8
;
1981 *fmtype
= pool
->data
[desc_index
].utf8
;
1983 return check_class_constant (class_index
);
1986 // Return field's type, compute class' type if requested.
1987 type
check_field_constant (int index
, type
*class_type
= NULL
)
1989 _Jv_Utf8Const
*name
, *field_type
;
1990 type ct
= handle_field_or_method (index
,
1991 JV_CONSTANT_Fieldref
,
1992 &name
, &field_type
);
1995 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1996 return type (field_type
, this);
1997 return get_type_val_for_signature (field_type
->data
[0]);
2000 type
check_method_constant (int index
, bool is_interface
,
2001 _Jv_Utf8Const
**method_name
,
2002 _Jv_Utf8Const
**method_signature
)
2004 return handle_field_or_method (index
,
2006 ? JV_CONSTANT_InterfaceMethodref
2007 : JV_CONSTANT_Methodref
),
2008 method_name
, method_signature
);
2011 type
get_one_type (char *&p
)
2029 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
2030 return type (name
, this);
2033 // Casting to jchar here is ok since we are looking at an ASCII
2035 type_val rt
= get_type_val_for_signature (jchar (v
));
2037 if (arraycount
== 0)
2039 // Callers of this function eventually push their arguments on
2040 // the stack. So, promote them here.
2041 return type (rt
).promote ();
2044 jclass k
= construct_primitive_array_type (rt
);
2045 while (--arraycount
> 0)
2046 k
= _Jv_GetArrayClass (k
, NULL
);
2047 return type (k
, this);
2050 void compute_argument_types (_Jv_Utf8Const
*signature
,
2053 char *p
= signature
->data
;
2059 types
[i
++] = get_one_type (p
);
2062 type
compute_return_type (_Jv_Utf8Const
*signature
)
2064 char *p
= signature
->data
;
2068 return get_one_type (p
);
2071 void check_return_type (type onstack
)
2073 type rt
= compute_return_type (current_method
->self
->signature
);
2074 if (! rt
.compatible (onstack
, this))
2075 verify_fail ("incompatible return type");
2078 // Initialize the stack for the new method. Returns true if this
2079 // method is an instance initializer.
2080 bool initialize_stack ()
2083 bool is_init
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2085 bool is_clinit
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2088 using namespace java::lang::reflect
;
2089 if (! Modifier::isStatic (current_method
->self
->accflags
))
2091 type
kurr (current_class
, this);
2094 kurr
.set_uninitialized (type::SELF
, this);
2098 verify_fail ("<clinit> method must be static");
2099 set_variable (0, kurr
);
2100 current_state
->set_this_type (kurr
);
2106 verify_fail ("<init> method must be non-static");
2109 // We have to handle wide arguments specially here.
2110 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2111 type arg_types
[arg_count
];
2112 compute_argument_types (current_method
->self
->signature
, arg_types
);
2113 for (int i
= 0; i
< arg_count
; ++i
)
2115 set_variable (var
, arg_types
[i
]);
2117 if (arg_types
[i
].iswide ())
2124 void verify_instructions_0 ()
2126 current_state
= new state (current_method
->max_stack
,
2127 current_method
->max_locals
);
2132 // True if we are verifying an instance initializer.
2133 bool this_is_init
= initialize_stack ();
2135 states
= (linked
<state
> **) _Jv_Malloc (sizeof (linked
<state
> *)
2136 * current_method
->code_length
);
2137 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2140 next_verify_state
= NULL
;
2144 // If the PC was invalidated, get a new one from the work list.
2145 if (PC
== state::NO_NEXT
)
2147 state
*new_state
= pop_jump ();
2148 // If it is null, we're done.
2149 if (new_state
== NULL
)
2152 PC
= new_state
->get_pc ();
2153 debug_print ("== State pop from pending list\n");
2154 // Set up the current state.
2155 current_state
->copy (new_state
, current_method
->max_stack
,
2156 current_method
->max_locals
);
2160 // We only have to do this checking in the situation where
2161 // control flow falls through from the previous
2162 // instruction. Otherwise merging is done at the time we
2164 if (states
[PC
] != NULL
)
2166 // We've already visited this instruction. So merge
2167 // the states together. It is simplest, but not most
2168 // efficient, to just always invalidate the PC here.
2169 merge_into (PC
, current_state
);
2175 // Control can't fall off the end of the bytecode. We need to
2176 // check this in both cases, not just the fall-through case,
2177 // because we don't check to see whether a `jsr' appears at
2178 // the end of the bytecode until we process a `ret'.
2179 if (PC
>= current_method
->code_length
)
2180 verify_fail ("fell off end");
2182 // We only have to keep saved state at branch targets. If
2183 // we're at a branch target and the state here hasn't been set
2184 // yet, we set it now. You might notice that `ret' targets
2185 // won't necessarily have FLAG_BRANCH_TARGET set. This
2186 // doesn't matter, since those states will be filled in by
2188 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2189 add_new_state (PC
, current_state
);
2191 // Set this before handling exceptions so that debug output is
2195 // Update states for all active exception handlers. Ordinarily
2196 // there are not many exception handlers. So we simply run
2197 // through them all.
2198 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2200 if (PC
>= exception
[i
].start_pc
.i
&& PC
< exception
[i
].end_pc
.i
)
2202 type
handler (&java::lang::Throwable::class$
, this);
2203 if (exception
[i
].handler_type
.i
!= 0)
2204 handler
= check_class_constant (exception
[i
].handler_type
.i
);
2205 push_exception_jump (handler
, exception
[i
].handler_pc
.i
);
2209 current_state
->print (" ", PC
, current_method
->max_stack
,
2210 current_method
->max_locals
);
2211 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2217 case op_aconst_null
:
2218 push_type (null_type
);
2228 push_type (int_type
);
2233 push_type (long_type
);
2239 push_type (float_type
);
2244 push_type (double_type
);
2249 push_type (int_type
);
2254 push_type (int_type
);
2258 push_type (check_constant (get_byte ()));
2261 push_type (check_constant (get_ushort ()));
2264 push_type (check_wide_constant (get_ushort ()));
2268 push_type (get_variable (get_byte (), int_type
));
2271 push_type (get_variable (get_byte (), long_type
));
2274 push_type (get_variable (get_byte (), float_type
));
2277 push_type (get_variable (get_byte (), double_type
));
2280 push_type (get_variable (get_byte (), reference_type
));
2287 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2293 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2299 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2305 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2311 push_type (get_variable (opcode
- op_aload_0
, 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 push_type (require_array_type (pop_init_ref (reference_type
),
2339 pop_type (int_type
);
2340 require_array_type (pop_init_ref (reference_type
), byte_type
);
2341 push_type (int_type
);
2344 pop_type (int_type
);
2345 require_array_type (pop_init_ref (reference_type
), char_type
);
2346 push_type (int_type
);
2349 pop_type (int_type
);
2350 require_array_type (pop_init_ref (reference_type
), short_type
);
2351 push_type (int_type
);
2354 set_variable (get_byte (), pop_type (int_type
));
2357 set_variable (get_byte (), pop_type (long_type
));
2360 set_variable (get_byte (), pop_type (float_type
));
2363 set_variable (get_byte (), pop_type (double_type
));
2366 set_variable (get_byte (), pop_ref_or_return ());
2372 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2378 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2384 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2390 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2396 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2399 pop_type (int_type
);
2400 pop_type (int_type
);
2401 require_array_type (pop_init_ref (reference_type
), int_type
);
2404 pop_type (long_type
);
2405 pop_type (int_type
);
2406 require_array_type (pop_init_ref (reference_type
), long_type
);
2409 pop_type (float_type
);
2410 pop_type (int_type
);
2411 require_array_type (pop_init_ref (reference_type
), float_type
);
2414 pop_type (double_type
);
2415 pop_type (int_type
);
2416 require_array_type (pop_init_ref (reference_type
), double_type
);
2419 pop_type (reference_type
);
2420 pop_type (int_type
);
2421 require_array_type (pop_init_ref (reference_type
), reference_type
);
2424 pop_type (int_type
);
2425 pop_type (int_type
);
2426 require_array_type (pop_init_ref (reference_type
), byte_type
);
2429 pop_type (int_type
);
2430 pop_type (int_type
);
2431 require_array_type (pop_init_ref (reference_type
), char_type
);
2434 pop_type (int_type
);
2435 pop_type (int_type
);
2436 require_array_type (pop_init_ref (reference_type
), short_type
);
2443 type t
= pop_raw ();
2467 type t2
= pop_raw ();
2482 type t
= pop_raw ();
2497 type t1
= pop_raw ();
2514 type t1
= pop_raw ();
2517 type t2
= pop_raw ();
2535 type t3
= pop_raw ();
2573 pop_type (int_type
);
2574 push_type (pop_type (int_type
));
2584 pop_type (long_type
);
2585 push_type (pop_type (long_type
));
2590 pop_type (int_type
);
2591 push_type (pop_type (long_type
));
2598 pop_type (float_type
);
2599 push_type (pop_type (float_type
));
2606 pop_type (double_type
);
2607 push_type (pop_type (double_type
));
2613 push_type (pop_type (int_type
));
2616 push_type (pop_type (long_type
));
2619 push_type (pop_type (float_type
));
2622 push_type (pop_type (double_type
));
2625 get_variable (get_byte (), int_type
);
2629 pop_type (int_type
);
2630 push_type (long_type
);
2633 pop_type (int_type
);
2634 push_type (float_type
);
2637 pop_type (int_type
);
2638 push_type (double_type
);
2641 pop_type (long_type
);
2642 push_type (int_type
);
2645 pop_type (long_type
);
2646 push_type (float_type
);
2649 pop_type (long_type
);
2650 push_type (double_type
);
2653 pop_type (float_type
);
2654 push_type (int_type
);
2657 pop_type (float_type
);
2658 push_type (long_type
);
2661 pop_type (float_type
);
2662 push_type (double_type
);
2665 pop_type (double_type
);
2666 push_type (int_type
);
2669 pop_type (double_type
);
2670 push_type (long_type
);
2673 pop_type (double_type
);
2674 push_type (float_type
);
2677 pop_type (long_type
);
2678 pop_type (long_type
);
2679 push_type (int_type
);
2683 pop_type (float_type
);
2684 pop_type (float_type
);
2685 push_type (int_type
);
2689 pop_type (double_type
);
2690 pop_type (double_type
);
2691 push_type (int_type
);
2699 pop_type (int_type
);
2700 push_jump (get_short ());
2708 pop_type (int_type
);
2709 pop_type (int_type
);
2710 push_jump (get_short ());
2714 pop_type (reference_type
);
2715 pop_type (reference_type
);
2716 push_jump (get_short ());
2719 push_jump (get_short ());
2723 handle_jsr_insn (get_short ());
2726 handle_ret_insn (get_byte ());
2728 case op_tableswitch
:
2730 pop_type (int_type
);
2732 push_jump (get_int ());
2733 jint low
= get_int ();
2734 jint high
= get_int ();
2735 // Already checked LOW -vs- HIGH.
2736 for (int i
= low
; i
<= high
; ++i
)
2737 push_jump (get_int ());
2742 case op_lookupswitch
:
2744 pop_type (int_type
);
2746 push_jump (get_int ());
2747 jint npairs
= get_int ();
2748 // Already checked NPAIRS >= 0.
2750 for (int i
= 0; i
< npairs
; ++i
)
2752 jint key
= get_int ();
2753 if (i
> 0 && key
<= lastkey
)
2754 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2756 push_jump (get_int ());
2762 check_return_type (pop_type (int_type
));
2766 check_return_type (pop_type (long_type
));
2770 check_return_type (pop_type (float_type
));
2774 check_return_type (pop_type (double_type
));
2778 check_return_type (pop_init_ref (reference_type
));
2782 // We only need to check this when the return type is
2783 // void, because all instance initializers return void.
2785 current_state
->check_this_initialized (this);
2786 check_return_type (void_type
);
2790 push_type (check_field_constant (get_ushort ()));
2793 pop_type (check_field_constant (get_ushort ()));
2798 type field
= check_field_constant (get_ushort (), &klass
);
2806 type field
= check_field_constant (get_ushort (), &klass
);
2809 // We have an obscure special case here: we can use
2810 // `putfield' on a field declared in this class, even if
2811 // `this' has not yet been initialized.
2812 if (! current_state
->this_type
.isinitialized ()
2813 && current_state
->this_type
.pc
== type::SELF
)
2814 klass
.set_uninitialized (type::SELF
, this);
2819 case op_invokevirtual
:
2820 case op_invokespecial
:
2821 case op_invokestatic
:
2822 case op_invokeinterface
:
2824 _Jv_Utf8Const
*method_name
, *method_signature
;
2826 = check_method_constant (get_ushort (),
2827 opcode
== op_invokeinterface
,
2830 // NARGS is only used when we're processing
2831 // invokeinterface. It is simplest for us to compute it
2832 // here and then verify it later.
2834 if (opcode
== op_invokeinterface
)
2836 nargs
= get_byte ();
2837 if (get_byte () != 0)
2838 verify_fail ("invokeinterface dummy byte is wrong");
2841 bool is_init
= false;
2842 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2845 if (opcode
!= op_invokespecial
)
2846 verify_fail ("can't invoke <init>");
2848 else if (method_name
->data
[0] == '<')
2849 verify_fail ("can't invoke method starting with `<'");
2851 // Pop arguments and check types.
2852 int arg_count
= _Jv_count_arguments (method_signature
);
2853 type arg_types
[arg_count
];
2854 compute_argument_types (method_signature
, arg_types
);
2855 for (int i
= arg_count
- 1; i
>= 0; --i
)
2857 // This is only used for verifying the byte for
2859 nargs
-= arg_types
[i
].depth ();
2860 pop_init_ref (arg_types
[i
]);
2863 if (opcode
== op_invokeinterface
2865 verify_fail ("wrong argument count for invokeinterface");
2867 if (opcode
!= op_invokestatic
)
2869 type t
= class_type
;
2872 // In this case the PC doesn't matter.
2873 t
.set_uninitialized (type::UNINIT
, this);
2874 // FIXME: check to make sure that the <init>
2875 // call is to the right class.
2876 // It must either be super or an exact class
2879 type raw
= pop_raw ();
2880 if (! t
.compatible (raw
, this))
2881 verify_fail ("incompatible type on stack");
2884 current_state
->set_initialized (raw
.get_pc (),
2885 current_method
->max_locals
);
2888 type rt
= compute_return_type (method_signature
);
2896 type t
= check_class_constant (get_ushort ());
2897 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2898 verify_fail ("type is array, interface, or abstract");
2899 t
.set_uninitialized (start_PC
, this);
2906 int atype
= get_byte ();
2907 // We intentionally have chosen constants to make this
2909 if (atype
< boolean_type
|| atype
> long_type
)
2910 verify_fail ("type not primitive", start_PC
);
2911 pop_type (int_type
);
2912 type
t (construct_primitive_array_type (type_val (atype
)), this);
2917 pop_type (int_type
);
2918 push_type (check_class_constant (get_ushort ()).to_array (this));
2920 case op_arraylength
:
2922 type t
= pop_init_ref (reference_type
);
2923 if (! t
.isarray () && ! t
.isnull ())
2924 verify_fail ("array type expected");
2925 push_type (int_type
);
2929 pop_type (type (&java::lang::Throwable::class$
, this));
2933 pop_init_ref (reference_type
);
2934 push_type (check_class_constant (get_ushort ()));
2937 pop_init_ref (reference_type
);
2938 check_class_constant (get_ushort ());
2939 push_type (int_type
);
2941 case op_monitorenter
:
2942 pop_init_ref (reference_type
);
2944 case op_monitorexit
:
2945 pop_init_ref (reference_type
);
2949 switch (get_byte ())
2952 push_type (get_variable (get_ushort (), int_type
));
2955 push_type (get_variable (get_ushort (), long_type
));
2958 push_type (get_variable (get_ushort (), float_type
));
2961 push_type (get_variable (get_ushort (), double_type
));
2964 push_type (get_variable (get_ushort (), reference_type
));
2967 set_variable (get_ushort (), pop_type (int_type
));
2970 set_variable (get_ushort (), pop_type (long_type
));
2973 set_variable (get_ushort (), pop_type (float_type
));
2976 set_variable (get_ushort (), pop_type (double_type
));
2979 set_variable (get_ushort (), pop_init_ref (reference_type
));
2982 handle_ret_insn (get_short ());
2985 get_variable (get_ushort (), int_type
);
2989 verify_fail ("unrecognized wide instruction", start_PC
);
2993 case op_multianewarray
:
2995 type atype
= check_class_constant (get_ushort ());
2996 int dim
= get_byte ();
2998 verify_fail ("too few dimensions to multianewarray", start_PC
);
2999 atype
.verify_dimensions (dim
, this);
3000 for (int i
= 0; i
< dim
; ++i
)
3001 pop_type (int_type
);
3007 pop_type (reference_type
);
3008 push_jump (get_short ());
3011 push_jump (get_int ());
3015 handle_jsr_insn (get_int ());
3018 // These are unused here, but we call them out explicitly
3019 // so that -Wswitch-enum doesn't complain.
3025 case op_putstatic_1
:
3026 case op_putstatic_2
:
3027 case op_putstatic_4
:
3028 case op_putstatic_8
:
3029 case op_putstatic_a
:
3031 case op_getfield_2s
:
3032 case op_getfield_2u
:
3036 case op_getstatic_1
:
3037 case op_getstatic_2s
:
3038 case op_getstatic_2u
:
3039 case op_getstatic_4
:
3040 case op_getstatic_8
:
3041 case op_getstatic_a
:
3043 // Unrecognized opcode.
3044 verify_fail ("unrecognized instruction in verify_instructions_0",
3052 void verify_instructions ()
3055 verify_instructions_0 ();
3058 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3060 // We just print the text as utf-8. This is just for debugging
3062 debug_print ("--------------------------------\n");
3063 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
3066 bytecode
= m
->bytecode ();
3067 exception
= m
->exceptions ();
3068 current_class
= m
->defining_class
;
3076 ~_Jv_BytecodeVerifier ()
3081 while (utf8_list
!= NULL
)
3083 linked
<_Jv_Utf8Const
> *n
= utf8_list
->next
;
3084 _Jv_Free (utf8_list
->val
);
3085 _Jv_Free (utf8_list
);
3089 while (isect_list
!= NULL
)
3091 ref_intersection
*next
= isect_list
->alloc_next
;
3098 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3100 linked
<state
> *iter
= states
[i
];
3101 while (iter
!= NULL
)
3103 linked
<state
> *next
= iter
->next
;
3115 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3117 _Jv_BytecodeVerifier
v (meth
);
3118 v
.verify_instructions ();
3121 #endif /* INTERPRETER */