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 if (target
->isArray ())
236 if (! source
->isArray ())
238 target
= target
->getComponentType ();
239 source
= source
->getComponentType ();
241 else if (target
->isInterface ())
243 for (int i
= 0; i
< source
->interface_count
; ++i
)
245 // We use a recursive call because we also need to
246 // check superinterfaces.
247 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
250 source
= source
->getSuperclass ();
254 // We must do this check before we check to see if SOURCE is
255 // an interface. This way we know that any interface is
256 // assignable to an Object.
257 else if (target
== &java::lang::Object::class$
)
259 else if (source
->isInterface ())
261 for (int i
= 0; i
< target
->interface_count
; ++i
)
263 // We use a recursive call because we also need to
264 // check superinterfaces.
265 if (is_assignable_from_slow (target
->interfaces
[i
], source
))
268 target
= target
->getSuperclass ();
272 else if (source
== &java::lang::Object::class$
)
275 source
= source
->getSuperclass ();
279 // This is used to keep track of which `jsr's correspond to a given
283 // PC of the instruction just after the jsr.
289 // This is used to keep track of which subroutine entry point
290 // corresponds to which `ret' instruction.
291 struct subr_entry_info
293 // PC of the subroutine entry point.
295 // PC of the `ret' instruction.
298 subr_entry_info
*next
;
301 // The `type' class is used to represent a single type in the
307 // Some associated data.
310 // For a resolved reference type, this is a pointer to the class.
312 // For other reference types, this it the name of the class.
315 // This is used when constructing a new object. It is the PC of the
316 // `new' instruction which created the object. We use the special
317 // value -2 to mean that this is uninitialized, and the special
318 // value -1 for the case where the current method is itself the
322 static const int UNINIT
= -2;
323 static const int SELF
= -1;
325 // Basic constructor.
328 key
= unsuitable_type
;
333 // Make a new instance given the type tag. We assume a generic
334 // `reference_type' means Object.
339 if (key
== reference_type
)
340 data
.klass
= &java::lang::Object::class$
;
344 // Make a new instance given a class.
347 key
= reference_type
;
352 // Make a new instance given the name of a class.
353 type (_Jv_Utf8Const
*n
)
355 key
= unresolved_reference_type
;
368 // These operators are required because libgcj can't link in
370 void *operator new[] (size_t bytes
)
372 return _Jv_Malloc (bytes
);
375 void operator delete[] (void *mem
)
380 type
& operator= (type_val k
)
388 type
& operator= (const type
& t
)
396 // Promote a numeric type.
399 if (key
== boolean_type
|| key
== char_type
400 || key
== byte_type
|| key
== short_type
)
405 // If *THIS is an unresolved reference type, resolve it.
406 void resolve (_Jv_BytecodeVerifier
*verifier
)
408 if (key
!= unresolved_reference_type
409 && key
!= uninitialized_unresolved_reference_type
)
412 using namespace java::lang
;
413 java::lang::ClassLoader
*loader
414 = verifier
->current_class
->getClassLoader();
415 // We might see either kind of name. Sigh.
416 if (data
.name
->data
[0] == 'L'
417 && data
.name
->data
[data
.name
->length
- 1] == ';')
418 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, loader
);
420 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
422 key
= (key
== unresolved_reference_type
424 : uninitialized_reference_type
);
427 // Mark this type as the uninitialized result of `new'.
428 void set_uninitialized (int npc
, _Jv_BytecodeVerifier
*verifier
)
430 if (key
== reference_type
)
431 key
= uninitialized_reference_type
;
432 else if (key
== unresolved_reference_type
)
433 key
= uninitialized_unresolved_reference_type
;
435 verifier
->verify_fail ("internal error in type::uninitialized");
439 // Mark this type as now initialized.
440 void set_initialized (int npc
)
442 if (npc
!= UNINIT
&& pc
== npc
443 && (key
== uninitialized_reference_type
444 || key
== uninitialized_unresolved_reference_type
))
446 key
= (key
== uninitialized_reference_type
448 : unresolved_reference_type
);
454 // Return true if an object of type K can be assigned to a variable
455 // of type *THIS. Handle various special cases too. Might modify
456 // *THIS or K. Note however that this does not perform numeric
458 bool compatible (type
&k
, _Jv_BytecodeVerifier
*verifier
)
460 // Any type is compatible with the unsuitable type.
461 if (key
== unsuitable_type
)
464 if (key
< reference_type
|| k
.key
< reference_type
)
467 // The `null' type is convertible to any reference type.
468 // FIXME: is this correct for THIS?
469 if (key
== null_type
|| k
.key
== null_type
)
472 // Any reference type is convertible to Object. This is a special
473 // case so we don't need to unnecessarily resolve a class.
474 if (key
== reference_type
475 && data
.klass
== &java::lang::Object::class$
)
478 // An initialized type and an uninitialized type are not
480 if (isinitialized () != k
.isinitialized ())
483 // Two uninitialized objects are compatible if either:
484 // * The PCs are identical, or
485 // * One PC is UNINIT.
486 if (! isinitialized ())
488 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
492 // Two unresolved types are equal if their names are the same.
495 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
498 // We must resolve both types and check assignability.
500 k
.resolve (verifier
);
501 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
506 return key
== void_type
;
511 return key
== long_type
|| key
== double_type
;
514 // Return number of stack or local variable slots taken by this
518 return iswide () ? 2 : 1;
521 bool isarray () const
523 // We treat null_type as not an array. This is ok based on the
524 // current uses of this method.
525 if (key
== reference_type
)
526 return data
.klass
->isArray ();
527 else if (key
== unresolved_reference_type
)
528 return data
.name
->data
[0] == '[';
534 return key
== null_type
;
537 bool isinterface (_Jv_BytecodeVerifier
*verifier
)
540 if (key
!= reference_type
)
542 return data
.klass
->isInterface ();
545 bool isabstract (_Jv_BytecodeVerifier
*verifier
)
548 if (key
!= reference_type
)
550 using namespace java::lang::reflect
;
551 return Modifier::isAbstract (data
.klass
->getModifiers ());
554 // Return the element type of an array.
555 type
element_type (_Jv_BytecodeVerifier
*verifier
)
557 // FIXME: maybe should do string manipulation here.
559 if (key
!= reference_type
)
560 verifier
->verify_fail ("programmer error in type::element_type()", -1);
562 jclass k
= data
.klass
->getComponentType ();
563 if (k
->isPrimitive ())
564 return type (verifier
->get_type_val_for_signature (k
));
568 // Return the array type corresponding to an initialized
569 // reference. We could expand this to work for other kinds of
570 // types, but currently we don't need to.
571 type
to_array (_Jv_BytecodeVerifier
*verifier
)
573 // Resolving isn't ideal, because it might force us to load
574 // another class, but it's easy. FIXME?
575 if (key
== unresolved_reference_type
)
578 if (key
== reference_type
)
579 return type (_Jv_GetArrayClass (data
.klass
,
580 data
.klass
->getClassLoader ()));
582 verifier
->verify_fail ("internal error in type::to_array()");
585 bool isreference () const
587 return key
>= reference_type
;
595 bool isinitialized () const
597 return (key
== reference_type
599 || key
== unresolved_reference_type
);
602 bool isresolved () const
604 return (key
== reference_type
606 || key
== uninitialized_reference_type
);
609 void verify_dimensions (int ndims
, _Jv_BytecodeVerifier
*verifier
)
611 // The way this is written, we don't need to check isarray().
612 if (key
== reference_type
)
614 jclass k
= data
.klass
;
615 while (k
->isArray () && ndims
> 0)
617 k
= k
->getComponentType ();
623 // We know KEY == unresolved_reference_type.
624 char *p
= data
.name
->data
;
625 while (*p
++ == '[' && ndims
-- > 0)
630 verifier
->verify_fail ("array type has fewer dimensions than required");
633 // Merge OLD_TYPE into this. On error throw exception.
634 bool merge (type
& old_type
, bool local_semantics
,
635 _Jv_BytecodeVerifier
*verifier
)
637 bool changed
= false;
638 bool refo
= old_type
.isreference ();
639 bool refn
= isreference ();
642 if (old_type
.key
== null_type
)
644 else if (key
== null_type
)
649 else if (isinitialized () != old_type
.isinitialized ())
650 verifier
->verify_fail ("merging initialized and uninitialized types");
653 if (! isinitialized ())
657 else if (old_type
.pc
== UNINIT
)
659 else if (pc
!= old_type
.pc
)
660 verifier
->verify_fail ("merging different uninitialized types");
664 && ! old_type
.isresolved ()
665 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
667 // Types are identical.
672 old_type
.resolve (verifier
);
674 jclass k
= data
.klass
;
675 jclass oldk
= old_type
.data
.klass
;
678 while (k
->isArray () && oldk
->isArray ())
681 k
= k
->getComponentType ();
682 oldk
= oldk
->getComponentType ();
685 // Ordinarily this terminates when we hit Object...
688 if (is_assignable_from_slow (k
, oldk
))
690 k
= k
->getSuperclass ();
693 // ... but K could have been an interface, in which
694 // case we'll end up here. We just convert this
697 k
= &java::lang::Object::class$
;
701 while (arraycount
> 0)
703 java::lang::ClassLoader
*loader
704 = verifier
->current_class
->getClassLoader();
705 k
= _Jv_GetArrayClass (k
, loader
);
713 else if (refo
|| refn
|| key
!= old_type
.key
)
717 // If we're merging into an "unused" slot, then we
718 // simply accept whatever we're merging from.
719 if (key
== unused_by_subroutine_type
)
724 else if (old_type
.key
== unused_by_subroutine_type
)
728 // If we already have an `unsuitable' type, then we
729 // don't need to change again.
730 else if (key
!= unsuitable_type
)
732 key
= unsuitable_type
;
737 verifier
->verify_fail ("unmergeable type");
743 void print (void) const
748 case boolean_type
: c
= 'Z'; break;
749 case byte_type
: c
= 'B'; break;
750 case char_type
: c
= 'C'; break;
751 case short_type
: c
= 'S'; break;
752 case int_type
: c
= 'I'; break;
753 case long_type
: c
= 'J'; break;
754 case float_type
: c
= 'F'; break;
755 case double_type
: c
= 'D'; break;
756 case void_type
: c
= 'V'; break;
757 case unsuitable_type
: c
= '-'; break;
758 case return_address_type
: c
= 'r'; break;
759 case continuation_type
: c
= '+'; break;
760 case unused_by_subroutine_type
: c
= '_'; break;
761 case reference_type
: c
= 'L'; break;
762 case null_type
: c
= '@'; break;
763 case unresolved_reference_type
: c
= 'l'; break;
764 case uninitialized_reference_type
: c
= 'U'; break;
765 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
767 debug_print ("%c", c
);
769 #endif /* VERIFY_DEBUG */
772 // This class holds all the state information we need for a given
776 // Current top of stack.
778 // Current stack depth. This is like the top of stack but it
779 // includes wide variable information.
783 // The local variables.
785 // This is used in subroutines to keep track of which local
786 // variables have been accessed.
788 // If not 0, then we are in a subroutine. The value is the PC of
789 // the subroutine's entry point. We can use 0 as an exceptional
790 // value because PC=0 can never be a subroutine.
792 // This is used to keep a linked list of all the states which
793 // require re-verification. We use the PC to keep track.
795 // We keep track of the type of `this' specially. This is used to
796 // ensure that an instance initializer invokes another initializer
797 // on `this' before returning. We must keep track of this
798 // specially because otherwise we might be confused by code which
799 // assigns to locals[0] (overwriting `this') and then returns
800 // without really initializing.
803 // INVALID marks a state which is not on the linked list of states
804 // requiring reverification.
805 static const int INVALID
= -1;
806 // NO_NEXT marks the state at the end of the reverification list.
807 static const int NO_NEXT
= -2;
814 local_changed
= NULL
;
817 state (int max_stack
, int max_locals
)
822 stack
= new type
[max_stack
];
823 for (int i
= 0; i
< max_stack
; ++i
)
824 stack
[i
] = unsuitable_type
;
825 locals
= new type
[max_locals
];
826 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
827 for (int i
= 0; i
< max_locals
; ++i
)
829 locals
[i
] = unsuitable_type
;
830 local_changed
[i
] = false;
836 state (const state
*orig
, int max_stack
, int max_locals
,
837 bool ret_semantics
= false)
839 stack
= new type
[max_stack
];
840 locals
= new type
[max_locals
];
841 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
842 copy (orig
, max_stack
, max_locals
, ret_semantics
);
853 _Jv_Free (local_changed
);
856 void *operator new[] (size_t bytes
)
858 return _Jv_Malloc (bytes
);
861 void operator delete[] (void *mem
)
866 void *operator new (size_t bytes
)
868 return _Jv_Malloc (bytes
);
871 void operator delete (void *mem
)
876 void copy (const state
*copy
, int max_stack
, int max_locals
,
877 bool ret_semantics
= false)
879 stacktop
= copy
->stacktop
;
880 stackdepth
= copy
->stackdepth
;
881 subroutine
= copy
->subroutine
;
882 for (int i
= 0; i
< max_stack
; ++i
)
883 stack
[i
] = copy
->stack
[i
];
884 for (int i
= 0; i
< max_locals
; ++i
)
886 // See push_jump_merge to understand this case.
888 locals
[i
] = type (copy
->local_changed
[i
]
890 : unused_by_subroutine_type
);
892 locals
[i
] = copy
->locals
[i
];
893 local_changed
[i
] = copy
->local_changed
[i
];
895 this_type
= copy
->this_type
;
896 // Don't modify `next'.
899 // Modify this state to reflect entry to an exception handler.
900 void set_exception (type t
, int max_stack
)
905 for (int i
= stacktop
; i
< max_stack
; ++i
)
906 stack
[i
] = unsuitable_type
;
909 // Modify this state to reflect entry into a subroutine.
910 void enter_subroutine (int npc
, int max_locals
)
913 // Mark all items as unchanged. Each subroutine needs to keep
914 // track of its `changed' state independently. In the case of
915 // nested subroutines, this information will be merged back into
916 // parent by the `ret'.
917 for (int i
= 0; i
< max_locals
; ++i
)
918 local_changed
[i
] = false;
921 // Merge STATE_OLD into this state. Destructively modifies this
922 // state. Returns true if the new state was in fact changed.
923 // Will throw an exception if the states are not mergeable.
924 bool merge (state
*state_old
, bool ret_semantics
,
925 int max_locals
, _Jv_BytecodeVerifier
*verifier
)
927 bool changed
= false;
929 // Special handling for `this'. If one or the other is
930 // uninitialized, then the merge is uninitialized.
931 if (this_type
.isinitialized ())
932 this_type
= state_old
->this_type
;
934 // Merge subroutine states. Here we just keep track of what
935 // subroutine we think we're in. We only check for a merge
936 // (which is invalid) when we see a `ret'.
937 if (subroutine
== state_old
->subroutine
)
941 else if (subroutine
== 0)
943 subroutine
= state_old
->subroutine
;
948 // If the subroutines differ, indicate that the state
949 // changed. This is needed to detect when subroutines have
955 if (state_old
->stacktop
!= stacktop
)
956 verifier
->verify_fail ("stack sizes differ");
957 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
959 if (stack
[i
].merge (state_old
->stack
[i
], false, verifier
))
963 // Merge local variables.
964 for (int i
= 0; i
< max_locals
; ++i
)
966 // If we're not processing a `ret', then we merge every
967 // local variable. If we are processing a `ret', then we
968 // only merge locals which changed in the subroutine. When
969 // processing a `ret', STATE_OLD is the state at the point
970 // of the `ret', and THIS is the state just after the `jsr'.
971 if (! ret_semantics
|| state_old
->local_changed
[i
])
973 if (locals
[i
].merge (state_old
->locals
[i
], true, verifier
))
975 // Note that we don't call `note_variable' here.
976 // This change doesn't represent a real change to a
977 // local, but rather a merge artifact. If we're in
978 // a subroutine which is called with two
979 // incompatible types in a slot that is unused by
980 // the subroutine, then we don't want to mark that
981 // variable as having been modified.
986 // If we're in a subroutine, we must compute the union of
987 // all the changed local variables.
988 if (state_old
->local_changed
[i
])
995 // Throw an exception if there is an uninitialized object on the
996 // stack or in a local variable. EXCEPTION_SEMANTICS controls
997 // whether we're using backwards-branch or exception-handing
999 void check_no_uninitialized_objects (int max_locals
,
1000 _Jv_BytecodeVerifier
*verifier
,
1001 bool exception_semantics
= false)
1003 if (! exception_semantics
)
1005 for (int i
= 0; i
< stacktop
; ++i
)
1006 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
1007 verifier
->verify_fail ("uninitialized object on stack");
1010 for (int i
= 0; i
< max_locals
; ++i
)
1011 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
1012 verifier
->verify_fail ("uninitialized object in local variable");
1014 check_this_initialized (verifier
);
1017 // Ensure that `this' has been initialized.
1018 void check_this_initialized (_Jv_BytecodeVerifier
*verifier
)
1020 if (this_type
.isreference () && ! this_type
.isinitialized ())
1021 verifier
->verify_fail ("`this' is uninitialized");
1024 // Set type of `this'.
1025 void set_this_type (const type
&k
)
1030 // Note that a local variable was modified.
1031 void note_variable (int index
)
1034 local_changed
[index
] = true;
1037 // Mark each `new'd object we know of that was allocated at PC as
1039 void set_initialized (int pc
, int max_locals
)
1041 for (int i
= 0; i
< stacktop
; ++i
)
1042 stack
[i
].set_initialized (pc
);
1043 for (int i
= 0; i
< max_locals
; ++i
)
1044 locals
[i
].set_initialized (pc
);
1045 this_type
.set_initialized (pc
);
1048 // Return true if this state is the unmerged result of a `ret'.
1049 bool is_unmerged_ret_state (int max_locals
) const
1051 for (int i
= 0; i
< max_locals
; ++i
)
1053 if (locals
[i
].key
== unused_by_subroutine_type
)
1060 void print (const char *leader
, int pc
,
1061 int max_stack
, int max_locals
) const
1063 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1065 for (i
= 0; i
< stacktop
; ++i
)
1067 for (; i
< max_stack
; ++i
)
1069 debug_print (" [local] ");
1070 for (i
= 0; i
< max_locals
; ++i
)
1073 debug_print (local_changed
[i
] ? "+" : " ");
1075 if (subroutine
== 0)
1076 debug_print (" | None");
1078 debug_print (" | %4d", subroutine
);
1079 debug_print (" | %p\n", this);
1082 inline void print (const char *, int, int, int) const
1085 #endif /* VERIFY_DEBUG */
1090 if (current_state
->stacktop
<= 0)
1091 verify_fail ("stack empty");
1092 type r
= current_state
->stack
[--current_state
->stacktop
];
1093 current_state
->stackdepth
-= r
.depth ();
1094 if (current_state
->stackdepth
< 0)
1095 verify_fail ("stack empty", start_PC
);
1101 type r
= pop_raw ();
1103 verify_fail ("narrow pop of wide type");
1109 type r
= pop_raw ();
1111 verify_fail ("wide pop of narrow type");
1115 type
pop_type (type match
)
1118 type t
= pop_raw ();
1119 if (! match
.compatible (t
, this))
1120 verify_fail ("incompatible type on stack");
1124 // Pop a reference type or a return address.
1125 type
pop_ref_or_return ()
1127 type t
= pop_raw ();
1128 if (! t
.isreference () && t
.key
!= return_address_type
)
1129 verify_fail ("expected reference or return address on stack");
1133 void push_type (type t
)
1135 // If T is a numeric type like short, promote it to int.
1138 int depth
= t
.depth ();
1139 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1140 verify_fail ("stack overflow");
1141 current_state
->stack
[current_state
->stacktop
++] = t
;
1142 current_state
->stackdepth
+= depth
;
1145 void set_variable (int index
, type t
)
1147 // If T is a numeric type like short, promote it to int.
1150 int depth
= t
.depth ();
1151 if (index
> current_method
->max_locals
- depth
)
1152 verify_fail ("invalid local variable");
1153 current_state
->locals
[index
] = t
;
1154 current_state
->note_variable (index
);
1158 current_state
->locals
[index
+ 1] = continuation_type
;
1159 current_state
->note_variable (index
+ 1);
1161 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1163 current_state
->locals
[index
- 1] = unsuitable_type
;
1164 // There's no need to call note_variable here.
1168 type
get_variable (int index
, type t
)
1170 int depth
= t
.depth ();
1171 if (index
> current_method
->max_locals
- depth
)
1172 verify_fail ("invalid local variable");
1173 if (! t
.compatible (current_state
->locals
[index
], this))
1174 verify_fail ("incompatible type in local variable");
1177 type
t (continuation_type
);
1178 if (! current_state
->locals
[index
+ 1].compatible (t
, this))
1179 verify_fail ("invalid local variable");
1181 return current_state
->locals
[index
];
1184 // Make sure ARRAY is an array type and that its elements are
1185 // compatible with type ELEMENT. Returns the actual element type.
1186 type
require_array_type (type array
, type element
)
1188 // An odd case. Here we just pretend that everything went ok. If
1189 // the requested element type is some kind of reference, return
1190 // the null type instead.
1191 if (array
.isnull ())
1192 return element
.isreference () ? type (null_type
) : element
;
1194 if (! array
.isarray ())
1195 verify_fail ("array required");
1197 type t
= array
.element_type (this);
1198 if (! element
.compatible (t
, this))
1200 // Special case for byte arrays, which must also be boolean
1203 if (element
.key
== byte_type
)
1205 type
e2 (boolean_type
);
1206 ok
= e2
.compatible (t
, this);
1209 verify_fail ("incompatible array element type");
1212 // Return T and not ELEMENT, because T might be specialized.
1218 if (PC
>= current_method
->code_length
)
1219 verify_fail ("premature end of bytecode");
1220 return (jint
) bytecode
[PC
++] & 0xff;
1225 jint b1
= get_byte ();
1226 jint b2
= get_byte ();
1227 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1232 jint b1
= get_byte ();
1233 jint b2
= get_byte ();
1234 jshort s
= (b1
<< 8) | b2
;
1240 jint b1
= get_byte ();
1241 jint b2
= get_byte ();
1242 jint b3
= get_byte ();
1243 jint b4
= get_byte ();
1244 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1247 int compute_jump (int offset
)
1249 int npc
= start_PC
+ offset
;
1250 if (npc
< 0 || npc
>= current_method
->code_length
)
1251 verify_fail ("branch out of range", start_PC
);
1255 // Merge the indicated state into the state at the branch target and
1256 // schedule a new PC if there is a change. If RET_SEMANTICS is
1257 // true, then we are merging from a `ret' instruction into the
1258 // instruction after a `jsr'. This is a special case with its own
1259 // modified semantics.
1260 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1262 bool changed
= true;
1263 if (states
[npc
] == NULL
)
1265 // There's a weird situation here. If are examining the
1266 // branch that results from a `ret', and there is not yet a
1267 // state available at the branch target (the instruction just
1268 // after the `jsr'), then we have to construct a special kind
1269 // of state at that point for future merging. This special
1270 // state has the type `unused_by_subroutine_type' in each slot
1271 // which was not modified by the subroutine.
1272 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1273 current_method
->max_locals
, ret_semantics
);
1274 debug_print ("== New state in push_jump_merge\n");
1275 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1276 current_method
->max_locals
);
1280 debug_print ("== Merge states in push_jump_merge\n");
1281 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1282 current_method
->max_locals
);
1283 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1284 current_method
->max_locals
);
1285 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1286 current_method
->max_locals
, this);
1287 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1288 current_method
->max_locals
);
1291 if (changed
&& states
[npc
]->next
== state::INVALID
)
1293 // The merge changed the state, and the new PC isn't yet on our
1294 // list of PCs to re-verify.
1295 states
[npc
]->next
= next_verify_pc
;
1296 next_verify_pc
= npc
;
1300 void push_jump (int offset
)
1302 int npc
= compute_jump (offset
);
1304 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1305 push_jump_merge (npc
, current_state
);
1308 void push_exception_jump (type t
, int pc
)
1310 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1312 state
s (current_state
, current_method
->max_stack
,
1313 current_method
->max_locals
);
1314 if (current_method
->max_stack
< 1)
1315 verify_fail ("stack overflow at exception handler");
1316 s
.set_exception (t
, current_method
->max_stack
);
1317 push_jump_merge (pc
, &s
);
1322 int *prev_loc
= &next_verify_pc
;
1323 int npc
= next_verify_pc
;
1324 bool skipped
= false;
1326 while (npc
!= state::NO_NEXT
)
1328 // If the next available PC is an unmerged `ret' state, then
1329 // we aren't yet ready to handle it. That's because we would
1330 // need all kind of special cases to do so. So instead we
1331 // defer this jump until after we've processed it via a
1332 // fall-through. This has to happen because the instruction
1333 // before this one must be a `jsr'.
1334 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1336 *prev_loc
= states
[npc
]->next
;
1337 states
[npc
]->next
= state::INVALID
;
1342 prev_loc
= &states
[npc
]->next
;
1343 npc
= states
[npc
]->next
;
1346 // If we've skipped states and there is nothing else, that's a
1349 verify_fail ("pop_jump: can't happen");
1350 return state::NO_NEXT
;
1353 void invalidate_pc ()
1355 PC
= state::NO_NEXT
;
1358 void note_branch_target (int pc
, bool is_jsr_target
= false)
1360 // Don't check `pc <= PC', because we've advanced PC after
1361 // fetching the target and we haven't yet checked the next
1363 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1364 verify_fail ("branch not to instruction start", start_PC
);
1365 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1368 // Record the jsr which called this instruction.
1369 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1371 info
->next
= jsr_ptrs
[pc
];
1372 jsr_ptrs
[pc
] = info
;
1376 void skip_padding ()
1378 while ((PC
% 4) > 0)
1379 if (get_byte () != 0)
1380 verify_fail ("found nonzero padding byte");
1383 // Return the subroutine to which the instruction at PC belongs.
1384 int get_subroutine (int pc
)
1386 if (states
[pc
] == NULL
)
1388 return states
[pc
]->subroutine
;
1391 // Do the work for a `ret' instruction. INDEX is the index into the
1393 void handle_ret_insn (int index
)
1395 get_variable (index
, return_address_type
);
1397 int csub
= current_state
->subroutine
;
1399 verify_fail ("no subroutine");
1401 // Check to see if we've merged subroutines.
1402 subr_entry_info
*entry
;
1403 for (entry
= entry_points
; entry
!= NULL
; entry
= entry
->next
)
1405 if (entry
->ret_pc
== start_PC
)
1410 entry
= (subr_entry_info
*) _Jv_Malloc (sizeof (subr_entry_info
));
1412 entry
->ret_pc
= start_PC
;
1413 entry
->next
= entry_points
;
1414 entry_points
= entry
;
1416 else if (entry
->pc
!= csub
)
1417 verify_fail ("subroutines merged");
1419 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1421 // Temporarily modify the current state so it looks like we're
1422 // in the enclosing context.
1423 current_state
->subroutine
= get_subroutine (subr
->pc
);
1425 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1426 push_jump_merge (subr
->pc
, current_state
, true);
1429 current_state
->subroutine
= csub
;
1433 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1434 // sure this subroutine isn't already on the stack.
1435 void check_nonrecursive_call (int sub
, int dest
)
1440 verify_fail ("recursive subroutine call");
1441 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1442 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1445 void handle_jsr_insn (int offset
)
1447 int npc
= compute_jump (offset
);
1450 current_state
->check_no_uninitialized_objects (current_method
->max_locals
, this);
1451 check_nonrecursive_call (current_state
->subroutine
, npc
);
1453 // Create a new state and modify it as appropriate for entry into
1454 // a subroutine. We're writing this in a weird way because,
1455 // unfortunately, push_type only works on the current state.
1456 push_type (return_address_type
);
1457 push_jump_merge (npc
, current_state
);
1458 // Clean up the weirdness.
1459 pop_type (return_address_type
);
1461 // On entry to the subroutine, the subroutine number must be set
1462 // and the locals must be marked as cleared. We do this after
1463 // merging state so that we don't erroneously "notice" a variable
1464 // change merely on entry.
1465 states
[npc
]->enter_subroutine (npc
, current_method
->max_locals
);
1468 jclass
construct_primitive_array_type (type_val prim
)
1474 k
= JvPrimClass (boolean
);
1477 k
= JvPrimClass (char);
1480 k
= JvPrimClass (float);
1483 k
= JvPrimClass (double);
1486 k
= JvPrimClass (byte
);
1489 k
= JvPrimClass (short);
1492 k
= JvPrimClass (int);
1495 k
= JvPrimClass (long);
1498 verify_fail ("unknown type in construct_primitive_array_type");
1500 k
= _Jv_GetArrayClass (k
, NULL
);
1504 // This pass computes the location of branch targets and also
1505 // instruction starts.
1506 void branch_prepass ()
1508 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1509 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1510 * current_method
->code_length
);
1512 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1518 bool last_was_jsr
= false;
1521 while (PC
< current_method
->code_length
)
1523 // Set `start_PC' early so that error checking can have the
1526 flags
[PC
] |= FLAG_INSN_START
;
1528 // If the previous instruction was a jsr, then the next
1529 // instruction is a branch target -- the branch being the
1530 // corresponding `ret'.
1532 note_branch_target (PC
);
1533 last_was_jsr
= false;
1535 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1539 case op_aconst_null
:
1675 case op_monitorenter
:
1676 case op_monitorexit
:
1684 case op_arraylength
:
1716 case op_invokespecial
:
1717 case op_invokestatic
:
1718 case op_invokevirtual
:
1722 case op_multianewarray
:
1728 last_was_jsr
= true;
1747 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1750 case op_tableswitch
:
1753 note_branch_target (compute_jump (get_int ()));
1754 jint low
= get_int ();
1755 jint hi
= get_int ();
1757 verify_fail ("invalid tableswitch", start_PC
);
1758 for (int i
= low
; i
<= hi
; ++i
)
1759 note_branch_target (compute_jump (get_int ()));
1763 case op_lookupswitch
:
1766 note_branch_target (compute_jump (get_int ()));
1767 int npairs
= get_int ();
1769 verify_fail ("too few pairs in lookupswitch", start_PC
);
1770 while (npairs
-- > 0)
1773 note_branch_target (compute_jump (get_int ()));
1778 case op_invokeinterface
:
1786 opcode
= (java_opcode
) get_byte ();
1788 if (opcode
== op_iinc
)
1794 last_was_jsr
= true;
1797 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1801 verify_fail ("unrecognized instruction in branch_prepass",
1805 // See if any previous branch tried to branch to the middle of
1806 // this instruction.
1807 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1809 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1810 verify_fail ("branch to middle of instruction", pc
);
1814 // Verify exception handlers.
1815 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1817 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1818 verify_fail ("exception handler not at instruction start",
1819 exception
[i
].handler_pc
);
1820 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1821 verify_fail ("exception start not at instruction start",
1822 exception
[i
].start_pc
);
1823 if (exception
[i
].end_pc
!= current_method
->code_length
1824 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1825 verify_fail ("exception end not at instruction start",
1826 exception
[i
].end_pc
);
1828 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1832 void check_pool_index (int index
)
1834 if (index
< 0 || index
>= current_class
->constants
.size
)
1835 verify_fail ("constant pool index out of range", start_PC
);
1838 type
check_class_constant (int index
)
1840 check_pool_index (index
);
1841 _Jv_Constants
*pool
= ¤t_class
->constants
;
1842 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1843 return type (pool
->data
[index
].clazz
);
1844 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1845 return type (pool
->data
[index
].utf8
);
1846 verify_fail ("expected class constant", start_PC
);
1849 type
check_constant (int index
)
1851 check_pool_index (index
);
1852 _Jv_Constants
*pool
= ¤t_class
->constants
;
1853 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1854 || pool
->tags
[index
] == JV_CONSTANT_String
)
1855 return type (&java::lang::String::class$
);
1856 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1857 return type (int_type
);
1858 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1859 return type (float_type
);
1860 verify_fail ("String, int, or float constant expected", start_PC
);
1863 type
check_wide_constant (int index
)
1865 check_pool_index (index
);
1866 _Jv_Constants
*pool
= ¤t_class
->constants
;
1867 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1868 return type (long_type
);
1869 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1870 return type (double_type
);
1871 verify_fail ("long or double constant expected", start_PC
);
1874 // Helper for both field and method. These are laid out the same in
1875 // the constant pool.
1876 type
handle_field_or_method (int index
, int expected
,
1877 _Jv_Utf8Const
**name
,
1878 _Jv_Utf8Const
**fmtype
)
1880 check_pool_index (index
);
1881 _Jv_Constants
*pool
= ¤t_class
->constants
;
1882 if (pool
->tags
[index
] != expected
)
1883 verify_fail ("didn't see expected constant", start_PC
);
1884 // Once we know we have a Fieldref or Methodref we assume that it
1885 // is correctly laid out in the constant pool. I think the code
1886 // in defineclass.cc guarantees this.
1887 _Jv_ushort class_index
, name_and_type_index
;
1888 _Jv_loadIndexes (&pool
->data
[index
],
1890 name_and_type_index
);
1891 _Jv_ushort name_index
, desc_index
;
1892 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1893 name_index
, desc_index
);
1895 *name
= pool
->data
[name_index
].utf8
;
1896 *fmtype
= pool
->data
[desc_index
].utf8
;
1898 return check_class_constant (class_index
);
1901 // Return field's type, compute class' type if requested.
1902 type
check_field_constant (int index
, type
*class_type
= NULL
)
1904 _Jv_Utf8Const
*name
, *field_type
;
1905 type ct
= handle_field_or_method (index
,
1906 JV_CONSTANT_Fieldref
,
1907 &name
, &field_type
);
1910 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1911 return type (field_type
);
1912 return get_type_val_for_signature (field_type
->data
[0]);
1915 type
check_method_constant (int index
, bool is_interface
,
1916 _Jv_Utf8Const
**method_name
,
1917 _Jv_Utf8Const
**method_signature
)
1919 return handle_field_or_method (index
,
1921 ? JV_CONSTANT_InterfaceMethodref
1922 : JV_CONSTANT_Methodref
),
1923 method_name
, method_signature
);
1926 type
get_one_type (char *&p
)
1944 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1948 // Casting to jchar here is ok since we are looking at an ASCII
1950 type_val rt
= get_type_val_for_signature (jchar (v
));
1952 if (arraycount
== 0)
1954 // Callers of this function eventually push their arguments on
1955 // the stack. So, promote them here.
1956 return type (rt
).promote ();
1959 jclass k
= construct_primitive_array_type (rt
);
1960 while (--arraycount
> 0)
1961 k
= _Jv_GetArrayClass (k
, NULL
);
1965 void compute_argument_types (_Jv_Utf8Const
*signature
,
1968 char *p
= signature
->data
;
1974 types
[i
++] = get_one_type (p
);
1977 type
compute_return_type (_Jv_Utf8Const
*signature
)
1979 char *p
= signature
->data
;
1983 return get_one_type (p
);
1986 void check_return_type (type onstack
)
1988 type rt
= compute_return_type (current_method
->self
->signature
);
1989 if (! rt
.compatible (onstack
, this))
1990 verify_fail ("incompatible return type");
1993 // Initialize the stack for the new method. Returns true if this
1994 // method is an instance initializer.
1995 bool initialize_stack ()
1998 bool is_init
= false;
2000 using namespace java::lang::reflect
;
2001 if (! Modifier::isStatic (current_method
->self
->accflags
))
2003 type
kurr (current_class
);
2004 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
2006 kurr
.set_uninitialized (type::SELF
, this);
2009 set_variable (0, kurr
);
2010 current_state
->set_this_type (kurr
);
2014 // We have to handle wide arguments specially here.
2015 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
2016 type arg_types
[arg_count
];
2017 compute_argument_types (current_method
->self
->signature
, arg_types
);
2018 for (int i
= 0; i
< arg_count
; ++i
)
2020 set_variable (var
, arg_types
[i
]);
2022 if (arg_types
[i
].iswide ())
2029 void verify_instructions_0 ()
2031 current_state
= new state (current_method
->max_stack
,
2032 current_method
->max_locals
);
2037 // True if we are verifying an instance initializer.
2038 bool this_is_init
= initialize_stack ();
2040 states
= (state
**) _Jv_Malloc (sizeof (state
*)
2041 * current_method
->code_length
);
2042 for (int i
= 0; i
< current_method
->code_length
; ++i
)
2045 next_verify_pc
= state::NO_NEXT
;
2049 // If the PC was invalidated, get a new one from the work list.
2050 if (PC
== state::NO_NEXT
)
2053 if (PC
== state::INVALID
)
2054 verify_fail ("can't happen: saw state::INVALID");
2055 if (PC
== state::NO_NEXT
)
2057 debug_print ("== State pop from pending list\n");
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 */