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.
183 linked
<_Jv_Utf8Const
> *utf8_list
;
185 // A linked list of all ref_intersection objects we allocate.
186 ref_intersection
*isect_list
;
188 // Create a new Utf-8 constant and return it. We do this to avoid
189 // having our Utf-8 constants prematurely collected.
190 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
192 linked
<_Jv_Utf8Const
> *lu
= (linked
<_Jv_Utf8Const
> *)
193 _Jv_Malloc (sizeof (linked
<_Jv_Utf8Const
>)
194 + _Jv_Utf8Const::space_needed(s
, len
));
195 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) (lu
+ 1);
198 lu
->next
= utf8_list
;
204 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
206 using namespace java::lang
;
207 StringBuffer
*buf
= new StringBuffer ();
209 buf
->append (JvNewStringLatin1 ("verification failed"));
214 buf
->append (JvNewStringLatin1 (" at PC "));
218 _Jv_InterpMethod
*method
= current_method
;
219 buf
->append (JvNewStringLatin1 (" in "));
220 buf
->append (current_class
->getName());
221 buf
->append ((jchar
) ':');
222 buf
->append (method
->get_method()->name
->toString());
223 buf
->append ((jchar
) '(');
224 buf
->append (method
->get_method()->signature
->toString());
225 buf
->append ((jchar
) ')');
227 buf
->append (JvNewStringLatin1 (": "));
228 buf
->append (JvNewStringLatin1 (s
));
229 throw new java::lang::VerifyError (buf
->toString ());
232 // This enum holds a list of tags for all the different types we
233 // need to handle. Reference types are treated specially by the
239 // The values for primitive types are chosen to correspond to values
240 // specified to newarray.
250 // Used when overwriting second word of a double or long in the
251 // local variables. Also used after merging local variable states
252 // to indicate an unusable value.
255 // This is the second word of a two-word value, i.e., a double or
259 // Everything after `reference_type' must be a reference type.
262 uninitialized_reference_type
265 // This represents a merged class type. Some verifiers (including
266 // earlier versions of this one) will compute the intersection of
267 // two class types when merging states. However, this loses
268 // critical information about interfaces implemented by the various
269 // classes. So instead we keep track of all the actual classes that
271 struct ref_intersection
273 // Whether or not this type has been resolved.
279 // For a resolved reference type, this is a pointer to the class.
281 // For other reference types, this it the name of the class.
285 // Link to the next reference in the intersection.
286 ref_intersection
*ref_next
;
288 // This is used to keep track of all the allocated
289 // ref_intersection objects, so we can free them.
290 // FIXME: we should allocate these in chunks.
291 ref_intersection
*alloc_next
;
293 ref_intersection (jclass klass
, _Jv_BytecodeVerifier
*verifier
)
298 alloc_next
= verifier
->isect_list
;
299 verifier
->isect_list
= this;
302 ref_intersection (_Jv_Utf8Const
*name
, _Jv_BytecodeVerifier
*verifier
)
307 alloc_next
= verifier
->isect_list
;
308 verifier
->isect_list
= this;
311 ref_intersection (ref_intersection
*dup
, ref_intersection
*tail
,
312 _Jv_BytecodeVerifier
*verifier
)
315 is_resolved
= dup
->is_resolved
;
317 alloc_next
= verifier
->isect_list
;
318 verifier
->isect_list
= this;
321 bool equals (ref_intersection
*other
, _Jv_BytecodeVerifier
*verifier
)
323 if (! is_resolved
&& ! other
->is_resolved
324 && _Jv_equalUtf8Consts (data
.name
, other
->data
.name
))
328 if (! other
->is_resolved
)
329 other
->resolve (verifier
);
330 return data
.klass
== other
->data
.klass
;
333 // Merge THIS type into OTHER, returning the result. This will
334 // return OTHER if all the classes in THIS already appear in
336 ref_intersection
*merge (ref_intersection
*other
,
337 _Jv_BytecodeVerifier
*verifier
)
339 ref_intersection
*tail
= other
;
340 for (ref_intersection
*self
= this; self
!= NULL
; self
= self
->ref_next
)
343 for (ref_intersection
*iter
= other
; iter
!= NULL
;
344 iter
= iter
->ref_next
)
346 if (iter
->equals (self
, verifier
))
354 tail
= new ref_intersection (self
, tail
, verifier
);
359 void resolve (_Jv_BytecodeVerifier
*verifier
)
364 using namespace java::lang
;
365 java::lang::ClassLoader
*loader
366 = verifier
->current_class
->getClassLoaderInternal();
367 // We might see either kind of name. Sigh.
368 if (data
.name
->first() == 'L' && data
.name
->limit()[-1] == ';')
369 data
.klass
= _Jv_FindClassFromSignature (data
.name
->chars(), loader
);
371 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
376 // See if an object of type OTHER can be assigned to an object of
377 // type *THIS. This might resolve classes in one chain or the
379 bool compatible (ref_intersection
*other
,
380 _Jv_BytecodeVerifier
*verifier
)
382 ref_intersection
*self
= this;
384 for (; self
!= NULL
; self
= self
->ref_next
)
386 ref_intersection
*other_iter
= other
;
388 for (; other_iter
!= NULL
; other_iter
= other_iter
->ref_next
)
390 // Avoid resolving if possible.
391 if (! self
->is_resolved
392 && ! other_iter
->is_resolved
393 && _Jv_equalUtf8Consts (self
->data
.name
,
394 other_iter
->data
.name
))
397 if (! self
->is_resolved
)
398 self
->resolve(verifier
);
399 if (! other_iter
->is_resolved
)
400 other_iter
->resolve(verifier
);
402 if (! is_assignable_from_slow (self
->data
.klass
,
403 other_iter
->data
.klass
))
413 // assert (ref_next == NULL);
415 return data
.klass
->isArray ();
417 return data
.name
->first() == '[';
420 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
422 // assert (ref_next == NULL);
425 return data
.klass
->isInterface ();
428 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
430 // assert (ref_next == NULL);
433 using namespace java::lang::reflect
;
434 return Modifier::isAbstract (data
.klass
->getModifiers ());
437 jclass
getclass (_Jv_BytecodeVerifier
*verifier
)
444 int count_dimensions ()
449 jclass k
= data
.klass
;
450 while (k
->isArray ())
452 k
= k
->getComponentType ();
458 char *p
= data
.name
->chars();
465 void *operator new (size_t bytes
)
467 return _Jv_Malloc (bytes
);
470 void operator delete (void *mem
)
476 // Return the type_val corresponding to a primitive signature
477 // character. For instance `I' returns `int.class'.
478 type_val
get_type_val_for_signature (jchar sig
)
511 verify_fail ("invalid signature");
516 // Return the type_val corresponding to a primitive class.
517 type_val
get_type_val_for_signature (jclass k
)
519 return get_type_val_for_signature ((jchar
) k
->method_count
);
522 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
523 // TARGET haven't been prepared.
524 static bool is_assignable_from_slow (jclass target
, jclass source
)
526 // First, strip arrays.
527 while (target
->isArray ())
529 // If target is array, source must be as well.
530 if (! source
->isArray ())
532 target
= target
->getComponentType ();
533 source
= source
->getComponentType ();
537 if (target
== &java::lang::Object::class$
)
542 if (source
== target
)
545 if (target
->isPrimitive () || source
->isPrimitive ())
548 if (target
->isInterface ())
550 for (int i
= 0; i
< source
->interface_count
; ++i
)
552 // We use a recursive call because we also need to
553 // check superinterfaces.
554 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
558 source
= source
->getSuperclass ();
560 while (source
!= NULL
);
565 // The `type' class is used to represent a single type in the
572 // For reference types, the representation of the type.
573 ref_intersection
*klass
;
575 // This is used in two situations.
577 // First, when constructing a new object, it is the PC of the
578 // `new' instruction which created the object. We use the special
579 // value UNINIT to mean that this is uninitialized, and the
580 // special value SELF for the case where the current method is
581 // itself the <init> method.
583 // Second, when the key is return_address_type, this holds the PC
584 // of the instruction following the `jsr'.
587 static const int UNINIT
= -2;
588 static const int SELF
= -1;
590 // Basic constructor.
593 key
= unsuitable_type
;
598 // Make a new instance given the type tag. We assume a generic
599 // `reference_type' means Object.
603 // For reference_type, if KLASS==NULL then that means we are
604 // looking for a generic object of any kind, including an
605 // uninitialized reference.
610 // Make a new instance given a class.
611 type (jclass k
, _Jv_BytecodeVerifier
*verifier
)
613 key
= reference_type
;
614 klass
= new ref_intersection (k
, verifier
);
618 // Make a new instance given the name of a class.
619 type (_Jv_Utf8Const
*n
, _Jv_BytecodeVerifier
*verifier
)
621 key
= reference_type
;
622 klass
= new ref_intersection (n
, verifier
);
634 // These operators are required because libgcj can't link in
636 void *operator new[] (size_t bytes
)
638 return _Jv_Malloc (bytes
);
641 void operator delete[] (void *mem
)
646 type
& operator= (type_val k
)
654 type
& operator= (const type
& t
)
662 // Promote a numeric type.
665 if (key
== boolean_type
|| key
== char_type
666 || key
== byte_type
|| key
== short_type
)
671 // Mark this type as the uninitialized result of `new'.
672 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
674 if (key
== reference_type
)
675 key
= uninitialized_reference_type
;
677 verifier
->verify_fail ("internal error in type::uninitialized");
681 // Mark this type as now initialized.
682 void set_initialized (int npc
)
684 if (npc
!= UNINIT
&& pc
== npc
&& key
== uninitialized_reference_type
)
686 key
= reference_type
;
691 // Mark this type as a particular return address.
692 void set_return_address (int npc
)
697 // Return true if this type and type OTHER are considered
698 // mergeable for the purposes of state merging. This is related
699 // to subroutine handling. For this purpose two types are
700 // considered unmergeable if they are both return-addresses but
701 // have different PCs.
702 bool state_mergeable_p (const type
&other
) const
704 return (key
!= return_address_type
705 || other
.key
!= return_address_type
709 // Return true if an object of type K can be assigned to a variable
710 // of type *THIS. Handle various special cases too. Might modify
711 // *THIS or K. Note however that this does not perform numeric
713 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
715 // Any type is compatible with the unsuitable type.
716 if (key
== unsuitable_type
)
719 if (key
< reference_type
|| k
.key
< reference_type
)
722 // The `null' type is convertible to any initialized reference
724 if (key
== null_type
)
725 return k
.key
!= uninitialized_reference_type
;
726 if (k
.key
== null_type
)
727 return key
!= uninitialized_reference_type
;
729 // A special case for a generic reference.
733 verifier
->verify_fail ("programmer error in type::compatible");
735 // An initialized type and an uninitialized type are not
737 if (isinitialized () != k
.isinitialized ())
740 // Two uninitialized objects are compatible if either:
741 // * The PCs are identical, or
742 // * One PC is UNINIT.
743 if (! isinitialized ())
745 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
749 return klass
->compatible(k
.klass
, verifier
);
754 return key
== void_type
;
759 return key
== long_type
|| key
== double_type
;
762 // Return number of stack or local variable slots taken by this
766 return iswide () ? 2 : 1;
769 bool isarray () const
771 // We treat null_type as not an array. This is ok based on the
772 // current uses of this method.
773 if (key
== reference_type
)
774 return klass
->isarray ();
780 return key
== null_type
;
783 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
785 if (key
!= reference_type
)
787 return klass
->isinterface (verifier
);
790 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
792 if (key
!= reference_type
)
794 return klass
->isabstract (verifier
);
797 // Return the element type of an array.
798 type
element_type (_Jv_BytecodeVerifier
*verifier
)
800 if (key
!= reference_type
)
801 verifier
->verify_fail ("programmer error in type::element_type()", -1);
803 jclass k
= klass
->getclass (verifier
)->getComponentType ();
804 if (k
->isPrimitive ())
805 return type (verifier
->get_type_val_for_signature (k
));
806 return type (k
, verifier
);
809 // Return the array type corresponding to an initialized
810 // reference. We could expand this to work for other kinds of
811 // types, but currently we don't need to.
812 type
to_array (_Jv_BytecodeVerifier
*verifier
)
814 if (key
!= reference_type
)
815 verifier
->verify_fail ("internal error in type::to_array()");
817 jclass k
= klass
->getclass (verifier
);
818 return type (_Jv_GetArrayClass (k
, k
->getClassLoaderInternal()),
822 bool isreference () const
824 return key
>= reference_type
;
832 bool isinitialized () const
834 return key
== reference_type
|| key
== null_type
;
837 bool isresolved () const
839 return (key
== reference_type
841 || key
== uninitialized_reference_type
);
844 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
846 // The way this is written, we don't need to check isarray().
847 if (key
!= reference_type
)
848 verifier
->verify_fail ("internal error in verify_dimensions:"
849 " not a reference type");
851 if (klass
->count_dimensions () < ndims
)
852 verifier
->verify_fail ("array type has fewer dimensions"
856 // Merge OLD_TYPE into this. On error throw exception. Return
857 // true if the merge caused a type change.
858 bool merge (type
& old_type
, bool local_semantics
,
859 _Jv_BytecodeVerifier
*verifier
)
861 bool changed
= false;
862 bool refo
= old_type
.isreference ();
863 bool refn
= isreference ();
866 if (old_type
.key
== null_type
)
868 else if (key
== null_type
)
873 else if (isinitialized () != old_type
.isinitialized ())
874 verifier
->verify_fail ("merging initialized and uninitialized types");
877 if (! isinitialized ())
881 else if (old_type
.pc
== UNINIT
)
883 else if (pc
!= old_type
.pc
)
884 verifier
->verify_fail ("merging different uninitialized types");
887 ref_intersection
*merged
= old_type
.klass
->merge (klass
,
896 else if (refo
|| refn
|| key
!= old_type
.key
)
900 // If we already have an `unsuitable' type, then we
901 // don't need to change again.
902 if (key
!= unsuitable_type
)
904 key
= unsuitable_type
;
909 verifier
->verify_fail ("unmergeable type");
915 void print (void) const
920 case boolean_type
: c
= 'Z'; break;
921 case byte_type
: c
= 'B'; break;
922 case char_type
: c
= 'C'; break;
923 case short_type
: c
= 'S'; break;
924 case int_type
: c
= 'I'; break;
925 case long_type
: c
= 'J'; break;
926 case float_type
: c
= 'F'; break;
927 case double_type
: c
= 'D'; break;
928 case void_type
: c
= 'V'; break;
929 case unsuitable_type
: c
= '-'; break;
930 case return_address_type
: c
= 'r'; break;
931 case continuation_type
: c
= '+'; break;
932 case reference_type
: c
= 'L'; break;
933 case null_type
: c
= '@'; break;
934 case uninitialized_reference_type
: c
= 'U'; break;
936 debug_print ("%c", c
);
938 #endif /* VERIFY_DEBUG */
941 // This class holds all the state information we need for a given
945 // The current top of the stack, in terms of slots.
947 // The current depth of the stack. This will be larger than
948 // STACKTOP when wide types are on the stack.
952 // The local variables.
954 // We keep track of the type of `this' specially. This is used to
955 // ensure that an instance initializer invokes another initializer
956 // on `this' before returning. We must keep track of this
957 // specially because otherwise we might be confused by code which
958 // assigns to locals[0] (overwriting `this') and then returns
959 // without really initializing.
962 // The PC for this state. This is only valid on states which are
963 // permanently attached to a given PC. For an object like
964 // `current_state', which is used transiently, this has no
967 // We keep a linked list of all states requiring reverification.
968 // If this is the special value INVALID_STATE then this state is
969 // not on the list. NULL marks the end of the linked list.
972 // NO_NEXT is the PC value meaning that a new state must be
973 // acquired from the verification list.
974 static const int NO_NEXT
= -1;
981 next
= INVALID_STATE
;
984 state (int max_stack
, int max_locals
)
989 stack
= new type
[max_stack
];
990 for (int i
= 0; i
< max_stack
; ++i
)
991 stack
[i
] = unsuitable_type
;
992 locals
= new type
[max_locals
];
993 for (int i
= 0; i
< max_locals
; ++i
)
994 locals
[i
] = unsuitable_type
;
996 next
= INVALID_STATE
;
999 state (const state
*orig
, int max_stack
, int max_locals
)
1001 stack
= new type
[max_stack
];
1002 locals
= new type
[max_locals
];
1003 copy (orig
, max_stack
, max_locals
);
1005 next
= INVALID_STATE
;
1016 void *operator new[] (size_t bytes
)
1018 return _Jv_Malloc (bytes
);
1021 void operator delete[] (void *mem
)
1026 void *operator new (size_t bytes
)
1028 return _Jv_Malloc (bytes
);
1031 void operator delete (void *mem
)
1036 void copy (const state
*copy
, int max_stack
, int max_locals
)
1038 stacktop
= copy
->stacktop
;
1039 stackdepth
= copy
->stackdepth
;
1040 for (int i
= 0; i
< max_stack
; ++i
)
1041 stack
[i
] = copy
->stack
[i
];
1042 for (int i
= 0; i
< max_locals
; ++i
)
1043 locals
[i
] = copy
->locals
[i
];
1045 this_type
= copy
->this_type
;
1046 // Don't modify `next' or `pc'.
1049 // Modify this state to reflect entry to an exception handler.
1050 void set_exception (type t
, int max_stack
)
1055 for (int i
= stacktop
; i
< max_stack
; ++i
)
1056 stack
[i
] = unsuitable_type
;
1059 inline int get_pc () const
1064 void set_pc (int npc
)
1069 // Merge STATE_OLD into this state. Destructively modifies this
1070 // state. Returns true if the new state was in fact changed.
1071 // Will throw an exception if the states are not mergeable.
1072 bool merge (state
*state_old
, int max_locals
,
1073 _Jv_BytecodeVerifier
*verifier
)
1075 bool changed
= false;
1077 // Special handling for `this'. If one or the other is
1078 // uninitialized, then the merge is uninitialized.
1079 if (this_type
.isinitialized ())
1080 this_type
= state_old
->this_type
;
1083 if (state_old
->stacktop
!= stacktop
) // FIXME stackdepth instead?
1084 verifier
->verify_fail ("stack sizes differ");
1085 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
1087 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
1091 // Merge local variables.
1092 for (int i
= 0; i
< max_locals
; ++i
)
1094 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
1101 // Throw an exception if there is an uninitialized object on the
1102 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1103 // whether we're using backwards-branch or exception-handing
1105 void check_no_uninitialized_objects (int max_locals
,
1106 _Jv_BytecodeVerifier
*verifier
,
1107 bool exception_semantics
= false)
1109 if (! exception_semantics
)
1111 for (int i
= 0; i
< stacktop
; ++i
)
1112 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1113 verifier
->verify_fail ("uninitialized object on stack");
1116 for (int i
= 0; i
< max_locals
; ++i
)
1117 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1118 verifier
->verify_fail ("uninitialized object in local variable");
1120 check_this_initialized (verifier
);
1123 // Ensure that `this' has been initialized.
1124 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1126 if (this_type
.isreference () && ! this_type
.isinitialized ())
1127 verifier
->verify_fail ("`this' is uninitialized");
1130 // Set type of `this'.
1131 void set_this_type (const type
&k
)
1136 // Mark each `new'd object we know of that was allocated at PC as
1138 void set_initialized (int pc
, int max_locals
)
1140 for (int i
= 0; i
< stacktop
; ++i
)
1141 stack
[i
].set_initialized (pc
);
1142 for (int i
= 0; i
< max_locals
; ++i
)
1143 locals
[i
].set_initialized (pc
);
1144 this_type
.set_initialized (pc
);
1147 // This tests to see whether two states can be considered "merge
1148 // compatible". If both states have a return-address in the same
1149 // slot, and the return addresses are different, then they are not
1150 // compatible and we must not try to merge them.
1151 bool state_mergeable_p (state
*other
, int max_locals
,
1152 _Jv_BytecodeVerifier
*verifier
)
1154 // This is tricky: if the stack sizes differ, then not only are
1155 // these not mergeable, but in fact we should give an error, as
1156 // we've found two execution paths that reach a branch target
1157 // with different stack depths. FIXME stackdepth instead?
1158 if (stacktop
!= other
->stacktop
)
1159 verifier
->verify_fail ("stack sizes differ");
1161 for (int i
= 0; i
< stacktop
; ++i
)
1162 if (! stack
[i
].state_mergeable_p (other
->stack
[i
]))
1164 for (int i
= 0; i
< max_locals
; ++i
)
1165 if (! locals
[i
].state_mergeable_p (other
->locals
[i
]))
1170 void reverify (_Jv_BytecodeVerifier
*verifier
)
1172 if (next
== INVALID_STATE
)
1174 next
= verifier
->next_verify_state
;
1175 verifier
->next_verify_state
= this;
1180 void print (const char *leader
, int pc
,
1181 int max_stack
, int max_locals
) const
1183 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1185 for (i
= 0; i
< stacktop
; ++i
)
1187 for (; i
< max_stack
; ++i
)
1189 debug_print (" [local] ");
1190 for (i
= 0; i
< max_locals
; ++i
)
1192 debug_print (" | %p\n", this);
1195 inline void print (const char *, int, int, int) const
1198 #endif /* VERIFY_DEBUG */
1203 if (current_state
->stacktop
<= 0)
1204 verify_fail ("stack empty");
1205 type r
= current_state
->stack
[--current_state
->stacktop
];
1206 current_state
->stackdepth
-= r
.depth ();
1207 if (current_state
->stackdepth
< 0)
1208 verify_fail ("stack empty", start_PC
);
1214 type r
= pop_raw ();
1216 verify_fail ("narrow pop of wide type");
1220 type
pop_type (type match
)
1223 type t
= pop_raw ();
1224 if (! match
.compatible (t
, this))
1225 verify_fail ("incompatible type on stack");
1229 // Pop a reference which is guaranteed to be initialized. MATCH
1230 // doesn't have to be a reference type; in this case this acts like
1232 type
pop_init_ref (type match
)
1234 type t
= pop_raw ();
1235 if (t
.isreference () && ! t
.isinitialized ())
1236 verify_fail ("initialized reference required");
1237 else if (! match
.compatible (t
, this))
1238 verify_fail ("incompatible type on stack");
1242 // Pop a reference type or a return address.
1243 type
pop_ref_or_return ()
1245 type t
= pop_raw ();
1246 if (! t
.isreference () && t
.key
!= return_address_type
)
1247 verify_fail ("expected reference or return address on stack");
1251 void push_type (type t
)
1253 // If T is a numeric type like short, promote it to int.
1256 int depth
= t
.depth ();
1257 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1258 verify_fail ("stack overflow");
1259 current_state
->stack
[current_state
->stacktop
++] = t
;
1260 current_state
->stackdepth
+= depth
;
1263 void set_variable (int index
, type t
)
1265 // If T is a numeric type like short, promote it to int.
1268 int depth
= t
.depth ();
1269 if (index
> current_method
->max_locals
- depth
)
1270 verify_fail ("invalid local variable");
1271 current_state
->locals
[index
] = t
;
1274 current_state
->locals
[index
+ 1] = continuation_type
;
1275 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1276 current_state
->locals
[index
- 1] = unsuitable_type
;
1279 type
get_variable (int index
, type t
)
1281 int depth
= t
.depth ();
1282 if (index
> current_method
->max_locals
- depth
)
1283 verify_fail ("invalid local variable");
1284 if (! t
.compatible (current_state
->locals
[index
], this))
1285 verify_fail ("incompatible type in local variable");
1288 type
t (continuation_type
);
1289 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1290 verify_fail ("invalid local variable");
1292 return current_state
->locals
[index
];
1295 // Make sure ARRAY is an array type and that its elements are
1296 // compatible with type ELEMENT. Returns the actual element type.
1297 type
require_array_type (type array
, type element
)
1299 // An odd case. Here we just pretend that everything went ok. If
1300 // the requested element type is some kind of reference, return
1301 // the null type instead.
1302 if (array
.isnull ())
1303 return element
.isreference () ? type (null_type
) : element
;
1305 if (! array
.isarray ())
1306 verify_fail ("array required");
1308 type t
= array
.element_type (this);
1309 if (! element
.compatible (t
, this))
1311 // Special case for byte arrays, which must also be boolean
1314 if (element
.key
== byte_type
)
1316 type
e2 (boolean_type
);
1317 ok
= e2
.compatible (t
, this);
1320 verify_fail ("incompatible array element type");
1323 // Return T and not ELEMENT, because T might be specialized.
1329 if (PC
>= current_method
->code_length
)
1330 verify_fail ("premature end of bytecode");
1331 return (jint
) bytecode
[PC
++] & 0xff;
1336 jint b1
= get_byte ();
1337 jint b2
= get_byte ();
1338 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1343 jint b1
= get_byte ();
1344 jint b2
= get_byte ();
1345 jshort s
= (b1
<< 8) | b2
;
1351 jint b1
= get_byte ();
1352 jint b2
= get_byte ();
1353 jint b3
= get_byte ();
1354 jint b4
= get_byte ();
1355 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1358 int compute_jump (int offset
)
1360 int npc
= start_PC
+ offset
;
1361 if (npc
< 0 || npc
>= current_method
->code_length
)
1362 verify_fail ("branch out of range", start_PC
);
1366 // Add a new state to the state list at NPC.
1367 state
*add_new_state (int npc
, state
*old_state
)
1369 state
*new_state
= new state (old_state
, current_method
->max_stack
,
1370 current_method
->max_locals
);
1371 debug_print ("== New state in add_new_state\n");
1372 new_state
->print ("New", npc
, current_method
->max_stack
,
1373 current_method
->max_locals
);
1374 linked
<state
> *nlink
1375 = (linked
<state
> *) _Jv_Malloc (sizeof (linked
<state
>));
1376 nlink
->val
= new_state
;
1377 nlink
->next
= states
[npc
];
1378 states
[npc
] = nlink
;
1379 new_state
->set_pc (npc
);
1383 // Merge the indicated state into the state at the branch target and
1384 // schedule a new PC if there is a change. NPC is the PC of the
1385 // branch target, and FROM_STATE is the state at the source of the
1386 // branch. This method returns true if the destination state
1387 // changed and requires reverification, false otherwise.
1388 void merge_into (int npc
, state
*from_state
)
1390 // Iterate over all target states and merge our state into each,
1391 // if applicable. FIXME one improvement we could make here is
1392 // "state destruction". Merging a new state into an existing one
1393 // might cause a return_address_type to be merged to
1394 // unsuitable_type. In this case the resulting state may now be
1395 // mergeable with other states currently held in parallel at this
1396 // location. So in this situation we could pairwise compare and
1397 // reduce the number of parallel states.
1398 bool applicable
= false;
1399 for (linked
<state
> *iter
= states
[npc
]; iter
!= NULL
; iter
= iter
->next
)
1401 state
*new_state
= iter
->val
;
1402 if (new_state
->state_mergeable_p (from_state
,
1403 current_method
->max_locals
, this))
1407 debug_print ("== Merge states in merge_into\n");
1408 from_state
->print ("Frm", start_PC
, current_method
->max_stack
,
1409 current_method
->max_locals
);
1410 new_state
->print (" To", npc
, current_method
->max_stack
,
1411 current_method
->max_locals
);
1412 bool changed
= new_state
->merge (from_state
,
1413 current_method
->max_locals
,
1415 new_state
->print ("New", npc
, current_method
->max_stack
,
1416 current_method
->max_locals
);
1419 new_state
->reverify (this);
1425 // Either we don't yet have a state at NPC, or we have a
1426 // return-address type that is in conflict with all existing
1427 // state. So, we need to create a new entry.
1428 state
*new_state
= add_new_state (npc
, from_state
);
1429 // A new state added in this way must always be reverified.
1430 new_state
->reverify (this);
1434 void push_jump (int offset
)
1436 int npc
= compute_jump (offset
);
1438 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1439 merge_into (npc
, current_state
);
1442 void push_exception_jump (type t
, int pc
)
1444 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1446 state
s (current_state
, current_method
->max_stack
,
1447 current_method
->max_locals
);
1448 if (current_method
->max_stack
< 1)
1449 verify_fail ("stack overflow at exception handler");
1450 s
.set_exception (t
, current_method
->max_stack
);
1451 merge_into (pc
, &s
);
1456 state
*new_state
= next_verify_state
;
1457 if (new_state
== INVALID_STATE
)
1458 verify_fail ("programmer error in pop_jump");
1459 if (new_state
!= NULL
)
1461 next_verify_state
= new_state
->next
;
1462 new_state
->next
= INVALID_STATE
;
1467 void invalidate_pc ()
1469 PC
= state::NO_NEXT
;
1472 void note_branch_target (int pc
)
1474 // Don't check `pc <= PC', because we've advanced PC after
1475 // fetching the target and we haven't yet checked the next
1477 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1478 verify_fail ("branch not to instruction start", start_PC
);
1479 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1482 void skip_padding ()
1484 while ((PC
% 4) > 0)
1485 if (get_byte () != 0)
1486 verify_fail ("found nonzero padding byte");
1489 // Do the work for a `ret' instruction. INDEX is the index into the
1491 void handle_ret_insn (int index
)
1493 type ret_addr
= get_variable (index
, return_address_type
);
1494 // It would be nice if we could do this. However, the JVM Spec
1495 // doesn't say that this is what happens. It is implied that
1496 // reusing a return address is invalid, but there's no actual
1497 // prohibition against it.
1498 // set_variable (index, unsuitable_type);
1500 int npc
= ret_addr
.get_pc ();
1501 // We might be returning to a `jsr' that is at the end of the
1502 // bytecode. This is ok if we never return from the called
1503 // subroutine, but if we see this here it is an error.
1504 if (npc
>= current_method
->code_length
)
1505 verify_fail ("fell off end");
1508 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1510 merge_into (npc
, current_state
);
1514 void handle_jsr_insn (int offset
)
1516 int npc
= compute_jump (offset
);
1519 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1521 // Modify our state as appropriate for entry into a subroutine.
1522 type
ret_addr (return_address_type
);
1523 ret_addr
.set_return_address (PC
);
1524 push_type (ret_addr
);
1525 merge_into (npc
, current_state
);
1529 jclass
construct_primitive_array_type (type_val prim
)
1535 k
= JvPrimClass (boolean
);
1538 k
= JvPrimClass (char);
1541 k
= JvPrimClass (float);
1544 k
= JvPrimClass (double);
1547 k
= JvPrimClass (byte
);
1550 k
= JvPrimClass (short);
1553 k
= JvPrimClass (int);
1556 k
= JvPrimClass (long);
1559 // These aren't used here but we call them out to avoid
1562 case unsuitable_type
:
1563 case return_address_type
:
1564 case continuation_type
:
1565 case reference_type
:
1567 case uninitialized_reference_type
:
1569 verify_fail ("unknown type in construct_primitive_array_type");
1571 k
= _Jv_GetArrayClass (k
, NULL
);
1575 // This pass computes the location of branch targets and also
1576 // instruction starts.
1577 void branch_prepass ()
1579 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1581 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1585 while (PC
< current_method
->code_length
)
1587 // Set `start_PC' early so that error checking can have the
1590 flags
[PC
] |= FLAG_INSN_START
;
1592 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1596 case op_aconst_null
:
1732 case op_monitorenter
:
1733 case op_monitorexit
:
1741 case op_arraylength
:
1773 case op_invokespecial
:
1774 case op_invokestatic
:
1775 case op_invokevirtual
:
1779 case op_multianewarray
:
1802 note_branch_target (compute_jump (get_short ()));
1805 case op_tableswitch
:
1808 note_branch_target (compute_jump (get_int ()));
1809 jint low
= get_int ();
1810 jint hi
= get_int ();
1812 verify_fail ("invalid tableswitch", start_PC
);
1813 for (int i
= low
; i
<= hi
; ++i
)
1814 note_branch_target (compute_jump (get_int ()));
1818 case op_lookupswitch
:
1821 note_branch_target (compute_jump (get_int ()));
1822 int npairs
= get_int ();
1824 verify_fail ("too few pairs in lookupswitch", start_PC
);
1825 while (npairs
-- > 0)
1828 note_branch_target (compute_jump (get_int ()));
1833 case op_invokeinterface
:
1841 opcode
= (java_opcode
) get_byte ();
1843 if (opcode
== op_iinc
)
1850 note_branch_target (compute_jump (get_int ()));
1853 // These are unused here, but we call them out explicitly
1854 // so that -Wswitch-enum doesn't complain.
1860 case op_putstatic_1
:
1861 case op_putstatic_2
:
1862 case op_putstatic_4
:
1863 case op_putstatic_8
:
1864 case op_putstatic_a
:
1866 case op_getfield_2s
:
1867 case op_getfield_2u
:
1871 case op_getstatic_1
:
1872 case op_getstatic_2s
:
1873 case op_getstatic_2u
:
1874 case op_getstatic_4
:
1875 case op_getstatic_8
:
1876 case op_getstatic_a
:
1878 verify_fail ("unrecognized instruction in branch_prepass",
1882 // See if any previous branch tried to branch to the middle of
1883 // this instruction.
1884 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1886 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1887 verify_fail ("branch to middle of instruction", pc
);
1891 // Verify exception handlers.
1892 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1894 if (! (flags
[exception
[i
].handler_pc
.i
] & FLAG_INSN_START
))
1895 verify_fail ("exception handler not at instruction start",
1896 exception
[i
].handler_pc
.i
);
1897 if (! (flags
[exception
[i
].start_pc
.i
] & FLAG_INSN_START
))
1898 verify_fail ("exception start not at instruction start",
1899 exception
[i
].start_pc
.i
);
1900 if (exception
[i
].end_pc
.i
!= current_method
->code_length
1901 && ! (flags
[exception
[i
].end_pc
.i
] & FLAG_INSN_START
))
1902 verify_fail ("exception end not at instruction start",
1903 exception
[i
].end_pc
.i
);
1905 flags
[exception
[i
].handler_pc
.i
] |= FLAG_BRANCH_TARGET
;
1909 void check_pool_index (int index
)
1911 if (index
< 0 || index
>= current_class
->constants
.size
)
1912 verify_fail ("constant pool index out of range", start_PC
);
1915 type
check_class_constant (int index
)
1917 check_pool_index (index
);
1918 _Jv_Constants
*pool
= ¤t_class
->constants
;
1919 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1920 return type (pool
->data
[index
].clazz
, this);
1921 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1922 return type (pool
->data
[index
].utf8
, this);
1923 verify_fail ("expected class constant", start_PC
);
1926 type
check_constant (int index
)
1928 check_pool_index (index
);
1929 _Jv_Constants
*pool
= ¤t_class
->constants
;
1930 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1931 || pool
->tags
[index
] == JV_CONSTANT_String
)
1932 return type (&java::lang::String::class$
, this);
1933 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1934 return type (int_type
);
1935 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1936 return type (float_type
);
1937 verify_fail ("String, int, or float constant expected", start_PC
);
1940 type
check_wide_constant (int index
)
1942 check_pool_index (index
);
1943 _Jv_Constants
*pool
= ¤t_class
->constants
;
1944 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1945 return type (long_type
);
1946 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1947 return type (double_type
);
1948 verify_fail ("long or double constant expected", start_PC
);
1951 // Helper for both field and method. These are laid out the same in
1952 // the constant pool.
1953 type
handle_field_or_method (int index
, int expected
,
1954 _Jv_Utf8Const
**name
,
1955 _Jv_Utf8Const
**fmtype
)
1957 check_pool_index (index
);
1958 _Jv_Constants
*pool
= ¤t_class
->constants
;
1959 if (pool
->tags
[index
] != expected
)
1960 verify_fail ("didn't see expected constant", start_PC
);
1961 // Once we know we have a Fieldref or Methodref we assume that it
1962 // is correctly laid out in the constant pool. I think the code
1963 // in defineclass.cc guarantees this.
1964 _Jv_ushort class_index
, name_and_type_index
;
1965 _Jv_loadIndexes (&pool
->data
[index
],
1967 name_and_type_index
);
1968 _Jv_ushort name_index
, desc_index
;
1969 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1970 name_index
, desc_index
);
1972 *name
= pool
->data
[name_index
].utf8
;
1973 *fmtype
= pool
->data
[desc_index
].utf8
;
1975 return check_class_constant (class_index
);
1978 // Return field's type, compute class' type if requested.
1979 type
check_field_constant (int index
, type
*class_type
= NULL
)
1981 _Jv_Utf8Const
*name
, *field_type
;
1982 type ct
= handle_field_or_method (index
,
1983 JV_CONSTANT_Fieldref
,
1984 &name
, &field_type
);
1987 if (field_type
->first() == '[' || field_type
->first() == 'L')
1988 return type (field_type
, this);
1989 return get_type_val_for_signature (field_type
->first());
1992 type
check_method_constant (int index
, bool is_interface
,
1993 _Jv_Utf8Const
**method_name
,
1994 _Jv_Utf8Const
**method_signature
)
1996 return handle_field_or_method (index
,
1998 ? JV_CONSTANT_InterfaceMethodref
1999 : JV_CONSTANT_Methodref
),
2000 method_name
, method_signature
);
2003 type
get_one_type (char *&p
)
2021 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
2022 return type (name
, this);
2025 // Casting to jchar here is ok since we are looking at an ASCII
2027 type_val rt
= get_type_val_for_signature (jchar (v
));
2029 if (arraycount
== 0)
2031 // Callers of this function eventually push their arguments on
2032 // the stack. So, promote them here.
2033 return type (rt
).promote ();
2036 jclass k
= construct_primitive_array_type (rt
);
2037 while (--arraycount
> 0)
2038 k
= _Jv_GetArrayClass (k
, NULL
);
2039 return type (k
, this);
2042 void compute_argument_types (_Jv_Utf8Const
*signature
,
2045 char *p
= signature
->chars();
2052 types
[i
++] = get_one_type (p
);
2055 type
compute_return_type (_Jv_Utf8Const
*signature
)
2057 char *p
= signature
->chars();
2061 return get_one_type (p
);
2064 void check_return_type (type onstack
)
2066 type rt
= compute_return_type (current_method
->self
->signature
);
2067 if (! rt
.compatible (onstack
, this))
2068 verify_fail ("incompatible return type");
2071 // Initialize the stack for the new method. Returns true if this
2072 // method is an instance initializer.
2073 bool initialize_stack ()
2076 bool is_init
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2078 bool is_clinit
= _Jv_equalUtf8Consts (current_method
->self
->name
,
2081 using namespace java::lang::reflect
;
2082 if (! Modifier::isStatic (current_method
->self
->accflags
))
2084 type
kurr (current_class
, this);
2087 kurr
.set_uninitialized (type::SELF
, this);
2091 verify_fail ("<clinit> method must be static");
2092 set_variable (0, kurr
);
2093 current_state
->set_this_type (kurr
);
2099 verify_fail ("<init> method must be non-static");
2102 // We have to handle wide arguments specially here.
2103 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2104 type arg_types
[arg_count
];
2105 compute_argument_types (current_method
->self
->signature
, arg_types
);
2106 for (int i
= 0; i
< arg_count
; ++i
)
2108 set_variable (var
, arg_types
[i
]);
2110 if (arg_types
[i
].iswide ())
2117 void verify_instructions_0 ()
2119 current_state
= new state (current_method
->max_stack
,
2120 current_method
->max_locals
);
2125 // True if we are verifying an instance initializer.
2126 bool this_is_init
= initialize_stack ();
2128 states
= (linked
<state
> **) _Jv_Malloc (sizeof (linked
<state
> *)
2129 * current_method
->code_length
);
2130 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2133 next_verify_state
= NULL
;
2137 // If the PC was invalidated, get a new one from the work list.
2138 if (PC
== state::NO_NEXT
)
2140 state
*new_state
= pop_jump ();
2141 // If it is null, we're done.
2142 if (new_state
== NULL
)
2145 PC
= new_state
->get_pc ();
2146 debug_print ("== State pop from pending list\n");
2147 // Set up the current state.
2148 current_state
->copy (new_state
, current_method
->max_stack
,
2149 current_method
->max_locals
);
2153 // We only have to do this checking in the situation where
2154 // control flow falls through from the previous
2155 // instruction. Otherwise merging is done at the time we
2157 if (states
[PC
] != NULL
)
2159 // We've already visited this instruction. So merge
2160 // the states together. It is simplest, but not most
2161 // efficient, to just always invalidate the PC here.
2162 merge_into (PC
, current_state
);
2168 // Control can't fall off the end of the bytecode. We need to
2169 // check this in both cases, not just the fall-through case,
2170 // because we don't check to see whether a `jsr' appears at
2171 // the end of the bytecode until we process a `ret'.
2172 if (PC
>= current_method
->code_length
)
2173 verify_fail ("fell off end");
2175 // We only have to keep saved state at branch targets. If
2176 // we're at a branch target and the state here hasn't been set
2177 // yet, we set it now. You might notice that `ret' targets
2178 // won't necessarily have FLAG_BRANCH_TARGET set. This
2179 // doesn't matter, since those states will be filled in by
2181 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2182 add_new_state (PC
, current_state
);
2184 // Set this before handling exceptions so that debug output is
2188 // Update states for all active exception handlers. Ordinarily
2189 // there are not many exception handlers. So we simply run
2190 // through them all.
2191 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2193 if (PC
>= exception
[i
].start_pc
.i
&& PC
< exception
[i
].end_pc
.i
)
2195 type
handler (&java::lang::Throwable::class$
, this);
2196 if (exception
[i
].handler_type
.i
!= 0)
2197 handler
= check_class_constant (exception
[i
].handler_type
.i
);
2198 push_exception_jump (handler
, exception
[i
].handler_pc
.i
);
2202 current_state
->print (" ", PC
, current_method
->max_stack
,
2203 current_method
->max_locals
);
2204 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2210 case op_aconst_null
:
2211 push_type (null_type
);
2221 push_type (int_type
);
2226 push_type (long_type
);
2232 push_type (float_type
);
2237 push_type (double_type
);
2242 push_type (int_type
);
2247 push_type (int_type
);
2251 push_type (check_constant (get_byte ()));
2254 push_type (check_constant (get_ushort ()));
2257 push_type (check_wide_constant (get_ushort ()));
2261 push_type (get_variable (get_byte (), int_type
));
2264 push_type (get_variable (get_byte (), long_type
));
2267 push_type (get_variable (get_byte (), float_type
));
2270 push_type (get_variable (get_byte (), double_type
));
2273 push_type (get_variable (get_byte (), reference_type
));
2280 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2286 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2292 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2298 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2304 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2307 pop_type (int_type
);
2308 push_type (require_array_type (pop_init_ref (reference_type
),
2312 pop_type (int_type
);
2313 push_type (require_array_type (pop_init_ref (reference_type
),
2317 pop_type (int_type
);
2318 push_type (require_array_type (pop_init_ref (reference_type
),
2322 pop_type (int_type
);
2323 push_type (require_array_type (pop_init_ref (reference_type
),
2327 pop_type (int_type
);
2328 push_type (require_array_type (pop_init_ref (reference_type
),
2332 pop_type (int_type
);
2333 require_array_type (pop_init_ref (reference_type
), byte_type
);
2334 push_type (int_type
);
2337 pop_type (int_type
);
2338 require_array_type (pop_init_ref (reference_type
), char_type
);
2339 push_type (int_type
);
2342 pop_type (int_type
);
2343 require_array_type (pop_init_ref (reference_type
), short_type
);
2344 push_type (int_type
);
2347 set_variable (get_byte (), pop_type (int_type
));
2350 set_variable (get_byte (), pop_type (long_type
));
2353 set_variable (get_byte (), pop_type (float_type
));
2356 set_variable (get_byte (), pop_type (double_type
));
2359 set_variable (get_byte (), pop_ref_or_return ());
2365 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2371 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2377 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2383 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2389 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2392 pop_type (int_type
);
2393 pop_type (int_type
);
2394 require_array_type (pop_init_ref (reference_type
), int_type
);
2397 pop_type (long_type
);
2398 pop_type (int_type
);
2399 require_array_type (pop_init_ref (reference_type
), long_type
);
2402 pop_type (float_type
);
2403 pop_type (int_type
);
2404 require_array_type (pop_init_ref (reference_type
), float_type
);
2407 pop_type (double_type
);
2408 pop_type (int_type
);
2409 require_array_type (pop_init_ref (reference_type
), double_type
);
2412 pop_type (reference_type
);
2413 pop_type (int_type
);
2414 require_array_type (pop_init_ref (reference_type
), reference_type
);
2417 pop_type (int_type
);
2418 pop_type (int_type
);
2419 require_array_type (pop_init_ref (reference_type
), byte_type
);
2422 pop_type (int_type
);
2423 pop_type (int_type
);
2424 require_array_type (pop_init_ref (reference_type
), char_type
);
2427 pop_type (int_type
);
2428 pop_type (int_type
);
2429 require_array_type (pop_init_ref (reference_type
), short_type
);
2436 type t
= pop_raw ();
2460 type t2
= pop_raw ();
2475 type t
= pop_raw ();
2490 type t1
= pop_raw ();
2507 type t1
= pop_raw ();
2510 type t2
= pop_raw ();
2528 type t3
= pop_raw ();
2566 pop_type (int_type
);
2567 push_type (pop_type (int_type
));
2577 pop_type (long_type
);
2578 push_type (pop_type (long_type
));
2583 pop_type (int_type
);
2584 push_type (pop_type (long_type
));
2591 pop_type (float_type
);
2592 push_type (pop_type (float_type
));
2599 pop_type (double_type
);
2600 push_type (pop_type (double_type
));
2606 push_type (pop_type (int_type
));
2609 push_type (pop_type (long_type
));
2612 push_type (pop_type (float_type
));
2615 push_type (pop_type (double_type
));
2618 get_variable (get_byte (), int_type
);
2622 pop_type (int_type
);
2623 push_type (long_type
);
2626 pop_type (int_type
);
2627 push_type (float_type
);
2630 pop_type (int_type
);
2631 push_type (double_type
);
2634 pop_type (long_type
);
2635 push_type (int_type
);
2638 pop_type (long_type
);
2639 push_type (float_type
);
2642 pop_type (long_type
);
2643 push_type (double_type
);
2646 pop_type (float_type
);
2647 push_type (int_type
);
2650 pop_type (float_type
);
2651 push_type (long_type
);
2654 pop_type (float_type
);
2655 push_type (double_type
);
2658 pop_type (double_type
);
2659 push_type (int_type
);
2662 pop_type (double_type
);
2663 push_type (long_type
);
2666 pop_type (double_type
);
2667 push_type (float_type
);
2670 pop_type (long_type
);
2671 pop_type (long_type
);
2672 push_type (int_type
);
2676 pop_type (float_type
);
2677 pop_type (float_type
);
2678 push_type (int_type
);
2682 pop_type (double_type
);
2683 pop_type (double_type
);
2684 push_type (int_type
);
2692 pop_type (int_type
);
2693 push_jump (get_short ());
2701 pop_type (int_type
);
2702 pop_type (int_type
);
2703 push_jump (get_short ());
2707 pop_type (reference_type
);
2708 pop_type (reference_type
);
2709 push_jump (get_short ());
2712 push_jump (get_short ());
2716 handle_jsr_insn (get_short ());
2719 handle_ret_insn (get_byte ());
2721 case op_tableswitch
:
2723 pop_type (int_type
);
2725 push_jump (get_int ());
2726 jint low
= get_int ();
2727 jint high
= get_int ();
2728 // Already checked LOW -vs- HIGH.
2729 for (int i
= low
; i
<= high
; ++i
)
2730 push_jump (get_int ());
2735 case op_lookupswitch
:
2737 pop_type (int_type
);
2739 push_jump (get_int ());
2740 jint npairs
= get_int ();
2741 // Already checked NPAIRS >= 0.
2743 for (int i
= 0; i
< npairs
; ++i
)
2745 jint key
= get_int ();
2746 if (i
> 0 && key
<= lastkey
)
2747 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2749 push_jump (get_int ());
2755 check_return_type (pop_type (int_type
));
2759 check_return_type (pop_type (long_type
));
2763 check_return_type (pop_type (float_type
));
2767 check_return_type (pop_type (double_type
));
2771 check_return_type (pop_init_ref (reference_type
));
2775 // We only need to check this when the return type is
2776 // void, because all instance initializers return void.
2778 current_state
->check_this_initialized (this);
2779 check_return_type (void_type
);
2783 push_type (check_field_constant (get_ushort ()));
2786 pop_type (check_field_constant (get_ushort ()));
2791 type field
= check_field_constant (get_ushort (), &klass
);
2799 type field
= check_field_constant (get_ushort (), &klass
);
2802 // We have an obscure special case here: we can use
2803 // `putfield' on a field declared in this class, even if
2804 // `this' has not yet been initialized.
2805 if (! current_state
->this_type
.isinitialized ()
2806 && current_state
->this_type
.pc
== type::SELF
)
2807 klass
.set_uninitialized (type::SELF
, this);
2812 case op_invokevirtual
:
2813 case op_invokespecial
:
2814 case op_invokestatic
:
2815 case op_invokeinterface
:
2817 _Jv_Utf8Const
*method_name
, *method_signature
;
2819 = check_method_constant (get_ushort (),
2820 opcode
== op_invokeinterface
,
2823 // NARGS is only used when we're processing
2824 // invokeinterface. It is simplest for us to compute it
2825 // here and then verify it later.
2827 if (opcode
== op_invokeinterface
)
2829 nargs
= get_byte ();
2830 if (get_byte () != 0)
2831 verify_fail ("invokeinterface dummy byte is wrong");
2834 bool is_init
= false;
2835 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2838 if (opcode
!= op_invokespecial
)
2839 verify_fail ("can't invoke <init>");
2841 else if (method_name
->first() == '<')
2842 verify_fail ("can't invoke method starting with `<'");
2844 // Pop arguments and check types.
2845 int arg_count
= _Jv_count_arguments (method_signature
);
2846 type arg_types
[arg_count
];
2847 compute_argument_types (method_signature
, arg_types
);
2848 for (int i
= arg_count
- 1; i
>= 0; --i
)
2850 // This is only used for verifying the byte for
2852 nargs
-= arg_types
[i
].depth ();
2853 pop_init_ref (arg_types
[i
]);
2856 if (opcode
== op_invokeinterface
2858 verify_fail ("wrong argument count for invokeinterface");
2860 if (opcode
!= op_invokestatic
)
2862 type t
= class_type
;
2865 // In this case the PC doesn't matter.
2866 t
.set_uninitialized (type::UNINIT
, this);
2867 // FIXME: check to make sure that the <init>
2868 // call is to the right class.
2869 // It must either be super or an exact class
2872 type raw
= pop_raw ();
2873 if (! t
.compatible (raw
, this))
2874 verify_fail ("incompatible type on stack");
2877 current_state
->set_initialized (raw
.get_pc (),
2878 current_method
->max_locals
);
2881 type rt
= compute_return_type (method_signature
);
2889 type t
= check_class_constant (get_ushort ());
2890 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2891 verify_fail ("type is array, interface, or abstract");
2892 t
.set_uninitialized (start_PC
, this);
2899 int atype
= get_byte ();
2900 // We intentionally have chosen constants to make this
2902 if (atype
< boolean_type
|| atype
> long_type
)
2903 verify_fail ("type not primitive", start_PC
);
2904 pop_type (int_type
);
2905 type
t (construct_primitive_array_type (type_val (atype
)), this);
2910 pop_type (int_type
);
2911 push_type (check_class_constant (get_ushort ()).to_array (this));
2913 case op_arraylength
:
2915 type t
= pop_init_ref (reference_type
);
2916 if (! t
.isarray () && ! t
.isnull ())
2917 verify_fail ("array type expected");
2918 push_type (int_type
);
2922 pop_type (type (&java::lang::Throwable::class$
, this));
2926 pop_init_ref (reference_type
);
2927 push_type (check_class_constant (get_ushort ()));
2930 pop_init_ref (reference_type
);
2931 check_class_constant (get_ushort ());
2932 push_type (int_type
);
2934 case op_monitorenter
:
2935 pop_init_ref (reference_type
);
2937 case op_monitorexit
:
2938 pop_init_ref (reference_type
);
2942 switch (get_byte ())
2945 push_type (get_variable (get_ushort (), int_type
));
2948 push_type (get_variable (get_ushort (), long_type
));
2951 push_type (get_variable (get_ushort (), float_type
));
2954 push_type (get_variable (get_ushort (), double_type
));
2957 push_type (get_variable (get_ushort (), reference_type
));
2960 set_variable (get_ushort (), pop_type (int_type
));
2963 set_variable (get_ushort (), pop_type (long_type
));
2966 set_variable (get_ushort (), pop_type (float_type
));
2969 set_variable (get_ushort (), pop_type (double_type
));
2972 set_variable (get_ushort (), pop_init_ref (reference_type
));
2975 handle_ret_insn (get_short ());
2978 get_variable (get_ushort (), int_type
);
2982 verify_fail ("unrecognized wide instruction", start_PC
);
2986 case op_multianewarray
:
2988 type atype
= check_class_constant (get_ushort ());
2989 int dim
= get_byte ();
2991 verify_fail ("too few dimensions to multianewarray", start_PC
);
2992 atype
.verify_dimensions (dim
, this);
2993 for (int i
= 0; i
< dim
; ++i
)
2994 pop_type (int_type
);
3000 pop_type (reference_type
);
3001 push_jump (get_short ());
3004 push_jump (get_int ());
3008 handle_jsr_insn (get_int ());
3011 // These are unused here, but we call them out explicitly
3012 // so that -Wswitch-enum doesn't complain.
3018 case op_putstatic_1
:
3019 case op_putstatic_2
:
3020 case op_putstatic_4
:
3021 case op_putstatic_8
:
3022 case op_putstatic_a
:
3024 case op_getfield_2s
:
3025 case op_getfield_2u
:
3029 case op_getstatic_1
:
3030 case op_getstatic_2s
:
3031 case op_getstatic_2u
:
3032 case op_getstatic_4
:
3033 case op_getstatic_8
:
3034 case op_getstatic_a
:
3036 // Unrecognized opcode.
3037 verify_fail ("unrecognized instruction in verify_instructions_0",
3045 void verify_instructions ()
3048 verify_instructions_0 ();
3051 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3053 // We just print the text as utf-8. This is just for debugging
3055 debug_print ("--------------------------------\n");
3056 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->chars());
3059 bytecode
= m
->bytecode ();
3060 exception
= m
->exceptions ();
3061 current_class
= m
->defining_class
;
3069 ~_Jv_BytecodeVerifier ()
3074 while (utf8_list
!= NULL
)
3076 linked
<_Jv_Utf8Const
> *n
= utf8_list
->next
;
3077 _Jv_Free (utf8_list
);
3081 while (isect_list
!= NULL
)
3083 ref_intersection
*next
= isect_list
->alloc_next
;
3090 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3092 linked
<state
> *iter
= states
[i
];
3093 while (iter
!= NULL
)
3095 linked
<state
> *next
= iter
->next
;
3107 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3109 _Jv_BytecodeVerifier
v (meth
);
3110 v
.verify_instructions ();
3113 #endif /* INTERPRETER */