1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Written by Tom Tromey <tromey@redhat.com>
13 // Define VERIFY_DEBUG to enable debugging output.
19 #include <java-insns.h>
20 #include <java-interp.h>
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
32 #endif /* VERIFY_DEBUG */
35 static void debug_print (const char *fmt
, ...)
36 __attribute__ ((format (printf
, 1, 2)));
39 debug_print (const char *fmt
, ...)
44 vfprintf (stderr
, fmt
, ap
);
46 #endif /* VERIFY_DEBUG */
49 class _Jv_BytecodeVerifier
53 static const int FLAG_INSN_START
= 1;
54 static const int FLAG_BRANCH_TARGET
= 2;
59 struct subr_entry_info
;
64 // The PC corresponding to the start of the current instruction.
67 // The current state of the stack, locals, etc.
70 // We store the state at branch targets, for merging. This holds
74 // We keep a linked list of all the PCs which we must reverify.
75 // The link is done using the PC values. This is the head of the
79 // We keep some flags for each instruction. The values are the
80 // FLAG_* constants defined above.
83 // We need to keep track of which instructions can call a given
84 // subroutine. FIXME: this is inefficient. We keep a linked list
85 // of all calling `jsr's at at each jsr target.
88 // We keep a linked list of entries which map each `ret' instruction
89 // to its unique subroutine entry point. We expect that there won't
90 // be many `ret' instructions, so a linked list is ok.
91 subr_entry_info
*entry_points
;
93 // The current top of the stack, in terms of slots.
95 // The current depth of the stack. This will be larger than
96 // STACKTOP when wide types are on the stack.
99 // The bytecode itself.
100 unsigned char *bytecode
;
102 _Jv_InterpException
*exception
;
105 jclass current_class
;
107 _Jv_InterpMethod
*current_method
;
109 // A linked list of utf8 objects we allocate. This is really ugly,
110 // but without this our utf8 objects would be collected.
111 linked_utf8
*utf8_list
;
119 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
121 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
122 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
125 r
->length
= val
->length
;
127 memcpy (r
->data
, val
->data
, val
->length
+ 1);
129 linked_utf8
*lu
= (linked_utf8
*) _Jv_Malloc (sizeof (linked_utf8
));
131 lu
->next
= utf8_list
;
137 // This enum holds a list of tags for all the different types we
138 // need to handle. Reference types are treated specially by the
144 // The values for primitive types are chosen to correspond to values
145 // specified to newarray.
155 // Used when overwriting second word of a double or long in the
156 // local variables. Also used after merging local variable states
157 // to indicate an unusable value.
162 // There is an obscure special case which requires us to note when
163 // a local variable has not been used by a subroutine. See
164 // push_jump_merge for more information.
165 unused_by_subroutine_type
,
167 // Everything after `reference_type' must be a reference type.
170 unresolved_reference_type
,
171 uninitialized_reference_type
,
172 uninitialized_unresolved_reference_type
175 // Return the type_val corresponding to a primitive signature
176 // character. For instance `I' returns `int.class'.
177 type_val
get_type_val_for_signature (jchar sig
)
210 verify_fail ("invalid signature");
215 // Return the type_val corresponding to a primitive class.
216 type_val
get_type_val_for_signature (jclass k
)
218 return get_type_val_for_signature ((jchar
) k
->method_count
);
221 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
222 // TARGET haven't been prepared.
223 static bool is_assignable_from_slow (jclass target
, jclass source
)
225 // This will terminate when SOURCE==Object.
228 if (source
== target
)
231 if (target
->isPrimitive () || source
->isPrimitive ())
234 // Check array case first because we can have an array whose
235 // component type is not prepared; _Jv_IsAssignableFrom
236 // doesn't handle this correctly.
237 if (target
->isArray ())
239 if (! source
->isArray ())
241 target
= target
->getComponentType ();
242 source
= source
->getComponentType ();
244 // _Jv_IsAssignableFrom can handle a target which is an
245 // interface even if it hasn't been prepared.
246 else if ((target
->state
> JV_STATE_LINKED
|| target
->isInterface ())
247 && source
->state
> JV_STATE_LINKED
)
248 return _Jv_IsAssignableFrom (target
, source
);
249 else if (target
->isInterface ())
251 for (int i
= 0; i
< source
->interface_count
; ++i
)
253 // We use a recursive call because we also need to
254 // check superinterfaces.
255 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
258 source
= source
->getSuperclass ();
262 // We must do this check before we check to see if SOURCE is
263 // an interface. This way we know that any interface is
264 // assignable to an Object.
265 else if (target
== &java::lang::Object::class$
)
267 else if (source
->isInterface ())
269 for (int i
= 0; i
< target
->interface_count
; ++i
)
271 // We use a recursive call because we also need to
272 // check superinterfaces.
273 if (is_assignable_from_slow (target
->interfaces
[i
], source
))
276 target
= target
->getSuperclass ();
280 else if (source
== &java::lang::Object::class$
)
283 source
= source
->getSuperclass ();
287 // This is used to keep track of which `jsr's correspond to a given
291 // PC of the instruction just after the jsr.
297 // This is used to keep track of which subroutine entry point
298 // corresponds to which `ret' instruction.
299 struct subr_entry_info
301 // PC of the subroutine entry point.
303 // PC of the `ret' instruction.
306 subr_entry_info
*next
;
309 // The `type' class is used to represent a single type in the
315 // Some associated data.
318 // For a resolved reference type, this is a pointer to the class.
320 // For other reference types, this it the name of the class.
323 // This is used when constructing a new object. It is the PC of the
324 // `new' instruction which created the object. We use the special
325 // value -2 to mean that this is uninitialized, and the special
326 // value -1 for the case where the current method is itself the
330 static const int UNINIT
= -2;
331 static const int SELF
= -1;
333 // Basic constructor.
336 key
= unsuitable_type
;
341 // Make a new instance given the type tag. We assume a generic
342 // `reference_type' means Object.
347 if (key
== reference_type
)
348 data
.klass
= &java::lang::Object::class$
;
352 // Make a new instance given a class.
355 key
= reference_type
;
360 // Make a new instance given the name of a class.
361 type (_Jv_Utf8Const
*n
)
363 key
= unresolved_reference_type
;
376 // These operators are required because libgcj can't link in
378 void *operator new[] (size_t bytes
)
380 return _Jv_Malloc (bytes
);
383 void operator delete[] (void *mem
)
388 type
& operator= (type_val k
)
396 type
& operator= (const type
& t
)
404 // Promote a numeric type.
407 if (key
== boolean_type
|| key
== char_type
408 || key
== byte_type
|| key
== short_type
)
413 // If *THIS is an unresolved reference type, resolve it.
414 void resolve (_Jv_BytecodeVerifier
*verifier
)
416 if (key
!= unresolved_reference_type
417 && key
!= uninitialized_unresolved_reference_type
)
420 using namespace java::lang
;
421 java::lang::ClassLoader
*loader
422 = verifier
->current_class
->getClassLoader();
423 // We might see either kind of name. Sigh.
424 if (data
.name
->data
[0] == 'L'
425 && data
.name
->data
[data
.name
->length
- 1] == ';')
426 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
428 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
430 key
= (key
== unresolved_reference_type
432 : uninitialized_reference_type
);
435 // Mark this type as the uninitialized result of `new'.
436 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
438 if (key
== reference_type
)
439 key
= uninitialized_reference_type
;
440 else if (key
== unresolved_reference_type
)
441 key
= uninitialized_unresolved_reference_type
;
443 verifier
->verify_fail ("internal error in type::uninitialized");
447 // Mark this type as now initialized.
448 void set_initialized (int npc
)
450 if (npc
!= UNINIT
&& pc
== npc
451 && (key
== uninitialized_reference_type
452 || key
== uninitialized_unresolved_reference_type
))
454 key
= (key
== uninitialized_reference_type
456 : unresolved_reference_type
);
462 // Return true if an object of type K can be assigned to a variable
463 // of type *THIS. Handle various special cases too. Might modify
464 // *THIS or K. Note however that this does not perform numeric
466 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
468 // Any type is compatible with the unsuitable type.
469 if (key
== unsuitable_type
)
472 if (key
< reference_type
|| k
.key
< reference_type
)
475 // The `null' type is convertible to any reference type.
476 // FIXME: is this correct for THIS?
477 if (key
== null_type
|| k
.key
== null_type
)
480 // Any reference type is convertible to Object. This is a special
481 // case so we don't need to unnecessarily resolve a class.
482 if (key
== reference_type
483 && data
.klass
== &java::lang::Object::class$
)
486 // An initialized type and an uninitialized type are not
488 if (isinitialized () != k
.isinitialized ())
491 // Two uninitialized objects are compatible if either:
492 // * The PCs are identical, or
493 // * One PC is UNINIT.
494 if (! isinitialized ())
496 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
500 // Two unresolved types are equal if their names are the same.
503 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
506 // We must resolve both types and check assignability.
508 k
.resolve (verifier
);
509 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
514 return key
== void_type
;
519 return key
== long_type
|| key
== double_type
;
522 // Return number of stack or local variable slots taken by this
526 return iswide () ? 2 : 1;
529 bool isarray () const
531 // We treat null_type as not an array. This is ok based on the
532 // current uses of this method.
533 if (key
== reference_type
)
534 return data
.klass
->isArray ();
535 else if (key
== unresolved_reference_type
)
536 return data
.name
->data
[0] == '[';
542 return key
== null_type
;
545 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
548 if (key
!= reference_type
)
550 return data
.klass
->isInterface ();
553 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
556 if (key
!= reference_type
)
558 using namespace java::lang::reflect
;
559 return Modifier::isAbstract (data
.klass
->getModifiers ());
562 // Return the element type of an array.
563 type
element_type (_Jv_BytecodeVerifier
*verifier
)
565 // FIXME: maybe should do string manipulation here.
567 if (key
!= reference_type
)
568 verifier
->verify_fail ("programmer error in type::element_type()", -1);
570 jclass k
= data
.klass
->getComponentType ();
571 if (k
->isPrimitive ())
572 return type (verifier
->get_type_val_for_signature (k
));
576 // Return the array type corresponding to an initialized
577 // reference. We could expand this to work for other kinds of
578 // types, but currently we don't need to.
579 type
to_array (_Jv_BytecodeVerifier
*verifier
)
581 // Resolving isn't ideal, because it might force us to load
582 // another class, but it's easy. FIXME?
583 if (key
== unresolved_reference_type
)
586 if (key
== reference_type
)
587 return type (_Jv_GetArrayClass (data
.klass
,
588 data
.klass
->getClassLoader ()));
590 verifier
->verify_fail ("internal error in type::to_array()");
593 bool isreference () const
595 return key
>= reference_type
;
603 bool isinitialized () const
605 return (key
== reference_type
607 || key
== unresolved_reference_type
);
610 bool isresolved () const
612 return (key
== reference_type
614 || key
== uninitialized_reference_type
);
617 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
619 // The way this is written, we don't need to check isarray().
620 if (key
== reference_type
)
622 jclass k
= data
.klass
;
623 while (k
->isArray () && ndims
> 0)
625 k
= k
->getComponentType ();
631 // We know KEY == unresolved_reference_type.
632 char *p
= data
.name
->data
;
633 while (*p
++ == '[' && ndims
-- > 0)
638 verifier
->verify_fail ("array type has fewer dimensions than required");
641 // Merge OLD_TYPE into this. On error throw exception.
642 bool merge (type
& old_type
, bool local_semantics
,
643 _Jv_BytecodeVerifier
*verifier
)
645 bool changed
= false;
646 bool refo
= old_type
.isreference ();
647 bool refn
= isreference ();
650 if (old_type
.key
== null_type
)
652 else if (key
== null_type
)
657 else if (isinitialized () != old_type
.isinitialized ())
658 verifier
->verify_fail ("merging initialized and uninitialized types");
661 if (! isinitialized ())
665 else if (old_type
.pc
== UNINIT
)
667 else if (pc
!= old_type
.pc
)
668 verifier
->verify_fail ("merging different uninitialized types");
672 && ! old_type
.isresolved ()
673 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
675 // Types are identical.
680 old_type
.resolve (verifier
);
682 jclass k
= data
.klass
;
683 jclass oldk
= old_type
.data
.klass
;
686 while (k
->isArray () && oldk
->isArray ())
689 k
= k
->getComponentType ();
690 oldk
= oldk
->getComponentType ();
693 // Ordinarily this terminates when we hit Object...
696 if (is_assignable_from_slow (k
, oldk
))
698 k
= k
->getSuperclass ();
701 // ... but K could have been an interface, in which
702 // case we'll end up here. We just convert this
705 k
= &java::lang::Object::class$
;
709 while (arraycount
> 0)
711 java::lang::ClassLoader
*loader
712 = verifier
->current_class
->getClassLoader();
713 k
= _Jv_GetArrayClass (k
, loader
);
721 else if (refo
|| refn
|| key
!= old_type
.key
)
725 // If we're merging into an "unused" slot, then we
726 // simply accept whatever we're merging from.
727 if (key
== unused_by_subroutine_type
)
732 else if (old_type
.key
== unused_by_subroutine_type
)
736 // If we already have an `unsuitable' type, then we
737 // don't need to change again.
738 else if (key
!= unsuitable_type
)
740 key
= unsuitable_type
;
745 verifier
->verify_fail ("unmergeable type");
751 void print (void) const
756 case boolean_type
: c
= 'Z'; break;
757 case byte_type
: c
= 'B'; break;
758 case char_type
: c
= 'C'; break;
759 case short_type
: c
= 'S'; break;
760 case int_type
: c
= 'I'; break;
761 case long_type
: c
= 'J'; break;
762 case float_type
: c
= 'F'; break;
763 case double_type
: c
= 'D'; break;
764 case void_type
: c
= 'V'; break;
765 case unsuitable_type
: c
= '-'; break;
766 case return_address_type
: c
= 'r'; break;
767 case continuation_type
: c
= '+'; break;
768 case unused_by_subroutine_type
: c
= '_'; break;
769 case reference_type
: c
= 'L'; break;
770 case null_type
: c
= '@'; break;
771 case unresolved_reference_type
: c
= 'l'; break;
772 case uninitialized_reference_type
: c
= 'U'; break;
773 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
775 debug_print ("%c", c
);
777 #endif /* VERIFY_DEBUG */
780 // This class holds all the state information we need for a given
784 // Current top of stack.
786 // Current stack depth. This is like the top of stack but it
787 // includes wide variable information.
791 // The local variables.
793 // This is used in subroutines to keep track of which local
794 // variables have been accessed.
796 // If not 0, then we are in a subroutine. The value is the PC of
797 // the subroutine's entry point. We can use 0 as an exceptional
798 // value because PC=0 can never be a subroutine.
800 // This is used to keep a linked list of all the states which
801 // require re-verification. We use the PC to keep track.
803 // We keep track of the type of `this' specially. This is used to
804 // ensure that an instance initializer invokes another initializer
805 // on `this' before returning. We must keep track of this
806 // specially because otherwise we might be confused by code which
807 // assigns to locals[0] (overwriting `this') and then returns
808 // without really initializing.
811 // INVALID marks a state which is not on the linked list of states
812 // requiring reverification.
813 static const int INVALID
= -1;
814 // NO_NEXT marks the state at the end of the reverification list.
815 static const int NO_NEXT
= -2;
822 local_changed
= NULL
;
825 state (int max_stack
, int max_locals
)
830 stack
= new type
[max_stack
];
831 for (int i
= 0; i
< max_stack
; ++i
)
832 stack
[i
] = unsuitable_type
;
833 locals
= new type
[max_locals
];
834 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
835 for (int i
= 0; i
< max_locals
; ++i
)
837 locals
[i
] = unsuitable_type
;
838 local_changed
[i
] = false;
844 state (const state
*orig
, int max_stack
, int max_locals
,
845 bool ret_semantics
= false)
847 stack
= new type
[max_stack
];
848 locals
= new type
[max_locals
];
849 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
850 copy (orig
, max_stack
, max_locals
, ret_semantics
);
861 _Jv_Free (local_changed
);
864 void *operator new[] (size_t bytes
)
866 return _Jv_Malloc (bytes
);
869 void operator delete[] (void *mem
)
874 void *operator new (size_t bytes
)
876 return _Jv_Malloc (bytes
);
879 void operator delete (void *mem
)
884 void copy (const state
*copy
, int max_stack
, int max_locals
,
885 bool ret_semantics
= false)
887 stacktop
= copy
->stacktop
;
888 stackdepth
= copy
->stackdepth
;
889 subroutine
= copy
->subroutine
;
890 for (int i
= 0; i
< max_stack
; ++i
)
891 stack
[i
] = copy
->stack
[i
];
892 for (int i
= 0; i
< max_locals
; ++i
)
894 // See push_jump_merge to understand this case.
896 locals
[i
] = type (copy
->local_changed
[i
]
898 : unused_by_subroutine_type
);
900 locals
[i
] = copy
->locals
[i
];
901 local_changed
[i
] = copy
->local_changed
[i
];
903 this_type
= copy
->this_type
;
904 // Don't modify `next'.
907 // Modify this state to reflect entry to an exception handler.
908 void set_exception (type t
, int max_stack
)
913 for (int i
= stacktop
; i
< max_stack
; ++i
)
914 stack
[i
] = unsuitable_type
;
916 // FIXME: subroutine handling?
919 // Modify this state to reflect entry into a subroutine.
920 void enter_subroutine (int npc
, int max_locals
)
923 // Mark all items as unchanged. Each subroutine needs to keep
924 // track of its `changed' state independently. In the case of
925 // nested subroutines, this information will be merged back into
926 // parent by the `ret'.
927 for (int i
= 0; i
< max_locals
; ++i
)
928 local_changed
[i
] = false;
931 // Merge STATE_OLD into this state. Destructively modifies this
932 // state. Returns true if the new state was in fact changed.
933 // Will throw an exception if the states are not mergeable.
934 bool merge (state
*state_old
, bool ret_semantics
,
935 int max_locals
, _Jv_BytecodeVerifier
*verifier
)
937 bool changed
= false;
939 // Special handling for `this'. If one or the other is
940 // uninitialized, then the merge is uninitialized.
941 if (this_type
.isinitialized ())
942 this_type
= state_old
->this_type
;
944 // Merge subroutine states. Here we just keep track of what
945 // subroutine we think we're in. We only check for a merge
946 // (which is invalid) when we see a `ret'.
947 if (subroutine
== state_old
->subroutine
)
951 else if (subroutine
== 0)
953 subroutine
= state_old
->subroutine
;
958 // If the subroutines differ, indicate that the state
959 // changed. This is needed to detect when subroutines have
965 if (state_old
->stacktop
!= stacktop
)
966 verifier
->verify_fail ("stack sizes differ");
967 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
969 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
973 // Merge local variables.
974 for (int i
= 0; i
< max_locals
; ++i
)
976 // If we're not processing a `ret', then we merge every
977 // local variable. If we are processing a `ret', then we
978 // only merge locals which changed in the subroutine. When
979 // processing a `ret', STATE_OLD is the state at the point
980 // of the `ret', and THIS is the state just after the `jsr'.
981 if (! ret_semantics
|| state_old
->local_changed
[i
])
983 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
990 // If we're in a subroutine, we must compute the union of
991 // all the changed local variables.
992 if (state_old
->local_changed
[i
])
999 // Throw an exception if there is an uninitialized object on the
1000 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1001 // whether we're using backwards-branch or exception-handing
1003 void check_no_uninitialized_objects (int max_locals
,
1004 _Jv_BytecodeVerifier
*verifier
,
1005 bool exception_semantics
= false)
1007 if (! exception_semantics
)
1009 for (int i
= 0; i
< stacktop
; ++i
)
1010 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1011 verifier
->verify_fail ("uninitialized object on stack");
1014 for (int i
= 0; i
< max_locals
; ++i
)
1015 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1016 verifier
->verify_fail ("uninitialized object in local variable");
1018 check_this_initialized (verifier
);
1021 // Ensure that `this' has been initialized.
1022 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1024 if (this_type
.isreference () && ! this_type
.isinitialized ())
1025 verifier
->verify_fail ("`this' is uninitialized");
1028 // Set type of `this'.
1029 void set_this_type (const type
&k
)
1034 // Note that a local variable was modified.
1035 void note_variable (int index
)
1038 local_changed
[index
] = true;
1041 // Mark each `new'd object we know of that was allocated at PC as
1043 void set_initialized (int pc
, int max_locals
)
1045 for (int i
= 0; i
< stacktop
; ++i
)
1046 stack
[i
].set_initialized (pc
);
1047 for (int i
= 0; i
< max_locals
; ++i
)
1048 locals
[i
].set_initialized (pc
);
1049 this_type
.set_initialized (pc
);
1052 // Return true if this state is the unmerged result of a `ret'.
1053 bool is_unmerged_ret_state (int max_locals
) const
1055 for (int i
= 0; i
< max_locals
; ++i
)
1057 if (locals
[i
].key
== unused_by_subroutine_type
)
1064 void print (const char *leader
, int pc
,
1065 int max_stack
, int max_locals
) const
1067 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1069 for (i
= 0; i
< stacktop
; ++i
)
1071 for (; i
< max_stack
; ++i
)
1073 debug_print (" [local] ");
1074 for (i
= 0; i
< max_locals
; ++i
)
1076 if (subroutine
== 0)
1077 debug_print (" | None");
1079 debug_print (" | %4d", subroutine
);
1080 debug_print (" | %p\n", this);
1083 inline void print (const char *, int, int, int) const
1086 #endif /* VERIFY_DEBUG */
1091 if (current_state
->stacktop
<= 0)
1092 verify_fail ("stack empty");
1093 type r
= current_state
->stack
[--current_state
->stacktop
];
1094 current_state
->stackdepth
-= r
.depth ();
1095 if (current_state
->stackdepth
< 0)
1096 verify_fail ("stack empty", start_PC
);
1102 type r
= pop_raw ();
1104 verify_fail ("narrow pop of wide type");
1110 type r
= pop_raw ();
1112 verify_fail ("wide pop of narrow type");
1116 type
pop_type (type match
)
1119 type t
= pop_raw ();
1120 if (! match
.compatible (t
, this))
1121 verify_fail ("incompatible type on stack");
1125 // Pop a reference type or a return address.
1126 type
pop_ref_or_return ()
1128 type t
= pop_raw ();
1129 if (! t
.isreference () && t
.key
!= return_address_type
)
1130 verify_fail ("expected reference or return address on stack");
1134 void push_type (type t
)
1136 // If T is a numeric type like short, promote it to int.
1139 int depth
= t
.depth ();
1140 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1141 verify_fail ("stack overflow");
1142 current_state
->stack
[current_state
->stacktop
++] = t
;
1143 current_state
->stackdepth
+= depth
;
1146 void set_variable (int index
, type t
)
1148 // If T is a numeric type like short, promote it to int.
1151 int depth
= t
.depth ();
1152 if (index
> current_method
->max_locals
- depth
)
1153 verify_fail ("invalid local variable");
1154 current_state
->locals
[index
] = t
;
1155 current_state
->note_variable (index
);
1159 current_state
->locals
[index
+ 1] = continuation_type
;
1160 current_state
->note_variable (index
+ 1);
1162 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1164 current_state
->locals
[index
- 1] = unsuitable_type
;
1165 // There's no need to call note_variable here.
1169 type
get_variable (int index
, type t
)
1171 int depth
= t
.depth ();
1172 if (index
> current_method
->max_locals
- depth
)
1173 verify_fail ("invalid local variable");
1174 if (! t
.compatible (current_state
->locals
[index
], this))
1175 verify_fail ("incompatible type in local variable");
1178 type
t (continuation_type
);
1179 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1180 verify_fail ("invalid local variable");
1182 return current_state
->locals
[index
];
1185 // Make sure ARRAY is an array type and that its elements are
1186 // compatible with type ELEMENT. Returns the actual element type.
1187 type
require_array_type (type array
, type element
)
1189 // An odd case. Here we just pretend that everything went ok. If
1190 // the requested element type is some kind of reference, return
1191 // the null type instead.
1192 if (array
.isnull ())
1193 return element
.isreference () ? type (null_type
) : element
;
1195 if (! array
.isarray ())
1196 verify_fail ("array required");
1198 type t
= array
.element_type (this);
1199 if (! element
.compatible (t
, this))
1201 // Special case for byte arrays, which must also be boolean
1204 if (element
.key
== byte_type
)
1206 type
e2 (boolean_type
);
1207 ok
= e2
.compatible (t
, this);
1210 verify_fail ("incompatible array element type");
1213 // Return T and not ELEMENT, because T might be specialized.
1219 if (PC
>= current_method
->code_length
)
1220 verify_fail ("premature end of bytecode");
1221 return (jint
) bytecode
[PC
++] & 0xff;
1226 jint b1
= get_byte ();
1227 jint b2
= get_byte ();
1228 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1233 jint b1
= get_byte ();
1234 jint b2
= get_byte ();
1235 jshort s
= (b1
<< 8) | b2
;
1241 jint b1
= get_byte ();
1242 jint b2
= get_byte ();
1243 jint b3
= get_byte ();
1244 jint b4
= get_byte ();
1245 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1248 int compute_jump (int offset
)
1250 int npc
= start_PC
+ offset
;
1251 if (npc
< 0 || npc
>= current_method
->code_length
)
1252 verify_fail ("branch out of range", start_PC
);
1256 // Merge the indicated state into the state at the branch target and
1257 // schedule a new PC if there is a change. If RET_SEMANTICS is
1258 // true, then we are merging from a `ret' instruction into the
1259 // instruction after a `jsr'. This is a special case with its own
1260 // modified semantics.
1261 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1263 bool changed
= true;
1264 if (states
[npc
] == NULL
)
1266 // There's a weird situation here. If are examining the
1267 // branch that results from a `ret', and there is not yet a
1268 // state available at the branch target (the instruction just
1269 // after the `jsr'), then we have to construct a special kind
1270 // of state at that point for future merging. This special
1271 // state has the type `unused_by_subroutine_type' in each slot
1272 // which was not modified by the subroutine.
1273 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1274 current_method
->max_locals
, ret_semantics
);
1275 debug_print ("== New state in push_jump_merge\n");
1276 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1277 current_method
->max_locals
);
1281 debug_print ("== Merge states in push_jump_merge\n");
1282 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1283 current_method
->max_locals
);
1284 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1285 current_method
->max_locals
);
1286 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1287 current_method
->max_locals
, this);
1288 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1289 current_method
->max_locals
);
1292 if (changed
&& states
[npc
]->next
== state::INVALID
)
1294 // The merge changed the state, and the new PC isn't yet on our
1295 // list of PCs to re-verify.
1296 states
[npc
]->next
= next_verify_pc
;
1297 next_verify_pc
= npc
;
1301 void push_jump (int offset
)
1303 int npc
= compute_jump (offset
);
1305 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1306 push_jump_merge (npc
, current_state
);
1309 void push_exception_jump (type t
, int pc
)
1311 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1313 state
s (current_state
, current_method
->max_stack
,
1314 current_method
->max_locals
);
1315 if (current_method
->max_stack
< 1)
1316 verify_fail ("stack overflow at exception handler");
1317 s
.set_exception (t
, current_method
->max_stack
);
1318 push_jump_merge (pc
, &s
);
1323 int *prev_loc
= &next_verify_pc
;
1324 int npc
= next_verify_pc
;
1325 bool skipped
= false;
1327 while (npc
!= state::NO_NEXT
)
1329 // If the next available PC is an unmerged `ret' state, then
1330 // we aren't yet ready to handle it. That's because we would
1331 // need all kind of special cases to do so. So instead we
1332 // defer this jump until after we've processed it via a
1333 // fall-through. This has to happen because the instruction
1334 // before this one must be a `jsr'.
1335 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1337 *prev_loc
= states
[npc
]->next
;
1338 states
[npc
]->next
= state::INVALID
;
1343 prev_loc
= &states
[npc
]->next
;
1344 npc
= states
[npc
]->next
;
1347 // If we've skipped states and there is nothing else, that's a
1350 verify_fail ("pop_jump: can't happen");
1351 return state::NO_NEXT
;
1354 void invalidate_pc ()
1356 PC
= state::NO_NEXT
;
1359 void note_branch_target (int pc
, bool is_jsr_target
= false)
1361 // Don't check `pc <= PC', because we've advanced PC after
1362 // fetching the target and we haven't yet checked the next
1364 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1365 verify_fail ("branch not to instruction start", start_PC
);
1366 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1369 // Record the jsr which called this instruction.
1370 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1372 info
->next
= jsr_ptrs
[pc
];
1373 jsr_ptrs
[pc
] = info
;
1377 void skip_padding ()
1379 while ((PC
% 4) > 0)
1380 if (get_byte () != 0)
1381 verify_fail ("found nonzero padding byte");
1384 // Return the subroutine to which the instruction at PC belongs.
1385 int get_subroutine (int pc
)
1387 if (states
[pc
] == NULL
)
1389 return states
[pc
]->subroutine
;
1392 // Do the work for a `ret' instruction. INDEX is the index into the
1394 void handle_ret_insn (int index
)
1396 get_variable (index
, return_address_type
);
1398 int csub
= current_state
->subroutine
;
1400 verify_fail ("no subroutine");
1402 // Check to see if we've merged subroutines.
1403 subr_entry_info
*entry
;
1404 for (entry
= entry_points
; entry
!= NULL
; entry
= entry
->next
)
1406 if (entry
->ret_pc
== start_PC
)
1411 entry
= (subr_entry_info
*) _Jv_Malloc (sizeof (subr_entry_info
));
1413 entry
->ret_pc
= start_PC
;
1414 entry
->next
= entry_points
;
1415 entry_points
= entry
;
1417 else if (entry
->pc
!= csub
)
1418 verify_fail ("subroutines merged");
1420 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1422 // Temporarily modify the current state so it looks like we're
1423 // in the enclosing context.
1424 current_state
->subroutine
= get_subroutine (subr
->pc
);
1426 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1427 push_jump_merge (subr
->pc
, current_state
, true);
1430 current_state
->subroutine
= csub
;
1434 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1435 // sure this subroutine isn't already on the stack.
1436 void check_nonrecursive_call (int sub
, int dest
)
1441 verify_fail ("recursive subroutine call");
1442 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1443 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1446 void handle_jsr_insn (int offset
)
1448 int npc
= compute_jump (offset
);
1451 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1452 check_nonrecursive_call (current_state
->subroutine
, npc
);
1454 // Create a new state and modify it as appropriate for entry into
1455 // a subroutine. We're writing this in a weird way because,
1456 // unfortunately, push_type only works on the current state.
1457 push_type (return_address_type
);
1458 push_jump_merge (npc
, current_state
);
1459 // Clean up the weirdness.
1460 pop_type (return_address_type
);
1462 // On entry to the subroutine, the subroutine number must be set
1463 // and the locals must be marked as cleared. We do this after
1464 // merging state so that we don't erroneously "notice" a variable
1465 // change merely on entry.
1466 states
[npc
]->enter_subroutine (npc
, current_method
->max_locals
);
1469 jclass
construct_primitive_array_type (type_val prim
)
1475 k
= JvPrimClass (boolean
);
1478 k
= JvPrimClass (char);
1481 k
= JvPrimClass (float);
1484 k
= JvPrimClass (double);
1487 k
= JvPrimClass (byte
);
1490 k
= JvPrimClass (short);
1493 k
= JvPrimClass (int);
1496 k
= JvPrimClass (long);
1499 verify_fail ("unknown type in construct_primitive_array_type");
1501 k
= _Jv_GetArrayClass (k
, NULL
);
1505 // This pass computes the location of branch targets and also
1506 // instruction starts.
1507 void branch_prepass ()
1509 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1510 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1511 * current_method
->code_length
);
1513 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1519 bool last_was_jsr
= false;
1522 while (PC
< current_method
->code_length
)
1524 // Set `start_PC' early so that error checking can have the
1527 flags
[PC
] |= FLAG_INSN_START
;
1529 // If the previous instruction was a jsr, then the next
1530 // instruction is a branch target -- the branch being the
1531 // corresponding `ret'.
1533 note_branch_target (PC
);
1534 last_was_jsr
= false;
1536 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1540 case op_aconst_null
:
1676 case op_monitorenter
:
1677 case op_monitorexit
:
1685 case op_arraylength
:
1717 case op_invokespecial
:
1718 case op_invokestatic
:
1719 case op_invokevirtual
:
1723 case op_multianewarray
:
1729 last_was_jsr
= true;
1748 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1751 case op_tableswitch
:
1754 note_branch_target (compute_jump (get_int ()));
1755 jint low
= get_int ();
1756 jint hi
= get_int ();
1758 verify_fail ("invalid tableswitch", start_PC
);
1759 for (int i
= low
; i
<= hi
; ++i
)
1760 note_branch_target (compute_jump (get_int ()));
1764 case op_lookupswitch
:
1767 note_branch_target (compute_jump (get_int ()));
1768 int npairs
= get_int ();
1770 verify_fail ("too few pairs in lookupswitch", start_PC
);
1771 while (npairs
-- > 0)
1774 note_branch_target (compute_jump (get_int ()));
1779 case op_invokeinterface
:
1787 opcode
= (java_opcode
) get_byte ();
1789 if (opcode
== op_iinc
)
1795 last_was_jsr
= true;
1798 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1802 verify_fail ("unrecognized instruction in branch_prepass",
1806 // See if any previous branch tried to branch to the middle of
1807 // this instruction.
1808 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1810 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1811 verify_fail ("branch to middle of instruction", pc
);
1815 // Verify exception handlers.
1816 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1818 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1819 verify_fail ("exception handler not at instruction start",
1820 exception
[i
].handler_pc
);
1821 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1822 verify_fail ("exception start not at instruction start",
1823 exception
[i
].start_pc
);
1824 if (exception
[i
].end_pc
!= current_method
->code_length
1825 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1826 verify_fail ("exception end not at instruction start",
1827 exception
[i
].end_pc
);
1829 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1833 void check_pool_index (int index
)
1835 if (index
< 0 || index
>= current_class
->constants
.size
)
1836 verify_fail ("constant pool index out of range", start_PC
);
1839 type
check_class_constant (int index
)
1841 check_pool_index (index
);
1842 _Jv_Constants
*pool
= ¤t_class
->constants
;
1843 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1844 return type (pool
->data
[index
].clazz
);
1845 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1846 return type (pool
->data
[index
].utf8
);
1847 verify_fail ("expected class constant", start_PC
);
1850 type
check_constant (int index
)
1852 check_pool_index (index
);
1853 _Jv_Constants
*pool
= ¤t_class
->constants
;
1854 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1855 || pool
->tags
[index
] == JV_CONSTANT_String
)
1856 return type (&java::lang::String::class$
);
1857 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1858 return type (int_type
);
1859 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1860 return type (float_type
);
1861 verify_fail ("String, int, or float constant expected", start_PC
);
1864 type
check_wide_constant (int index
)
1866 check_pool_index (index
);
1867 _Jv_Constants
*pool
= ¤t_class
->constants
;
1868 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1869 return type (long_type
);
1870 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1871 return type (double_type
);
1872 verify_fail ("long or double constant expected", start_PC
);
1875 // Helper for both field and method. These are laid out the same in
1876 // the constant pool.
1877 type
handle_field_or_method (int index
, int expected
,
1878 _Jv_Utf8Const
**name
,
1879 _Jv_Utf8Const
**fmtype
)
1881 check_pool_index (index
);
1882 _Jv_Constants
*pool
= ¤t_class
->constants
;
1883 if (pool
->tags
[index
] != expected
)
1884 verify_fail ("didn't see expected constant", start_PC
);
1885 // Once we know we have a Fieldref or Methodref we assume that it
1886 // is correctly laid out in the constant pool. I think the code
1887 // in defineclass.cc guarantees this.
1888 _Jv_ushort class_index
, name_and_type_index
;
1889 _Jv_loadIndexes (&pool
->data
[index
],
1891 name_and_type_index
);
1892 _Jv_ushort name_index
, desc_index
;
1893 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1894 name_index
, desc_index
);
1896 *name
= pool
->data
[name_index
].utf8
;
1897 *fmtype
= pool
->data
[desc_index
].utf8
;
1899 return check_class_constant (class_index
);
1902 // Return field's type, compute class' type if requested.
1903 type
check_field_constant (int index
, type
*class_type
= NULL
)
1905 _Jv_Utf8Const
*name
, *field_type
;
1906 type ct
= handle_field_or_method (index
,
1907 JV_CONSTANT_Fieldref
,
1908 &name
, &field_type
);
1911 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1912 return type (field_type
);
1913 return get_type_val_for_signature (field_type
->data
[0]);
1916 type
check_method_constant (int index
, bool is_interface
,
1917 _Jv_Utf8Const
**method_name
,
1918 _Jv_Utf8Const
**method_signature
)
1920 return handle_field_or_method (index
,
1922 ? JV_CONSTANT_InterfaceMethodref
1923 : JV_CONSTANT_Methodref
),
1924 method_name
, method_signature
);
1927 type
get_one_type (char *&p
)
1945 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1949 // Casting to jchar here is ok since we are looking at an ASCII
1951 type_val rt
= get_type_val_for_signature (jchar (v
));
1953 if (arraycount
== 0)
1955 // Callers of this function eventually push their arguments on
1956 // the stack. So, promote them here.
1957 return type (rt
).promote ();
1960 jclass k
= construct_primitive_array_type (rt
);
1961 while (--arraycount
> 0)
1962 k
= _Jv_GetArrayClass (k
, NULL
);
1966 void compute_argument_types (_Jv_Utf8Const
*signature
,
1969 char *p
= signature
->data
;
1975 types
[i
++] = get_one_type (p
);
1978 type
compute_return_type (_Jv_Utf8Const
*signature
)
1980 char *p
= signature
->data
;
1984 return get_one_type (p
);
1987 void check_return_type (type onstack
)
1989 type rt
= compute_return_type (current_method
->self
->signature
);
1990 if (! rt
.compatible (onstack
, this))
1991 verify_fail ("incompatible return type");
1994 // Initialize the stack for the new method. Returns true if this
1995 // method is an instance initializer.
1996 bool initialize_stack ()
1999 bool is_init
= false;
2001 using namespace java::lang::reflect
;
2002 if (! Modifier::isStatic (current_method
->self
->accflags
))
2004 type
kurr (current_class
);
2005 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
2007 kurr
.set_uninitialized (type::SELF
, this);
2010 set_variable (0, kurr
);
2011 current_state
->set_this_type (kurr
);
2015 // We have to handle wide arguments specially here.
2016 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2017 type arg_types
[arg_count
];
2018 compute_argument_types (current_method
->self
->signature
, arg_types
);
2019 for (int i
= 0; i
< arg_count
; ++i
)
2021 set_variable (var
, arg_types
[i
]);
2023 if (arg_types
[i
].iswide ())
2030 void verify_instructions_0 ()
2032 current_state
= new state (current_method
->max_stack
,
2033 current_method
->max_locals
);
2038 // True if we are verifying an instance initializer.
2039 bool this_is_init
= initialize_stack ();
2041 states
= (state
**) _Jv_Malloc (sizeof (state
*)
2042 * current_method
->code_length
);
2043 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2046 next_verify_pc
= state::NO_NEXT
;
2050 // If the PC was invalidated, get a new one from the work list.
2051 if (PC
== state::NO_NEXT
)
2054 if (PC
== state::INVALID
)
2055 verify_fail ("can't happen: saw state::INVALID");
2056 if (PC
== state::NO_NEXT
)
2058 // Set up the current state.
2059 current_state
->copy (states
[PC
], current_method
->max_stack
,
2060 current_method
->max_locals
);
2064 // Control can't fall off the end of the bytecode. We
2065 // only need to check this in the fall-through case,
2066 // because branch bounds are checked when they are
2068 if (PC
>= current_method
->code_length
)
2069 verify_fail ("fell off end");
2071 // We only have to do this checking in the situation where
2072 // control flow falls through from the previous
2073 // instruction. Otherwise merging is done at the time we
2075 if (states
[PC
] != NULL
)
2077 // We've already visited this instruction. So merge
2078 // the states together. If this yields no change then
2079 // we don't have to re-verify. However, if the new
2080 // state is an the result of an unmerged `ret', we
2081 // must continue through it.
2082 debug_print ("== Fall through merge\n");
2083 states
[PC
]->print ("Old", PC
, current_method
->max_stack
,
2084 current_method
->max_locals
);
2085 current_state
->print ("Cur", PC
, current_method
->max_stack
,
2086 current_method
->max_locals
);
2087 if (! current_state
->merge (states
[PC
], false,
2088 current_method
->max_locals
, this)
2089 && ! states
[PC
]->is_unmerged_ret_state (current_method
->max_locals
))
2091 debug_print ("== Fall through optimization\n");
2095 // Save a copy of it for later.
2096 states
[PC
]->copy (current_state
, current_method
->max_stack
,
2097 current_method
->max_locals
);
2098 current_state
->print ("New", PC
, current_method
->max_stack
,
2099 current_method
->max_locals
);
2103 // We only have to keep saved state at branch targets. If
2104 // we're at a branch target and the state here hasn't been set
2105 // yet, we set it now.
2106 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2108 states
[PC
] = new state (current_state
, current_method
->max_stack
,
2109 current_method
->max_locals
);
2112 // Set this before handling exceptions so that debug output is
2116 // Update states for all active exception handlers. Ordinarily
2117 // there are not many exception handlers. So we simply run
2118 // through them all.
2119 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2121 if (PC
>= exception
[i
].start_pc
&& PC
< exception
[i
].end_pc
)
2123 type
handler (&java::lang::Throwable::class$
);
2124 if (exception
[i
].handler_type
!= 0)
2125 handler
= check_class_constant (exception
[i
].handler_type
);
2126 push_exception_jump (handler
, exception
[i
].handler_pc
);
2130 current_state
->print (" ", PC
, current_method
->max_stack
,
2131 current_method
->max_locals
);
2132 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2138 case op_aconst_null
:
2139 push_type (null_type
);
2149 push_type (int_type
);
2154 push_type (long_type
);
2160 push_type (float_type
);
2165 push_type (double_type
);
2170 push_type (int_type
);
2175 push_type (int_type
);
2179 push_type (check_constant (get_byte ()));
2182 push_type (check_constant (get_ushort ()));
2185 push_type (check_wide_constant (get_ushort ()));
2189 push_type (get_variable (get_byte (), int_type
));
2192 push_type (get_variable (get_byte (), long_type
));
2195 push_type (get_variable (get_byte (), float_type
));
2198 push_type (get_variable (get_byte (), double_type
));
2201 push_type (get_variable (get_byte (), reference_type
));
2208 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2214 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2220 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2226 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2232 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2235 pop_type (int_type
);
2236 push_type (require_array_type (pop_type (reference_type
),
2240 pop_type (int_type
);
2241 push_type (require_array_type (pop_type (reference_type
),
2245 pop_type (int_type
);
2246 push_type (require_array_type (pop_type (reference_type
),
2250 pop_type (int_type
);
2251 push_type (require_array_type (pop_type (reference_type
),
2255 pop_type (int_type
);
2256 push_type (require_array_type (pop_type (reference_type
),
2260 pop_type (int_type
);
2261 require_array_type (pop_type (reference_type
), byte_type
);
2262 push_type (int_type
);
2265 pop_type (int_type
);
2266 require_array_type (pop_type (reference_type
), char_type
);
2267 push_type (int_type
);
2270 pop_type (int_type
);
2271 require_array_type (pop_type (reference_type
), short_type
);
2272 push_type (int_type
);
2275 set_variable (get_byte (), pop_type (int_type
));
2278 set_variable (get_byte (), pop_type (long_type
));
2281 set_variable (get_byte (), pop_type (float_type
));
2284 set_variable (get_byte (), pop_type (double_type
));
2287 set_variable (get_byte (), pop_ref_or_return ());
2293 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2299 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2305 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2311 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2317 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2320 pop_type (int_type
);
2321 pop_type (int_type
);
2322 require_array_type (pop_type (reference_type
), int_type
);
2325 pop_type (long_type
);
2326 pop_type (int_type
);
2327 require_array_type (pop_type (reference_type
), long_type
);
2330 pop_type (float_type
);
2331 pop_type (int_type
);
2332 require_array_type (pop_type (reference_type
), float_type
);
2335 pop_type (double_type
);
2336 pop_type (int_type
);
2337 require_array_type (pop_type (reference_type
), double_type
);
2340 pop_type (reference_type
);
2341 pop_type (int_type
);
2342 require_array_type (pop_type (reference_type
), reference_type
);
2345 pop_type (int_type
);
2346 pop_type (int_type
);
2347 require_array_type (pop_type (reference_type
), byte_type
);
2350 pop_type (int_type
);
2351 pop_type (int_type
);
2352 require_array_type (pop_type (reference_type
), char_type
);
2355 pop_type (int_type
);
2356 pop_type (int_type
);
2357 require_array_type (pop_type (reference_type
), short_type
);
2384 type t2
= pop_raw ();
2399 type t
= pop_raw ();
2414 type t1
= pop_raw ();
2431 type t1
= pop_raw ();
2434 type t2
= pop_raw ();
2452 type t3
= pop_raw ();
2490 pop_type (int_type
);
2491 push_type (pop_type (int_type
));
2501 pop_type (long_type
);
2502 push_type (pop_type (long_type
));
2507 pop_type (int_type
);
2508 push_type (pop_type (long_type
));
2515 pop_type (float_type
);
2516 push_type (pop_type (float_type
));
2523 pop_type (double_type
);
2524 push_type (pop_type (double_type
));
2530 push_type (pop_type (int_type
));
2533 push_type (pop_type (long_type
));
2536 push_type (pop_type (float_type
));
2539 push_type (pop_type (double_type
));
2542 get_variable (get_byte (), int_type
);
2546 pop_type (int_type
);
2547 push_type (long_type
);
2550 pop_type (int_type
);
2551 push_type (float_type
);
2554 pop_type (int_type
);
2555 push_type (double_type
);
2558 pop_type (long_type
);
2559 push_type (int_type
);
2562 pop_type (long_type
);
2563 push_type (float_type
);
2566 pop_type (long_type
);
2567 push_type (double_type
);
2570 pop_type (float_type
);
2571 push_type (int_type
);
2574 pop_type (float_type
);
2575 push_type (long_type
);
2578 pop_type (float_type
);
2579 push_type (double_type
);
2582 pop_type (double_type
);
2583 push_type (int_type
);
2586 pop_type (double_type
);
2587 push_type (long_type
);
2590 pop_type (double_type
);
2591 push_type (float_type
);
2594 pop_type (long_type
);
2595 pop_type (long_type
);
2596 push_type (int_type
);
2600 pop_type (float_type
);
2601 pop_type (float_type
);
2602 push_type (int_type
);
2606 pop_type (double_type
);
2607 pop_type (double_type
);
2608 push_type (int_type
);
2616 pop_type (int_type
);
2617 push_jump (get_short ());
2625 pop_type (int_type
);
2626 pop_type (int_type
);
2627 push_jump (get_short ());
2631 pop_type (reference_type
);
2632 pop_type (reference_type
);
2633 push_jump (get_short ());
2636 push_jump (get_short ());
2640 handle_jsr_insn (get_short ());
2643 handle_ret_insn (get_byte ());
2645 case op_tableswitch
:
2647 pop_type (int_type
);
2649 push_jump (get_int ());
2650 jint low
= get_int ();
2651 jint high
= get_int ();
2652 // Already checked LOW -vs- HIGH.
2653 for (int i
= low
; i
<= high
; ++i
)
2654 push_jump (get_int ());
2659 case op_lookupswitch
:
2661 pop_type (int_type
);
2663 push_jump (get_int ());
2664 jint npairs
= get_int ();
2665 // Already checked NPAIRS >= 0.
2667 for (int i
= 0; i
< npairs
; ++i
)
2669 jint key
= get_int ();
2670 if (i
> 0 && key
<= lastkey
)
2671 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2673 push_jump (get_int ());
2679 check_return_type (pop_type (int_type
));
2683 check_return_type (pop_type (long_type
));
2687 check_return_type (pop_type (float_type
));
2691 check_return_type (pop_type (double_type
));
2695 check_return_type (pop_type (reference_type
));
2699 // We only need to check this when the return type is
2700 // void, because all instance initializers return void.
2702 current_state
->check_this_initialized (this);
2703 check_return_type (void_type
);
2707 push_type (check_field_constant (get_ushort ()));
2710 pop_type (check_field_constant (get_ushort ()));
2715 type field
= check_field_constant (get_ushort (), &klass
);
2723 type field
= check_field_constant (get_ushort (), &klass
);
2726 // We have an obscure special case here: we can use
2727 // `putfield' on a field declared in this class, even if
2728 // `this' has not yet been initialized.
2729 if (! current_state
->this_type
.isinitialized ()
2730 && current_state
->this_type
.pc
== type::SELF
)
2731 klass
.set_uninitialized (type::SELF
, this);
2736 case op_invokevirtual
:
2737 case op_invokespecial
:
2738 case op_invokestatic
:
2739 case op_invokeinterface
:
2741 _Jv_Utf8Const
*method_name
, *method_signature
;
2743 = check_method_constant (get_ushort (),
2744 opcode
== op_invokeinterface
,
2747 // NARGS is only used when we're processing
2748 // invokeinterface. It is simplest for us to compute it
2749 // here and then verify it later.
2751 if (opcode
== op_invokeinterface
)
2753 nargs
= get_byte ();
2754 if (get_byte () != 0)
2755 verify_fail ("invokeinterface dummy byte is wrong");
2758 bool is_init
= false;
2759 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2762 if (opcode
!= op_invokespecial
)
2763 verify_fail ("can't invoke <init>");
2765 else if (method_name
->data
[0] == '<')
2766 verify_fail ("can't invoke method starting with `<'");
2768 // Pop arguments and check types.
2769 int arg_count
= _Jv_count_arguments (method_signature
);
2770 type arg_types
[arg_count
];
2771 compute_argument_types (method_signature
, arg_types
);
2772 for (int i
= arg_count
- 1; i
>= 0; --i
)
2774 // This is only used for verifying the byte for
2776 nargs
-= arg_types
[i
].depth ();
2777 pop_type (arg_types
[i
]);
2780 if (opcode
== op_invokeinterface
2782 verify_fail ("wrong argument count for invokeinterface");
2784 if (opcode
!= op_invokestatic
)
2786 type t
= class_type
;
2789 // In this case the PC doesn't matter.
2790 t
.set_uninitialized (type::UNINIT
, this);
2792 type raw
= pop_raw ();
2794 if (t
.compatible (raw
, this))
2798 else if (opcode
== op_invokeinterface
)
2800 // This is a hack. We might have merged two
2801 // items and gotten `Object'. This can happen
2802 // because we don't keep track of where merges
2803 // come from. This is safe as long as the
2804 // interpreter checks interfaces at runtime.
2805 type
obj (&java::lang::Object::class$
);
2806 ok
= raw
.compatible (obj
, this);
2810 verify_fail ("incompatible type on stack");
2813 current_state
->set_initialized (raw
.get_pc (),
2814 current_method
->max_locals
);
2817 type rt
= compute_return_type (method_signature
);
2825 type t
= check_class_constant (get_ushort ());
2826 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2827 verify_fail ("type is array, interface, or abstract");
2828 t
.set_uninitialized (start_PC
, this);
2835 int atype
= get_byte ();
2836 // We intentionally have chosen constants to make this
2838 if (atype
< boolean_type
|| atype
> long_type
)
2839 verify_fail ("type not primitive", start_PC
);
2840 pop_type (int_type
);
2841 push_type (construct_primitive_array_type (type_val (atype
)));
2845 pop_type (int_type
);
2846 push_type (check_class_constant (get_ushort ()).to_array (this));
2848 case op_arraylength
:
2850 type t
= pop_type (reference_type
);
2851 if (! t
.isarray () && ! t
.isnull ())
2852 verify_fail ("array type expected");
2853 push_type (int_type
);
2857 pop_type (type (&java::lang::Throwable::class$
));
2861 pop_type (reference_type
);
2862 push_type (check_class_constant (get_ushort ()));
2865 pop_type (reference_type
);
2866 check_class_constant (get_ushort ());
2867 push_type (int_type
);
2869 case op_monitorenter
:
2870 pop_type (reference_type
);
2872 case op_monitorexit
:
2873 pop_type (reference_type
);
2877 switch (get_byte ())
2880 push_type (get_variable (get_ushort (), int_type
));
2883 push_type (get_variable (get_ushort (), long_type
));
2886 push_type (get_variable (get_ushort (), float_type
));
2889 push_type (get_variable (get_ushort (), double_type
));
2892 push_type (get_variable (get_ushort (), reference_type
));
2895 set_variable (get_ushort (), pop_type (int_type
));
2898 set_variable (get_ushort (), pop_type (long_type
));
2901 set_variable (get_ushort (), pop_type (float_type
));
2904 set_variable (get_ushort (), pop_type (double_type
));
2907 set_variable (get_ushort (), pop_type (reference_type
));
2910 handle_ret_insn (get_short ());
2913 get_variable (get_ushort (), int_type
);
2917 verify_fail ("unrecognized wide instruction", start_PC
);
2921 case op_multianewarray
:
2923 type atype
= check_class_constant (get_ushort ());
2924 int dim
= get_byte ();
2926 verify_fail ("too few dimensions to multianewarray", start_PC
);
2927 atype
.verify_dimensions (dim
, this);
2928 for (int i
= 0; i
< dim
; ++i
)
2929 pop_type (int_type
);
2935 pop_type (reference_type
);
2936 push_jump (get_short ());
2939 push_jump (get_int ());
2943 handle_jsr_insn (get_int ());
2947 // Unrecognized opcode.
2948 verify_fail ("unrecognized instruction in verify_instructions_0",
2954 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
2956 using namespace java::lang
;
2957 StringBuffer
*buf
= new StringBuffer ();
2959 buf
->append (JvNewStringLatin1 ("verification failed"));
2964 buf
->append (JvNewStringLatin1 (" at PC "));
2968 _Jv_InterpMethod
*method
= current_method
;
2969 buf
->append (JvNewStringLatin1 (" in "));
2970 buf
->append (current_class
->getName());
2971 buf
->append ((jchar
) ':');
2972 buf
->append (JvNewStringUTF (method
->get_method()->name
->data
));
2973 buf
->append ((jchar
) '(');
2974 buf
->append (JvNewStringUTF (method
->get_method()->signature
->data
));
2975 buf
->append ((jchar
) ')');
2977 buf
->append (JvNewStringLatin1 (": "));
2978 buf
->append (JvNewStringLatin1 (s
));
2979 throw new java::lang::VerifyError (buf
->toString ());
2984 void verify_instructions ()
2987 verify_instructions_0 ();
2990 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
2992 // We just print the text as utf-8. This is just for debugging
2994 debug_print ("--------------------------------\n");
2995 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
2998 bytecode
= m
->bytecode ();
2999 exception
= m
->exceptions ();
3000 current_class
= m
->defining_class
;
3006 entry_points
= NULL
;
3009 ~_Jv_BytecodeVerifier ()
3018 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3020 if (jsr_ptrs
[i
] != NULL
)
3022 subr_info
*info
= jsr_ptrs
[i
];
3023 while (info
!= NULL
)
3025 subr_info
*next
= info
->next
;
3031 _Jv_Free (jsr_ptrs
);
3034 while (utf8_list
!= NULL
)
3036 linked_utf8
*n
= utf8_list
->next
;
3037 _Jv_Free (utf8_list
->val
);
3038 _Jv_Free (utf8_list
);
3042 while (entry_points
!= NULL
)
3044 subr_entry_info
*next
= entry_points
->next
;
3045 _Jv_Free (entry_points
);
3046 entry_points
= next
;
3052 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3054 _Jv_BytecodeVerifier
v (meth
);
3055 v
.verify_instructions ();
3057 #endif /* INTERPRETER */