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 bytecode itself.
94 unsigned char *bytecode
;
96 _Jv_InterpException
*exception
;
101 _Jv_InterpMethod
*current_method
;
103 // A linked list of utf8 objects we allocate. This is really ugly,
104 // but without this our utf8 objects would be collected.
105 linked_utf8
*utf8_list
;
113 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
115 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
116 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
119 r
->length
= val
->length
;
121 memcpy (r
->data
, val
->data
, val
->length
+ 1);
123 linked_utf8
*lu
= (linked_utf8
*) _Jv_Malloc (sizeof (linked_utf8
));
125 lu
->next
= utf8_list
;
131 // This enum holds a list of tags for all the different types we
132 // need to handle. Reference types are treated specially by the
138 // The values for primitive types are chosen to correspond to values
139 // specified to newarray.
149 // Used when overwriting second word of a double or long in the
150 // local variables. Also used after merging local variable states
151 // to indicate an unusable value.
156 // There is an obscure special case which requires us to note when
157 // a local variable has not been used by a subroutine. See
158 // push_jump_merge for more information.
159 unused_by_subroutine_type
,
161 // Everything after `reference_type' must be a reference type.
164 unresolved_reference_type
,
165 uninitialized_reference_type
,
166 uninitialized_unresolved_reference_type
169 // Return the type_val corresponding to a primitive signature
170 // character. For instance `I' returns `int.class'.
171 type_val
get_type_val_for_signature (jchar sig
)
204 verify_fail ("invalid signature");
209 // Return the type_val corresponding to a primitive class.
210 type_val
get_type_val_for_signature (jclass k
)
212 return get_type_val_for_signature ((jchar
) k
->method_count
);
215 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
216 // TARGET haven't been prepared.
217 static bool is_assignable_from_slow (jclass target
, jclass source
)
219 // This will terminate when SOURCE==Object.
222 if (source
== target
)
225 if (target
->isPrimitive () || source
->isPrimitive ())
228 if (target
->isArray ())
230 if (! source
->isArray ())
232 target
= target
->getComponentType ();
233 source
= source
->getComponentType ();
235 else if (target
->isInterface ())
237 for (int i
= 0; i
< source
->interface_count
; ++i
)
239 // We use a recursive call because we also need to
240 // check superinterfaces.
241 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
244 source
= source
->getSuperclass ();
248 // We must do this check before we check to see if SOURCE is
249 // an interface. This way we know that any interface is
250 // assignable to an Object.
251 else if (target
== &java::lang::Object::class$
)
253 else if (source
->isInterface ())
255 for (int i
= 0; i
< target
->interface_count
; ++i
)
257 // We use a recursive call because we also need to
258 // check superinterfaces.
259 if (is_assignable_from_slow (target
->interfaces
[i
], source
))
262 target
= target
->getSuperclass ();
266 else if (source
== &java::lang::Object::class$
)
269 source
= source
->getSuperclass ();
273 // This is used to keep track of which `jsr's correspond to a given
277 // PC of the instruction just after the jsr.
283 // This is used to keep track of which subroutine entry point
284 // corresponds to which `ret' instruction.
285 struct subr_entry_info
287 // PC of the subroutine entry point.
289 // PC of the `ret' instruction.
292 subr_entry_info
*next
;
295 // The `type' class is used to represent a single type in the
301 // Some associated data.
304 // For a resolved reference type, this is a pointer to the class.
306 // For other reference types, this it the name of the class.
309 // This is used when constructing a new object. It is the PC of the
310 // `new' instruction which created the object. We use the special
311 // value -2 to mean that this is uninitialized, and the special
312 // value -1 for the case where the current method is itself the
316 static const int UNINIT
= -2;
317 static const int SELF
= -1;
319 // Basic constructor.
322 key
= unsuitable_type
;
327 // Make a new instance given the type tag. We assume a generic
328 // `reference_type' means Object.
333 if (key
== reference_type
)
334 data
.klass
= &java::lang::Object::class$
;
338 // Make a new instance given a class.
341 key
= reference_type
;
346 // Make a new instance given the name of a class.
347 type (_Jv_Utf8Const
*n
)
349 key
= unresolved_reference_type
;
362 // These operators are required because libgcj can't link in
364 void *operator new[] (size_t bytes
)
366 return _Jv_Malloc (bytes
);
369 void operator delete[] (void *mem
)
374 type
& operator= (type_val k
)
382 type
& operator= (const type
& t
)
390 // Promote a numeric type.
393 if (key
== boolean_type
|| key
== char_type
394 || key
== byte_type
|| key
== short_type
)
399 // If *THIS is an unresolved reference type, resolve it.
400 void resolve (_Jv_BytecodeVerifier
*verifier
)
402 if (key
!= unresolved_reference_type
403 && key
!= uninitialized_unresolved_reference_type
)
406 using namespace java::lang
;
407 java::lang::ClassLoader
*loader
408 = verifier
->current_class
->getClassLoader();
409 // We might see either kind of name. Sigh.
410 if (data
.name
->data
[0] == 'L'
411 && data
.name
->data
[data
.name
->length
- 1] == ';')
412 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
414 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
416 key
= (key
== unresolved_reference_type
418 : uninitialized_reference_type
);
421 // Mark this type as the uninitialized result of `new'.
422 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
424 if (key
== reference_type
)
425 key
= uninitialized_reference_type
;
426 else if (key
== unresolved_reference_type
)
427 key
= uninitialized_unresolved_reference_type
;
429 verifier
->verify_fail ("internal error in type::uninitialized");
433 // Mark this type as now initialized.
434 void set_initialized (int npc
)
436 if (npc
!= UNINIT
&& pc
== npc
437 && (key
== uninitialized_reference_type
438 || key
== uninitialized_unresolved_reference_type
))
440 key
= (key
== uninitialized_reference_type
442 : unresolved_reference_type
);
448 // Return true if an object of type K can be assigned to a variable
449 // of type *THIS. Handle various special cases too. Might modify
450 // *THIS or K. Note however that this does not perform numeric
452 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
454 // Any type is compatible with the unsuitable type.
455 if (key
== unsuitable_type
)
458 if (key
< reference_type
|| k
.key
< reference_type
)
461 // The `null' type is convertible to any reference type.
462 // FIXME: is this correct for THIS?
463 if (key
== null_type
|| k
.key
== null_type
)
466 // Any reference type is convertible to Object. This is a special
467 // case so we don't need to unnecessarily resolve a class.
468 if (key
== reference_type
469 && data
.klass
== &java::lang::Object::class$
)
472 // An initialized type and an uninitialized type are not
474 if (isinitialized () != k
.isinitialized ())
477 // Two uninitialized objects are compatible if either:
478 // * The PCs are identical, or
479 // * One PC is UNINIT.
480 if (! isinitialized ())
482 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
486 // Two unresolved types are equal if their names are the same.
489 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
492 // We must resolve both types and check assignability.
494 k
.resolve (verifier
);
495 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
500 return key
== void_type
;
505 return key
== long_type
|| key
== double_type
;
508 // Return number of stack or local variable slots taken by this
512 return iswide () ? 2 : 1;
515 bool isarray () const
517 // We treat null_type as not an array. This is ok based on the
518 // current uses of this method.
519 if (key
== reference_type
)
520 return data
.klass
->isArray ();
521 else if (key
== unresolved_reference_type
)
522 return data
.name
->data
[0] == '[';
528 return key
== null_type
;
531 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
534 if (key
!= reference_type
)
536 return data
.klass
->isInterface ();
539 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
542 if (key
!= reference_type
)
544 using namespace java::lang::reflect
;
545 return Modifier::isAbstract (data
.klass
->getModifiers ());
548 // Return the element type of an array.
549 type
element_type (_Jv_BytecodeVerifier
*verifier
)
551 // FIXME: maybe should do string manipulation here.
553 if (key
!= reference_type
)
554 verifier
->verify_fail ("programmer error in type::element_type()", -1);
556 jclass k
= data
.klass
->getComponentType ();
557 if (k
->isPrimitive ())
558 return type (verifier
->get_type_val_for_signature (k
));
562 // Return the array type corresponding to an initialized
563 // reference. We could expand this to work for other kinds of
564 // types, but currently we don't need to.
565 type
to_array (_Jv_BytecodeVerifier
*verifier
)
567 // Resolving isn't ideal, because it might force us to load
568 // another class, but it's easy. FIXME?
569 if (key
== unresolved_reference_type
)
572 if (key
== reference_type
)
573 return type (_Jv_GetArrayClass (data
.klass
,
574 data
.klass
->getClassLoader ()));
576 verifier
->verify_fail ("internal error in type::to_array()");
579 bool isreference () const
581 return key
>= reference_type
;
589 bool isinitialized () const
591 return (key
== reference_type
593 || key
== unresolved_reference_type
);
596 bool isresolved () const
598 return (key
== reference_type
600 || key
== uninitialized_reference_type
);
603 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
605 // The way this is written, we don't need to check isarray().
606 if (key
== reference_type
)
608 jclass k
= data
.klass
;
609 while (k
->isArray () && ndims
> 0)
611 k
= k
->getComponentType ();
617 // We know KEY == unresolved_reference_type.
618 char *p
= data
.name
->data
;
619 while (*p
++ == '[' && ndims
-- > 0)
624 verifier
->verify_fail ("array type has fewer dimensions than required");
627 // Merge OLD_TYPE into this. On error throw exception.
628 bool merge (type
& old_type
, bool local_semantics
,
629 _Jv_BytecodeVerifier
*verifier
)
631 bool changed
= false;
632 bool refo
= old_type
.isreference ();
633 bool refn
= isreference ();
636 if (old_type
.key
== null_type
)
638 else if (key
== null_type
)
643 else if (isinitialized () != old_type
.isinitialized ())
644 verifier
->verify_fail ("merging initialized and uninitialized types");
647 if (! isinitialized ())
651 else if (old_type
.pc
== UNINIT
)
653 else if (pc
!= old_type
.pc
)
654 verifier
->verify_fail ("merging different uninitialized types");
658 && ! old_type
.isresolved ()
659 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
661 // Types are identical.
666 old_type
.resolve (verifier
);
668 jclass k
= data
.klass
;
669 jclass oldk
= old_type
.data
.klass
;
672 while (k
->isArray () && oldk
->isArray ())
675 k
= k
->getComponentType ();
676 oldk
= oldk
->getComponentType ();
679 // Ordinarily this terminates when we hit Object...
682 if (is_assignable_from_slow (k
, oldk
))
684 k
= k
->getSuperclass ();
687 // ... but K could have been an interface, in which
688 // case we'll end up here. We just convert this
691 k
= &java::lang::Object::class$
;
695 while (arraycount
> 0)
697 java::lang::ClassLoader
*loader
698 = verifier
->current_class
->getClassLoader();
699 k
= _Jv_GetArrayClass (k
, loader
);
707 else if (refo
|| refn
|| key
!= old_type
.key
)
711 // If we're merging into an "unused" slot, then we
712 // simply accept whatever we're merging from.
713 if (key
== unused_by_subroutine_type
)
718 else if (old_type
.key
== unused_by_subroutine_type
)
722 // If we already have an `unsuitable' type, then we
723 // don't need to change again.
724 else if (key
!= unsuitable_type
)
726 key
= unsuitable_type
;
731 verifier
->verify_fail ("unmergeable type");
737 void print (void) const
742 case boolean_type
: c
= 'Z'; break;
743 case byte_type
: c
= 'B'; break;
744 case char_type
: c
= 'C'; break;
745 case short_type
: c
= 'S'; break;
746 case int_type
: c
= 'I'; break;
747 case long_type
: c
= 'J'; break;
748 case float_type
: c
= 'F'; break;
749 case double_type
: c
= 'D'; break;
750 case void_type
: c
= 'V'; break;
751 case unsuitable_type
: c
= '-'; break;
752 case return_address_type
: c
= 'r'; break;
753 case continuation_type
: c
= '+'; break;
754 case unused_by_subroutine_type
: c
= '_'; break;
755 case reference_type
: c
= 'L'; break;
756 case null_type
: c
= '@'; break;
757 case unresolved_reference_type
: c
= 'l'; break;
758 case uninitialized_reference_type
: c
= 'U'; break;
759 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
761 debug_print ("%c", c
);
763 #endif /* VERIFY_DEBUG */
766 // This class holds all the state information we need for a given
770 // The current top of the stack, in terms of slots.
772 // The current depth of the stack. This will be larger than
773 // STACKTOP when wide types are on the stack.
777 // The local variables.
779 // This is used in subroutines to keep track of which local
780 // variables have been accessed.
782 // If not 0, then we are in a subroutine. The value is the PC of
783 // the subroutine's entry point. We can use 0 as an exceptional
784 // value because PC=0 can never be a subroutine.
786 // This is used to keep a linked list of all the states which
787 // require re-verification. We use the PC to keep track.
789 // We keep track of the type of `this' specially. This is used to
790 // ensure that an instance initializer invokes another initializer
791 // on `this' before returning. We must keep track of this
792 // specially because otherwise we might be confused by code which
793 // assigns to locals[0] (overwriting `this') and then returns
794 // without really initializing.
797 // INVALID marks a state which is not on the linked list of states
798 // requiring reverification.
799 static const int INVALID
= -1;
800 // NO_NEXT marks the state at the end of the reverification list.
801 static const int NO_NEXT
= -2;
803 // This is used to mark the stack depth at the instruction just
804 // after a `jsr' when we haven't yet processed the corresponding
805 // `ret'. See handle_jsr_insn for more information.
806 static const int NO_STACK
= -1;
813 local_changed
= NULL
;
816 state (int max_stack
, int max_locals
)
821 stack
= new type
[max_stack
];
822 for (int i
= 0; i
< max_stack
; ++i
)
823 stack
[i
] = unsuitable_type
;
824 locals
= new type
[max_locals
];
825 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
826 for (int i
= 0; i
< max_locals
; ++i
)
828 locals
[i
] = unsuitable_type
;
829 local_changed
[i
] = false;
835 state (const state
*orig
, int max_stack
, int max_locals
,
836 bool ret_semantics
= false)
838 stack
= new type
[max_stack
];
839 locals
= new type
[max_locals
];
840 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
841 copy (orig
, max_stack
, max_locals
, ret_semantics
);
852 _Jv_Free (local_changed
);
855 void *operator new[] (size_t bytes
)
857 return _Jv_Malloc (bytes
);
860 void operator delete[] (void *mem
)
865 void *operator new (size_t bytes
)
867 return _Jv_Malloc (bytes
);
870 void operator delete (void *mem
)
875 void copy (const state
*copy
, int max_stack
, int max_locals
,
876 bool ret_semantics
= false)
878 stacktop
= copy
->stacktop
;
879 stackdepth
= copy
->stackdepth
;
880 subroutine
= copy
->subroutine
;
881 for (int i
= 0; i
< max_stack
; ++i
)
882 stack
[i
] = copy
->stack
[i
];
883 for (int i
= 0; i
< max_locals
; ++i
)
885 // See push_jump_merge to understand this case.
887 locals
[i
] = type (copy
->local_changed
[i
]
889 : unused_by_subroutine_type
);
891 locals
[i
] = copy
->locals
[i
];
892 local_changed
[i
] = copy
->local_changed
[i
];
894 this_type
= copy
->this_type
;
895 // Don't modify `next'.
898 // Modify this state to reflect entry to an exception handler.
899 void set_exception (type t
, int max_stack
)
904 for (int i
= stacktop
; i
< max_stack
; ++i
)
905 stack
[i
] = unsuitable_type
;
908 // Modify this state to reflect entry into a subroutine.
909 void enter_subroutine (int npc
, int max_locals
)
912 // Mark all items as unchanged. Each subroutine needs to keep
913 // track of its `changed' state independently. In the case of
914 // nested subroutines, this information will be merged back into
915 // parent by the `ret'.
916 for (int i
= 0; i
< max_locals
; ++i
)
917 local_changed
[i
] = false;
920 // Merge STATE_OLD into this state. Destructively modifies this
921 // state. Returns true if the new state was in fact changed.
922 // Will throw an exception if the states are not mergeable.
923 bool merge (state
*state_old
, bool ret_semantics
,
924 int max_locals
, _Jv_BytecodeVerifier
*verifier
)
926 bool changed
= false;
928 // Special handling for `this'. If one or the other is
929 // uninitialized, then the merge is uninitialized.
930 if (this_type
.isinitialized ())
931 this_type
= state_old
->this_type
;
933 // Merge subroutine states. Here we just keep track of what
934 // subroutine we think we're in. We only check for a merge
935 // (which is invalid) when we see a `ret'.
936 if (subroutine
== state_old
->subroutine
)
940 else if (subroutine
== 0)
942 subroutine
= state_old
->subroutine
;
947 // If the subroutines differ, indicate that the state
948 // changed. This is needed to detect when subroutines have
953 // Merge stacks. Special handling for NO_STACK case.
954 if (state_old
->stacktop
== NO_STACK
)
956 // Nothing to do in this case; we don't care about modifying
959 else if (stacktop
== NO_STACK
)
961 stacktop
= state_old
->stacktop
;
962 stackdepth
= state_old
->stackdepth
;
963 for (int i
= 0; i
< stacktop
; ++i
)
964 stack
[i
] = state_old
->stack
[i
];
967 else if (state_old
->stacktop
!= stacktop
)
968 verifier
->verify_fail ("stack sizes differ");
971 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
973 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
978 // Merge local variables.
979 for (int i
= 0; i
< max_locals
; ++i
)
981 // If we're not processing a `ret', then we merge every
982 // local variable. If we are processing a `ret', then we
983 // only merge locals which changed in the subroutine. When
984 // processing a `ret', STATE_OLD is the state at the point
985 // of the `ret', and THIS is the state just after the `jsr'.
986 if (! ret_semantics
|| state_old
->local_changed
[i
])
988 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
990 // Note that we don't call `note_variable' here.
991 // This change doesn't represent a real change to a
992 // local, but rather a merge artifact. If we're in
993 // a subroutine which is called with two
994 // incompatible types in a slot that is unused by
995 // the subroutine, then we don't want to mark that
996 // variable as having been modified.
1001 // If we're in a subroutine, we must compute the union of
1002 // all the changed local variables.
1003 if (state_old
->local_changed
[i
])
1010 // Throw an exception if there is an uninitialized object on the
1011 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1012 // whether we're using backwards-branch or exception-handing
1014 void check_no_uninitialized_objects (int max_locals
,
1015 _Jv_BytecodeVerifier
*verifier
,
1016 bool exception_semantics
= false)
1018 if (! exception_semantics
)
1020 for (int i
= 0; i
< stacktop
; ++i
)
1021 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1022 verifier
->verify_fail ("uninitialized object on stack");
1025 for (int i
= 0; i
< max_locals
; ++i
)
1026 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1027 verifier
->verify_fail ("uninitialized object in local variable");
1029 check_this_initialized (verifier
);
1032 // Ensure that `this' has been initialized.
1033 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1035 if (this_type
.isreference () && ! this_type
.isinitialized ())
1036 verifier
->verify_fail ("`this' is uninitialized");
1039 // Set type of `this'.
1040 void set_this_type (const type
&k
)
1045 // Note that a local variable was modified.
1046 void note_variable (int index
)
1049 local_changed
[index
] = true;
1052 // Mark each `new'd object we know of that was allocated at PC as
1054 void set_initialized (int pc
, int max_locals
)
1056 for (int i
= 0; i
< stacktop
; ++i
)
1057 stack
[i
].set_initialized (pc
);
1058 for (int i
= 0; i
< max_locals
; ++i
)
1059 locals
[i
].set_initialized (pc
);
1060 this_type
.set_initialized (pc
);
1063 // Return true if this state is the unmerged result of a `ret'.
1064 bool is_unmerged_ret_state (int max_locals
) const
1066 if (stacktop
== NO_STACK
)
1068 for (int i
= 0; i
< max_locals
; ++i
)
1070 if (locals
[i
].key
== unused_by_subroutine_type
)
1077 void print (const char *leader
, int pc
,
1078 int max_stack
, int max_locals
) const
1080 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1082 for (i
= 0; i
< stacktop
; ++i
)
1084 for (; i
< max_stack
; ++i
)
1086 debug_print (" [local] ");
1087 for (i
= 0; i
< max_locals
; ++i
)
1090 debug_print (local_changed
[i
] ? "+" : " ");
1092 if (subroutine
== 0)
1093 debug_print (" | None");
1095 debug_print (" | %4d", subroutine
);
1096 debug_print (" | %p\n", this);
1099 inline void print (const char *, int, int, int) const
1102 #endif /* VERIFY_DEBUG */
1107 if (current_state
->stacktop
<= 0)
1108 verify_fail ("stack empty");
1109 type r
= current_state
->stack
[--current_state
->stacktop
];
1110 current_state
->stackdepth
-= r
.depth ();
1111 if (current_state
->stackdepth
< 0)
1112 verify_fail ("stack empty", start_PC
);
1118 type r
= pop_raw ();
1120 verify_fail ("narrow pop of wide type");
1126 type r
= pop_raw ();
1128 verify_fail ("wide pop of narrow type");
1132 type
pop_type (type match
)
1135 type t
= pop_raw ();
1136 if (! match
.compatible (t
, this))
1137 verify_fail ("incompatible type on stack");
1141 // Pop a reference type or a return address.
1142 type
pop_ref_or_return ()
1144 type t
= pop_raw ();
1145 if (! t
.isreference () && t
.key
!= return_address_type
)
1146 verify_fail ("expected reference or return address on stack");
1150 void push_type (type t
)
1152 // If T is a numeric type like short, promote it to int.
1155 int depth
= t
.depth ();
1156 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1157 verify_fail ("stack overflow");
1158 current_state
->stack
[current_state
->stacktop
++] = t
;
1159 current_state
->stackdepth
+= depth
;
1162 void set_variable (int index
, type t
)
1164 // If T is a numeric type like short, promote it to int.
1167 int depth
= t
.depth ();
1168 if (index
> current_method
->max_locals
- depth
)
1169 verify_fail ("invalid local variable");
1170 current_state
->locals
[index
] = t
;
1171 current_state
->note_variable (index
);
1175 current_state
->locals
[index
+ 1] = continuation_type
;
1176 current_state
->note_variable (index
+ 1);
1178 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1180 current_state
->locals
[index
- 1] = unsuitable_type
;
1181 // There's no need to call note_variable here.
1185 type
get_variable (int index
, type t
)
1187 int depth
= t
.depth ();
1188 if (index
> current_method
->max_locals
- depth
)
1189 verify_fail ("invalid local variable");
1190 if (! t
.compatible (current_state
->locals
[index
], this))
1191 verify_fail ("incompatible type in local variable");
1194 type
t (continuation_type
);
1195 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1196 verify_fail ("invalid local variable");
1198 return current_state
->locals
[index
];
1201 // Make sure ARRAY is an array type and that its elements are
1202 // compatible with type ELEMENT. Returns the actual element type.
1203 type
require_array_type (type array
, type element
)
1205 // An odd case. Here we just pretend that everything went ok. If
1206 // the requested element type is some kind of reference, return
1207 // the null type instead.
1208 if (array
.isnull ())
1209 return element
.isreference () ? type (null_type
) : element
;
1211 if (! array
.isarray ())
1212 verify_fail ("array required");
1214 type t
= array
.element_type (this);
1215 if (! element
.compatible (t
, this))
1217 // Special case for byte arrays, which must also be boolean
1220 if (element
.key
== byte_type
)
1222 type
e2 (boolean_type
);
1223 ok
= e2
.compatible (t
, this);
1226 verify_fail ("incompatible array element type");
1229 // Return T and not ELEMENT, because T might be specialized.
1235 if (PC
>= current_method
->code_length
)
1236 verify_fail ("premature end of bytecode");
1237 return (jint
) bytecode
[PC
++] & 0xff;
1242 jint b1
= get_byte ();
1243 jint b2
= get_byte ();
1244 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1249 jint b1
= get_byte ();
1250 jint b2
= get_byte ();
1251 jshort s
= (b1
<< 8) | b2
;
1257 jint b1
= get_byte ();
1258 jint b2
= get_byte ();
1259 jint b3
= get_byte ();
1260 jint b4
= get_byte ();
1261 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1264 int compute_jump (int offset
)
1266 int npc
= start_PC
+ offset
;
1267 if (npc
< 0 || npc
>= current_method
->code_length
)
1268 verify_fail ("branch out of range", start_PC
);
1272 // Merge the indicated state into the state at the branch target and
1273 // schedule a new PC if there is a change. If RET_SEMANTICS is
1274 // true, then we are merging from a `ret' instruction into the
1275 // instruction after a `jsr'. This is a special case with its own
1276 // modified semantics.
1277 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1279 bool changed
= true;
1280 if (states
[npc
] == NULL
)
1282 // There's a weird situation here. If are examining the
1283 // branch that results from a `ret', and there is not yet a
1284 // state available at the branch target (the instruction just
1285 // after the `jsr'), then we have to construct a special kind
1286 // of state at that point for future merging. This special
1287 // state has the type `unused_by_subroutine_type' in each slot
1288 // which was not modified by the subroutine.
1289 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1290 current_method
->max_locals
, ret_semantics
);
1291 debug_print ("== New state in push_jump_merge\n");
1292 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1293 current_method
->max_locals
);
1297 debug_print ("== Merge states in push_jump_merge\n");
1298 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1299 current_method
->max_locals
);
1300 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1301 current_method
->max_locals
);
1302 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1303 current_method
->max_locals
, this);
1304 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1305 current_method
->max_locals
);
1308 if (changed
&& states
[npc
]->next
== state::INVALID
)
1310 // The merge changed the state, and the new PC isn't yet on our
1311 // list of PCs to re-verify.
1312 states
[npc
]->next
= next_verify_pc
;
1313 next_verify_pc
= npc
;
1317 void push_jump (int offset
)
1319 int npc
= compute_jump (offset
);
1321 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1322 push_jump_merge (npc
, current_state
);
1325 void push_exception_jump (type t
, int pc
)
1327 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1329 state
s (current_state
, current_method
->max_stack
,
1330 current_method
->max_locals
);
1331 if (current_method
->max_stack
< 1)
1332 verify_fail ("stack overflow at exception handler");
1333 s
.set_exception (t
, current_method
->max_stack
);
1334 push_jump_merge (pc
, &s
);
1339 int *prev_loc
= &next_verify_pc
;
1340 int npc
= next_verify_pc
;
1341 bool skipped
= false;
1343 while (npc
!= state::NO_NEXT
)
1345 // If the next available PC is an unmerged `ret' state, then
1346 // we aren't yet ready to handle it. That's because we would
1347 // need all kind of special cases to do so. So instead we
1348 // defer this jump until after we've processed it via a
1349 // fall-through. This has to happen because the instruction
1350 // before this one must be a `jsr'.
1351 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1353 *prev_loc
= states
[npc
]->next
;
1354 states
[npc
]->next
= state::INVALID
;
1359 prev_loc
= &states
[npc
]->next
;
1360 npc
= states
[npc
]->next
;
1363 // Note that we might have gotten here even when there are
1364 // remaining states to process. That can happen if we find a
1365 // `jsr' without a `ret'.
1366 return state::NO_NEXT
;
1369 void invalidate_pc ()
1371 PC
= state::NO_NEXT
;
1374 void note_branch_target (int pc
, bool is_jsr_target
= false)
1376 // Don't check `pc <= PC', because we've advanced PC after
1377 // fetching the target and we haven't yet checked the next
1379 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1380 verify_fail ("branch not to instruction start", start_PC
);
1381 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1384 // Record the jsr which called this instruction.
1385 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1387 info
->next
= jsr_ptrs
[pc
];
1388 jsr_ptrs
[pc
] = info
;
1392 void skip_padding ()
1394 while ((PC
% 4) > 0)
1395 if (get_byte () != 0)
1396 verify_fail ("found nonzero padding byte");
1399 // Return the subroutine to which the instruction at PC belongs.
1400 int get_subroutine (int pc
)
1402 if (states
[pc
] == NULL
)
1404 return states
[pc
]->subroutine
;
1407 // Do the work for a `ret' instruction. INDEX is the index into the
1409 void handle_ret_insn (int index
)
1411 get_variable (index
, return_address_type
);
1413 int csub
= current_state
->subroutine
;
1415 verify_fail ("no subroutine");
1417 // Check to see if we've merged subroutines.
1418 subr_entry_info
*entry
;
1419 for (entry
= entry_points
; entry
!= NULL
; entry
= entry
->next
)
1421 if (entry
->ret_pc
== start_PC
)
1426 entry
= (subr_entry_info
*) _Jv_Malloc (sizeof (subr_entry_info
));
1428 entry
->ret_pc
= start_PC
;
1429 entry
->next
= entry_points
;
1430 entry_points
= entry
;
1432 else if (entry
->pc
!= csub
)
1433 verify_fail ("subroutines merged");
1435 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1437 // Temporarily modify the current state so it looks like we're
1438 // in the enclosing context.
1439 current_state
->subroutine
= get_subroutine (subr
->pc
);
1441 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1442 push_jump_merge (subr
->pc
, current_state
, true);
1445 current_state
->subroutine
= csub
;
1449 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1450 // sure this subroutine isn't already on the stack.
1451 void check_nonrecursive_call (int sub
, int dest
)
1456 verify_fail ("recursive subroutine call");
1457 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1458 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1461 void handle_jsr_insn (int offset
)
1463 int npc
= compute_jump (offset
);
1466 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1467 check_nonrecursive_call (current_state
->subroutine
, npc
);
1469 // Modify our state as appropriate for entry into a subroutine.
1470 push_type (return_address_type
);
1471 push_jump_merge (npc
, current_state
);
1473 pop_type (return_address_type
);
1475 // On entry to the subroutine, the subroutine number must be set
1476 // and the locals must be marked as cleared. We do this after
1477 // merging state so that we don't erroneously "notice" a variable
1478 // change merely on entry.
1479 states
[npc
]->enter_subroutine (npc
, current_method
->max_locals
);
1481 // Indicate that we don't know the stack depth of the instruction
1482 // following the `jsr'. The idea here is that we need to merge
1483 // the local variable state across the jsr, but the subroutine
1484 // might change the stack depth, so we can't make any assumptions
1485 // about it. So we have yet another special case. We know that
1486 // at this point PC points to the instruction after the jsr.
1488 // FIXME: what if we have a jsr at the end of the code, but that
1489 // jsr has no corresponding ret? Is this verifiable, or is it
1490 // not? If it is then we need a special case here.
1491 if (PC
>= current_method
->code_length
)
1492 verify_fail ("fell off end");
1494 current_state
->stacktop
= state::NO_STACK
;
1495 push_jump_merge (PC
, current_state
);
1499 jclass
construct_primitive_array_type (type_val prim
)
1505 k
= JvPrimClass (boolean
);
1508 k
= JvPrimClass (char);
1511 k
= JvPrimClass (float);
1514 k
= JvPrimClass (double);
1517 k
= JvPrimClass (byte
);
1520 k
= JvPrimClass (short);
1523 k
= JvPrimClass (int);
1526 k
= JvPrimClass (long);
1529 verify_fail ("unknown type in construct_primitive_array_type");
1531 k
= _Jv_GetArrayClass (k
, NULL
);
1535 // This pass computes the location of branch targets and also
1536 // instruction starts.
1537 void branch_prepass ()
1539 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1540 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1541 * current_method
->code_length
);
1543 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1549 bool last_was_jsr
= false;
1552 while (PC
< current_method
->code_length
)
1554 // Set `start_PC' early so that error checking can have the
1557 flags
[PC
] |= FLAG_INSN_START
;
1559 // If the previous instruction was a jsr, then the next
1560 // instruction is a branch target -- the branch being the
1561 // corresponding `ret'.
1563 note_branch_target (PC
);
1564 last_was_jsr
= false;
1566 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1570 case op_aconst_null
:
1706 case op_monitorenter
:
1707 case op_monitorexit
:
1715 case op_arraylength
:
1747 case op_invokespecial
:
1748 case op_invokestatic
:
1749 case op_invokevirtual
:
1753 case op_multianewarray
:
1759 last_was_jsr
= true;
1778 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1781 case op_tableswitch
:
1784 note_branch_target (compute_jump (get_int ()));
1785 jint low
= get_int ();
1786 jint hi
= get_int ();
1788 verify_fail ("invalid tableswitch", start_PC
);
1789 for (int i
= low
; i
<= hi
; ++i
)
1790 note_branch_target (compute_jump (get_int ()));
1794 case op_lookupswitch
:
1797 note_branch_target (compute_jump (get_int ()));
1798 int npairs
= get_int ();
1800 verify_fail ("too few pairs in lookupswitch", start_PC
);
1801 while (npairs
-- > 0)
1804 note_branch_target (compute_jump (get_int ()));
1809 case op_invokeinterface
:
1817 opcode
= (java_opcode
) get_byte ();
1819 if (opcode
== op_iinc
)
1825 last_was_jsr
= true;
1828 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1832 verify_fail ("unrecognized instruction in branch_prepass",
1836 // See if any previous branch tried to branch to the middle of
1837 // this instruction.
1838 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1840 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1841 verify_fail ("branch to middle of instruction", pc
);
1845 // Verify exception handlers.
1846 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1848 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1849 verify_fail ("exception handler not at instruction start",
1850 exception
[i
].handler_pc
);
1851 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1852 verify_fail ("exception start not at instruction start",
1853 exception
[i
].start_pc
);
1854 if (exception
[i
].end_pc
!= current_method
->code_length
1855 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1856 verify_fail ("exception end not at instruction start",
1857 exception
[i
].end_pc
);
1859 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1863 void check_pool_index (int index
)
1865 if (index
< 0 || index
>= current_class
->constants
.size
)
1866 verify_fail ("constant pool index out of range", start_PC
);
1869 type
check_class_constant (int index
)
1871 check_pool_index (index
);
1872 _Jv_Constants
*pool
= ¤t_class
->constants
;
1873 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1874 return type (pool
->data
[index
].clazz
);
1875 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1876 return type (pool
->data
[index
].utf8
);
1877 verify_fail ("expected class constant", start_PC
);
1880 type
check_constant (int index
)
1882 check_pool_index (index
);
1883 _Jv_Constants
*pool
= ¤t_class
->constants
;
1884 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1885 || pool
->tags
[index
] == JV_CONSTANT_String
)
1886 return type (&java::lang::String::class$
);
1887 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1888 return type (int_type
);
1889 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1890 return type (float_type
);
1891 verify_fail ("String, int, or float constant expected", start_PC
);
1894 type
check_wide_constant (int index
)
1896 check_pool_index (index
);
1897 _Jv_Constants
*pool
= ¤t_class
->constants
;
1898 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1899 return type (long_type
);
1900 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1901 return type (double_type
);
1902 verify_fail ("long or double constant expected", start_PC
);
1905 // Helper for both field and method. These are laid out the same in
1906 // the constant pool.
1907 type
handle_field_or_method (int index
, int expected
,
1908 _Jv_Utf8Const
**name
,
1909 _Jv_Utf8Const
**fmtype
)
1911 check_pool_index (index
);
1912 _Jv_Constants
*pool
= ¤t_class
->constants
;
1913 if (pool
->tags
[index
] != expected
)
1914 verify_fail ("didn't see expected constant", start_PC
);
1915 // Once we know we have a Fieldref or Methodref we assume that it
1916 // is correctly laid out in the constant pool. I think the code
1917 // in defineclass.cc guarantees this.
1918 _Jv_ushort class_index
, name_and_type_index
;
1919 _Jv_loadIndexes (&pool
->data
[index
],
1921 name_and_type_index
);
1922 _Jv_ushort name_index
, desc_index
;
1923 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1924 name_index
, desc_index
);
1926 *name
= pool
->data
[name_index
].utf8
;
1927 *fmtype
= pool
->data
[desc_index
].utf8
;
1929 return check_class_constant (class_index
);
1932 // Return field's type, compute class' type if requested.
1933 type
check_field_constant (int index
, type
*class_type
= NULL
)
1935 _Jv_Utf8Const
*name
, *field_type
;
1936 type ct
= handle_field_or_method (index
,
1937 JV_CONSTANT_Fieldref
,
1938 &name
, &field_type
);
1941 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1942 return type (field_type
);
1943 return get_type_val_for_signature (field_type
->data
[0]);
1946 type
check_method_constant (int index
, bool is_interface
,
1947 _Jv_Utf8Const
**method_name
,
1948 _Jv_Utf8Const
**method_signature
)
1950 return handle_field_or_method (index
,
1952 ? JV_CONSTANT_InterfaceMethodref
1953 : JV_CONSTANT_Methodref
),
1954 method_name
, method_signature
);
1957 type
get_one_type (char *&p
)
1975 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1979 // Casting to jchar here is ok since we are looking at an ASCII
1981 type_val rt
= get_type_val_for_signature (jchar (v
));
1983 if (arraycount
== 0)
1985 // Callers of this function eventually push their arguments on
1986 // the stack. So, promote them here.
1987 return type (rt
).promote ();
1990 jclass k
= construct_primitive_array_type (rt
);
1991 while (--arraycount
> 0)
1992 k
= _Jv_GetArrayClass (k
, NULL
);
1996 void compute_argument_types (_Jv_Utf8Const
*signature
,
1999 char *p
= signature
->data
;
2005 types
[i
++] = get_one_type (p
);
2008 type
compute_return_type (_Jv_Utf8Const
*signature
)
2010 char *p
= signature
->data
;
2014 return get_one_type (p
);
2017 void check_return_type (type onstack
)
2019 type rt
= compute_return_type (current_method
->self
->signature
);
2020 if (! rt
.compatible (onstack
, this))
2021 verify_fail ("incompatible return type");
2024 // Initialize the stack for the new method. Returns true if this
2025 // method is an instance initializer.
2026 bool initialize_stack ()
2029 bool is_init
= false;
2031 using namespace java::lang::reflect
;
2032 if (! Modifier::isStatic (current_method
->self
->accflags
))
2034 type
kurr (current_class
);
2035 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
2037 kurr
.set_uninitialized (type::SELF
, this);
2040 set_variable (0, kurr
);
2041 current_state
->set_this_type (kurr
);
2045 // We have to handle wide arguments specially here.
2046 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2047 type arg_types
[arg_count
];
2048 compute_argument_types (current_method
->self
->signature
, arg_types
);
2049 for (int i
= 0; i
< arg_count
; ++i
)
2051 set_variable (var
, arg_types
[i
]);
2053 if (arg_types
[i
].iswide ())
2060 void verify_instructions_0 ()
2062 current_state
= new state (current_method
->max_stack
,
2063 current_method
->max_locals
);
2068 // True if we are verifying an instance initializer.
2069 bool this_is_init
= initialize_stack ();
2071 states
= (state
**) _Jv_Malloc (sizeof (state
*)
2072 * current_method
->code_length
);
2073 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2076 next_verify_pc
= state::NO_NEXT
;
2080 // If the PC was invalidated, get a new one from the work list.
2081 if (PC
== state::NO_NEXT
)
2084 if (PC
== state::INVALID
)
2085 verify_fail ("can't happen: saw state::INVALID");
2086 if (PC
== state::NO_NEXT
)
2088 debug_print ("== State pop from pending list\n");
2089 // Set up the current state.
2090 current_state
->copy (states
[PC
], current_method
->max_stack
,
2091 current_method
->max_locals
);
2095 // Control can't fall off the end of the bytecode. We
2096 // only need to check this in the fall-through case,
2097 // because branch bounds are checked when they are
2099 if (PC
>= current_method
->code_length
)
2100 verify_fail ("fell off end");
2102 // We only have to do this checking in the situation where
2103 // control flow falls through from the previous
2104 // instruction. Otherwise merging is done at the time we
2106 if (states
[PC
] != NULL
)
2108 // We've already visited this instruction. So merge
2109 // the states together. If this yields no change then
2110 // we don't have to re-verify. However, if the new
2111 // state is an the result of an unmerged `ret', we
2112 // must continue through it.
2113 debug_print ("== Fall through merge\n");
2114 states
[PC
]->print ("Old", PC
, current_method
->max_stack
,
2115 current_method
->max_locals
);
2116 current_state
->print ("Cur", PC
, current_method
->max_stack
,
2117 current_method
->max_locals
);
2118 if (! current_state
->merge (states
[PC
], false,
2119 current_method
->max_locals
, this)
2120 && ! states
[PC
]->is_unmerged_ret_state (current_method
->max_locals
))
2122 debug_print ("== Fall through optimization\n");
2126 // Save a copy of it for later.
2127 states
[PC
]->copy (current_state
, current_method
->max_stack
,
2128 current_method
->max_locals
);
2129 current_state
->print ("New", PC
, current_method
->max_stack
,
2130 current_method
->max_locals
);
2134 // We only have to keep saved state at branch targets. If
2135 // we're at a branch target and the state here hasn't been set
2136 // yet, we set it now.
2137 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2139 states
[PC
] = new state (current_state
, current_method
->max_stack
,
2140 current_method
->max_locals
);
2143 // Set this before handling exceptions so that debug output is
2147 // Update states for all active exception handlers. Ordinarily
2148 // there are not many exception handlers. So we simply run
2149 // through them all.
2150 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2152 if (PC
>= exception
[i
].start_pc
&& PC
< exception
[i
].end_pc
)
2154 type
handler (&java::lang::Throwable::class$
);
2155 if (exception
[i
].handler_type
!= 0)
2156 handler
= check_class_constant (exception
[i
].handler_type
);
2157 push_exception_jump (handler
, exception
[i
].handler_pc
);
2161 current_state
->print (" ", PC
, current_method
->max_stack
,
2162 current_method
->max_locals
);
2163 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2169 case op_aconst_null
:
2170 push_type (null_type
);
2180 push_type (int_type
);
2185 push_type (long_type
);
2191 push_type (float_type
);
2196 push_type (double_type
);
2201 push_type (int_type
);
2206 push_type (int_type
);
2210 push_type (check_constant (get_byte ()));
2213 push_type (check_constant (get_ushort ()));
2216 push_type (check_wide_constant (get_ushort ()));
2220 push_type (get_variable (get_byte (), int_type
));
2223 push_type (get_variable (get_byte (), long_type
));
2226 push_type (get_variable (get_byte (), float_type
));
2229 push_type (get_variable (get_byte (), double_type
));
2232 push_type (get_variable (get_byte (), reference_type
));
2239 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2245 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2251 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2257 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2263 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2266 pop_type (int_type
);
2267 push_type (require_array_type (pop_type (reference_type
),
2271 pop_type (int_type
);
2272 push_type (require_array_type (pop_type (reference_type
),
2276 pop_type (int_type
);
2277 push_type (require_array_type (pop_type (reference_type
),
2281 pop_type (int_type
);
2282 push_type (require_array_type (pop_type (reference_type
),
2286 pop_type (int_type
);
2287 push_type (require_array_type (pop_type (reference_type
),
2291 pop_type (int_type
);
2292 require_array_type (pop_type (reference_type
), byte_type
);
2293 push_type (int_type
);
2296 pop_type (int_type
);
2297 require_array_type (pop_type (reference_type
), char_type
);
2298 push_type (int_type
);
2301 pop_type (int_type
);
2302 require_array_type (pop_type (reference_type
), short_type
);
2303 push_type (int_type
);
2306 set_variable (get_byte (), pop_type (int_type
));
2309 set_variable (get_byte (), pop_type (long_type
));
2312 set_variable (get_byte (), pop_type (float_type
));
2315 set_variable (get_byte (), pop_type (double_type
));
2318 set_variable (get_byte (), pop_ref_or_return ());
2324 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2330 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2336 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2342 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2348 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2351 pop_type (int_type
);
2352 pop_type (int_type
);
2353 require_array_type (pop_type (reference_type
), int_type
);
2356 pop_type (long_type
);
2357 pop_type (int_type
);
2358 require_array_type (pop_type (reference_type
), long_type
);
2361 pop_type (float_type
);
2362 pop_type (int_type
);
2363 require_array_type (pop_type (reference_type
), float_type
);
2366 pop_type (double_type
);
2367 pop_type (int_type
);
2368 require_array_type (pop_type (reference_type
), double_type
);
2371 pop_type (reference_type
);
2372 pop_type (int_type
);
2373 require_array_type (pop_type (reference_type
), reference_type
);
2376 pop_type (int_type
);
2377 pop_type (int_type
);
2378 require_array_type (pop_type (reference_type
), byte_type
);
2381 pop_type (int_type
);
2382 pop_type (int_type
);
2383 require_array_type (pop_type (reference_type
), char_type
);
2386 pop_type (int_type
);
2387 pop_type (int_type
);
2388 require_array_type (pop_type (reference_type
), short_type
);
2415 type t2
= pop_raw ();
2430 type t
= pop_raw ();
2445 type t1
= pop_raw ();
2462 type t1
= pop_raw ();
2465 type t2
= pop_raw ();
2483 type t3
= pop_raw ();
2521 pop_type (int_type
);
2522 push_type (pop_type (int_type
));
2532 pop_type (long_type
);
2533 push_type (pop_type (long_type
));
2538 pop_type (int_type
);
2539 push_type (pop_type (long_type
));
2546 pop_type (float_type
);
2547 push_type (pop_type (float_type
));
2554 pop_type (double_type
);
2555 push_type (pop_type (double_type
));
2561 push_type (pop_type (int_type
));
2564 push_type (pop_type (long_type
));
2567 push_type (pop_type (float_type
));
2570 push_type (pop_type (double_type
));
2573 get_variable (get_byte (), int_type
);
2577 pop_type (int_type
);
2578 push_type (long_type
);
2581 pop_type (int_type
);
2582 push_type (float_type
);
2585 pop_type (int_type
);
2586 push_type (double_type
);
2589 pop_type (long_type
);
2590 push_type (int_type
);
2593 pop_type (long_type
);
2594 push_type (float_type
);
2597 pop_type (long_type
);
2598 push_type (double_type
);
2601 pop_type (float_type
);
2602 push_type (int_type
);
2605 pop_type (float_type
);
2606 push_type (long_type
);
2609 pop_type (float_type
);
2610 push_type (double_type
);
2613 pop_type (double_type
);
2614 push_type (int_type
);
2617 pop_type (double_type
);
2618 push_type (long_type
);
2621 pop_type (double_type
);
2622 push_type (float_type
);
2625 pop_type (long_type
);
2626 pop_type (long_type
);
2627 push_type (int_type
);
2631 pop_type (float_type
);
2632 pop_type (float_type
);
2633 push_type (int_type
);
2637 pop_type (double_type
);
2638 pop_type (double_type
);
2639 push_type (int_type
);
2647 pop_type (int_type
);
2648 push_jump (get_short ());
2656 pop_type (int_type
);
2657 pop_type (int_type
);
2658 push_jump (get_short ());
2662 pop_type (reference_type
);
2663 pop_type (reference_type
);
2664 push_jump (get_short ());
2667 push_jump (get_short ());
2671 handle_jsr_insn (get_short ());
2674 handle_ret_insn (get_byte ());
2676 case op_tableswitch
:
2678 pop_type (int_type
);
2680 push_jump (get_int ());
2681 jint low
= get_int ();
2682 jint high
= get_int ();
2683 // Already checked LOW -vs- HIGH.
2684 for (int i
= low
; i
<= high
; ++i
)
2685 push_jump (get_int ());
2690 case op_lookupswitch
:
2692 pop_type (int_type
);
2694 push_jump (get_int ());
2695 jint npairs
= get_int ();
2696 // Already checked NPAIRS >= 0.
2698 for (int i
= 0; i
< npairs
; ++i
)
2700 jint key
= get_int ();
2701 if (i
> 0 && key
<= lastkey
)
2702 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2704 push_jump (get_int ());
2710 check_return_type (pop_type (int_type
));
2714 check_return_type (pop_type (long_type
));
2718 check_return_type (pop_type (float_type
));
2722 check_return_type (pop_type (double_type
));
2726 check_return_type (pop_type (reference_type
));
2730 // We only need to check this when the return type is
2731 // void, because all instance initializers return void.
2733 current_state
->check_this_initialized (this);
2734 check_return_type (void_type
);
2738 push_type (check_field_constant (get_ushort ()));
2741 pop_type (check_field_constant (get_ushort ()));
2746 type field
= check_field_constant (get_ushort (), &klass
);
2754 type field
= check_field_constant (get_ushort (), &klass
);
2757 // We have an obscure special case here: we can use
2758 // `putfield' on a field declared in this class, even if
2759 // `this' has not yet been initialized.
2760 if (! current_state
->this_type
.isinitialized ()
2761 && current_state
->this_type
.pc
== type::SELF
)
2762 klass
.set_uninitialized (type::SELF
, this);
2767 case op_invokevirtual
:
2768 case op_invokespecial
:
2769 case op_invokestatic
:
2770 case op_invokeinterface
:
2772 _Jv_Utf8Const
*method_name
, *method_signature
;
2774 = check_method_constant (get_ushort (),
2775 opcode
== op_invokeinterface
,
2778 // NARGS is only used when we're processing
2779 // invokeinterface. It is simplest for us to compute it
2780 // here and then verify it later.
2782 if (opcode
== op_invokeinterface
)
2784 nargs
= get_byte ();
2785 if (get_byte () != 0)
2786 verify_fail ("invokeinterface dummy byte is wrong");
2789 bool is_init
= false;
2790 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2793 if (opcode
!= op_invokespecial
)
2794 verify_fail ("can't invoke <init>");
2796 else if (method_name
->data
[0] == '<')
2797 verify_fail ("can't invoke method starting with `<'");
2799 // Pop arguments and check types.
2800 int arg_count
= _Jv_count_arguments (method_signature
);
2801 type arg_types
[arg_count
];
2802 compute_argument_types (method_signature
, arg_types
);
2803 for (int i
= arg_count
- 1; i
>= 0; --i
)
2805 // This is only used for verifying the byte for
2807 nargs
-= arg_types
[i
].depth ();
2808 pop_type (arg_types
[i
]);
2811 if (opcode
== op_invokeinterface
2813 verify_fail ("wrong argument count for invokeinterface");
2815 if (opcode
!= op_invokestatic
)
2817 type t
= class_type
;
2820 // In this case the PC doesn't matter.
2821 t
.set_uninitialized (type::UNINIT
, this);
2823 type raw
= pop_raw ();
2825 if (t
.compatible (raw
, this))
2829 else if (opcode
== op_invokeinterface
)
2831 // This is a hack. We might have merged two
2832 // items and gotten `Object'. This can happen
2833 // because we don't keep track of where merges
2834 // come from. This is safe as long as the
2835 // interpreter checks interfaces at runtime.
2836 type
obj (&java::lang::Object::class$
);
2837 ok
= raw
.compatible (obj
, this);
2841 verify_fail ("incompatible type on stack");
2844 current_state
->set_initialized (raw
.get_pc (),
2845 current_method
->max_locals
);
2848 type rt
= compute_return_type (method_signature
);
2856 type t
= check_class_constant (get_ushort ());
2857 if (t
.isarray () || t
.isinterface (this) || t
.isabstract (this))
2858 verify_fail ("type is array, interface, or abstract");
2859 t
.set_uninitialized (start_PC
, this);
2866 int atype
= get_byte ();
2867 // We intentionally have chosen constants to make this
2869 if (atype
< boolean_type
|| atype
> long_type
)
2870 verify_fail ("type not primitive", start_PC
);
2871 pop_type (int_type
);
2872 push_type (construct_primitive_array_type (type_val (atype
)));
2876 pop_type (int_type
);
2877 push_type (check_class_constant (get_ushort ()).to_array (this));
2879 case op_arraylength
:
2881 type t
= pop_type (reference_type
);
2882 if (! t
.isarray () && ! t
.isnull ())
2883 verify_fail ("array type expected");
2884 push_type (int_type
);
2888 pop_type (type (&java::lang::Throwable::class$
));
2892 pop_type (reference_type
);
2893 push_type (check_class_constant (get_ushort ()));
2896 pop_type (reference_type
);
2897 check_class_constant (get_ushort ());
2898 push_type (int_type
);
2900 case op_monitorenter
:
2901 pop_type (reference_type
);
2903 case op_monitorexit
:
2904 pop_type (reference_type
);
2908 switch (get_byte ())
2911 push_type (get_variable (get_ushort (), int_type
));
2914 push_type (get_variable (get_ushort (), long_type
));
2917 push_type (get_variable (get_ushort (), float_type
));
2920 push_type (get_variable (get_ushort (), double_type
));
2923 push_type (get_variable (get_ushort (), reference_type
));
2926 set_variable (get_ushort (), pop_type (int_type
));
2929 set_variable (get_ushort (), pop_type (long_type
));
2932 set_variable (get_ushort (), pop_type (float_type
));
2935 set_variable (get_ushort (), pop_type (double_type
));
2938 set_variable (get_ushort (), pop_type (reference_type
));
2941 handle_ret_insn (get_short ());
2944 get_variable (get_ushort (), int_type
);
2948 verify_fail ("unrecognized wide instruction", start_PC
);
2952 case op_multianewarray
:
2954 type atype
= check_class_constant (get_ushort ());
2955 int dim
= get_byte ();
2957 verify_fail ("too few dimensions to multianewarray", start_PC
);
2958 atype
.verify_dimensions (dim
, this);
2959 for (int i
= 0; i
< dim
; ++i
)
2960 pop_type (int_type
);
2966 pop_type (reference_type
);
2967 push_jump (get_short ());
2970 push_jump (get_int ());
2974 handle_jsr_insn (get_int ());
2978 // Unrecognized opcode.
2979 verify_fail ("unrecognized instruction in verify_instructions_0",
2985 __attribute__ ((__noreturn__
)) void verify_fail (char *s
, jint pc
= -1)
2987 using namespace java::lang
;
2988 StringBuffer
*buf
= new StringBuffer ();
2990 buf
->append (JvNewStringLatin1 ("verification failed"));
2995 buf
->append (JvNewStringLatin1 (" at PC "));
2999 _Jv_InterpMethod
*method
= current_method
;
3000 buf
->append (JvNewStringLatin1 (" in "));
3001 buf
->append (current_class
->getName());
3002 buf
->append ((jchar
) ':');
3003 buf
->append (JvNewStringUTF (method
->get_method()->name
->data
));
3004 buf
->append ((jchar
) '(');
3005 buf
->append (JvNewStringUTF (method
->get_method()->signature
->data
));
3006 buf
->append ((jchar
) ')');
3008 buf
->append (JvNewStringLatin1 (": "));
3009 buf
->append (JvNewStringLatin1 (s
));
3010 throw new java::lang::VerifyError (buf
->toString ());
3015 void verify_instructions ()
3018 verify_instructions_0 ();
3021 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
3023 // We just print the text as utf-8. This is just for debugging
3025 debug_print ("--------------------------------\n");
3026 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
3029 bytecode
= m
->bytecode ();
3030 exception
= m
->exceptions ();
3031 current_class
= m
->defining_class
;
3037 entry_points
= NULL
;
3040 ~_Jv_BytecodeVerifier ()
3049 for (int i
= 0; i
< current_method
->code_length
; ++i
)
3051 if (jsr_ptrs
[i
] != NULL
)
3053 subr_info
*info
= jsr_ptrs
[i
];
3054 while (info
!= NULL
)
3056 subr_info
*next
= info
->next
;
3062 _Jv_Free (jsr_ptrs
);
3065 while (utf8_list
!= NULL
)
3067 linked_utf8
*n
= utf8_list
->next
;
3068 _Jv_Free (utf8_list
->val
);
3069 _Jv_Free (utf8_list
);
3073 while (entry_points
!= NULL
)
3075 subr_entry_info
*next
= entry_points
->next
;
3076 _Jv_Free (entry_points
);
3077 entry_points
= next
;
3083 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
3085 _Jv_BytecodeVerifier
v (meth
);
3086 v
.verify_instructions ();
3088 #endif /* INTERPRETER */