1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001 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 // * read more about when classes must be loaded
36 // * class loader madness
37 // * Lots and lots of debugging and testing
38 // * type representation is still ugly. look for the big switches
39 // * at least one GC problem :-(
42 // This is global because __attribute__ doesn't seem to work on static
44 static void verify_fail (char *msg
, jint pc
= -1)
45 __attribute__ ((__noreturn__
));
47 static void debug_print (const char *fmt
, ...)
48 __attribute__ ((format (printf
, 1, 2)));
51 debug_print (const char *fmt
, ...)
56 vfprintf (stderr
, fmt
, ap
);
58 #endif /* VERIFY_DEBUG */
61 class _Jv_BytecodeVerifier
65 static const int FLAG_INSN_START
= 1;
66 static const int FLAG_BRANCH_TARGET
= 2;
75 // The PC corresponding to the start of the current instruction.
78 // The current state of the stack, locals, etc.
81 // We store the state at branch targets, for merging. This holds
85 // We keep a linked list of all the PCs which we must reverify.
86 // The link is done using the PC values. This is the head of the
90 // We keep some flags for each instruction. The values are the
91 // FLAG_* constants defined above.
94 // We need to keep track of which instructions can call a given
95 // subroutine. FIXME: this is inefficient. We keep a linked list
96 // of all calling `jsr's at at each jsr target.
99 // The current top of the stack, in terms of slots.
101 // The current depth of the stack. This will be larger than
102 // STACKTOP when wide types are on the stack.
105 // The bytecode itself.
106 unsigned char *bytecode
;
108 _Jv_InterpException
*exception
;
111 jclass current_class
;
113 _Jv_InterpMethod
*current_method
;
115 // A linked list of utf8 objects we allocate. This is really ugly,
116 // but without this our utf8 objects would be collected.
117 linked_utf8
*utf8_list
;
125 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
127 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
128 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
131 r
->length
= val
->length
;
133 memcpy (r
->data
, val
->data
, val
->length
+ 1);
135 linked_utf8
*lu
= (linked_utf8
*) _Jv_Malloc (sizeof (linked_utf8
));
137 lu
->next
= utf8_list
;
143 // This enum holds a list of tags for all the different types we
144 // need to handle. Reference types are treated specially by the
150 // The values for primitive types are chosen to correspond to values
151 // specified to newarray.
161 // Used when overwriting second word of a double or long in the
162 // local variables. Also used after merging local variable states
163 // to indicate an unusable value.
168 // There is an obscure special case which requires us to note when
169 // a local variable has not been used by a subroutine. See
170 // push_jump_merge for more information.
171 unused_by_subroutine_type
,
173 // Everything after `reference_type' must be a reference type.
176 unresolved_reference_type
,
177 uninitialized_reference_type
,
178 uninitialized_unresolved_reference_type
181 // Return the type_val corresponding to a primitive signature
182 // character. For instance `I' returns `int.class'.
183 static type_val
get_type_val_for_signature (jchar sig
)
216 verify_fail ("invalid signature");
221 // Return the type_val corresponding to a primitive class.
222 static type_val
get_type_val_for_signature (jclass k
)
224 return get_type_val_for_signature ((jchar
) k
->method_count
);
227 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
228 // TARGET haven't been prepared.
229 static bool is_assignable_from_slow (jclass target
, jclass source
)
231 // This will terminate when SOURCE==Object.
234 if (source
== target
)
237 if (target
->isPrimitive () || source
->isPrimitive ())
240 // Check array case first because we can have an array whose
241 // component type is not prepared; _Jv_IsAssignableFrom
242 // doesn't handle this correctly.
243 if (target
->isArray ())
245 if (! source
->isArray ())
247 target
= target
->getComponentType ();
248 source
= source
->getComponentType ();
250 // _Jv_IsAssignableFrom can handle a target which is an
251 // interface even if it hasn't been prepared.
252 else if ((target
->state
> JV_STATE_LINKED
|| target
->isInterface ())
253 && source
->state
> JV_STATE_LINKED
)
254 return _Jv_IsAssignableFrom (target
, source
);
255 else if (target
->isInterface ())
257 for (int i
= 0; i
< source
->interface_count
; ++i
)
259 // We use a recursive call because we also need to
260 // check superinterfaces.
261 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
266 else if (target
== &java::lang::Object::class$
)
268 else if (source
->isInterface ()
269 || source
== &java::lang::Object::class$
)
272 source
= source
->getSuperclass ();
276 // This is used to keep track of which `jsr's correspond to a given
280 // PC of the instruction just after the jsr.
286 // The `type' class is used to represent a single type in the
292 // Some associated data.
295 // For a resolved reference type, this is a pointer to the class.
297 // For other reference types, this it the name of the class.
300 // This is used when constructing a new object. It is the PC of the
301 // `new' instruction which created the object. We use the special
302 // value -2 to mean that this is uninitialized, and the special
303 // value -1 for the case where the current method is itself the
307 static const int UNINIT
= -2;
308 static const int SELF
= -1;
310 // Basic constructor.
313 key
= unsuitable_type
;
318 // Make a new instance given the type tag. We assume a generic
319 // `reference_type' means Object.
324 if (key
== reference_type
)
325 data
.klass
= &java::lang::Object::class$
;
329 // Make a new instance given a class.
332 key
= reference_type
;
337 // Make a new instance given the name of a class.
338 type (_Jv_Utf8Const
*n
)
340 key
= unresolved_reference_type
;
353 // These operators are required because libgcj can't link in
355 void *operator new[] (size_t bytes
)
357 return _Jv_Malloc (bytes
);
360 void operator delete[] (void *mem
)
365 type
& operator= (type_val k
)
373 type
& operator= (const type
& t
)
381 // Promote a numeric type.
384 if (key
== boolean_type
|| key
== char_type
385 || key
== byte_type
|| key
== short_type
)
390 // If *THIS is an unresolved reference type, resolve it.
393 if (key
!= unresolved_reference_type
394 && key
!= uninitialized_unresolved_reference_type
)
397 // FIXME: class loader
398 using namespace java::lang
;
399 // We might see either kind of name. Sigh.
400 if (data
.name
->data
[0] == 'L'
401 && data
.name
->data
[data
.name
->length
- 1] == ';')
402 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, NULL
);
404 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
406 key
= (key
== unresolved_reference_type
408 : uninitialized_reference_type
);
411 // Mark this type as the uninitialized result of `new'.
412 void set_uninitialized (int npc
)
414 if (key
== reference_type
)
415 key
= uninitialized_reference_type
;
416 else if (key
== unresolved_reference_type
)
417 key
= uninitialized_unresolved_reference_type
;
419 verify_fail ("internal error in type::uninitialized");
423 // Mark this type as now initialized.
424 void set_initialized (int npc
)
426 if (npc
!= UNINIT
&& pc
== npc
427 && (key
== uninitialized_reference_type
428 || key
== uninitialized_unresolved_reference_type
))
430 key
= (key
== uninitialized_reference_type
432 : unresolved_reference_type
);
438 // Return true if an object of type K can be assigned to a variable
439 // of type *THIS. Handle various special cases too. Might modify
440 // *THIS or K. Note however that this does not perform numeric
442 bool compatible (type
&k
)
444 // Any type is compatible with the unsuitable type.
445 if (key
== unsuitable_type
)
448 if (key
< reference_type
|| k
.key
< reference_type
)
451 // The `null' type is convertible to any reference type.
452 // FIXME: is this correct for THIS?
453 if (key
== null_type
|| k
.key
== null_type
)
456 // Any reference type is convertible to Object. This is a special
457 // case so we don't need to unnecessarily resolve a class.
458 if (key
== reference_type
459 && data
.klass
== &java::lang::Object::class$
)
462 // An initialized type and an uninitialized type are not
464 if (isinitialized () != k
.isinitialized ())
467 // Two uninitialized objects are compatible if either:
468 // * The PCs are identical, or
469 // * One PC is UNINIT.
470 if (! isinitialized ())
472 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
476 // Two unresolved types are equal if their names are the same.
479 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
482 // We must resolve both types and check assignability.
485 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
490 return key
== void_type
;
495 return key
== long_type
|| key
== double_type
;
498 // Return number of stack or local variable slots taken by this
502 return iswide () ? 2 : 1;
505 bool isarray () const
507 // We treat null_type as not an array. This is ok based on the
508 // current uses of this method.
509 if (key
== reference_type
)
510 return data
.klass
->isArray ();
511 else if (key
== unresolved_reference_type
)
512 return data
.name
->data
[0] == '[';
519 if (key
!= reference_type
)
521 return data
.klass
->isInterface ();
527 if (key
!= reference_type
)
529 using namespace java::lang::reflect
;
530 return Modifier::isAbstract (data
.klass
->getModifiers ());
533 // Return the element type of an array.
536 // FIXME: maybe should do string manipulation here.
538 if (key
!= reference_type
)
539 verify_fail ("programmer error in type::element_type()");
541 jclass k
= data
.klass
->getComponentType ();
542 if (k
->isPrimitive ())
543 return type (get_type_val_for_signature (k
));
547 // Return the array type corresponding to an initialized
548 // reference. We could expand this to work for other kinds of
549 // types, but currently we don't need to.
552 // Resolving isn't ideal, because it might force us to load
553 // another class, but it's easy. FIXME?
554 if (key
== unresolved_reference_type
)
557 if (key
== reference_type
)
558 return type (_Jv_GetArrayClass (data
.klass
,
559 data
.klass
->getClassLoader ()));
561 verify_fail ("internal error in type::to_array()");
564 bool isreference () const
566 return key
>= reference_type
;
574 bool isinitialized () const
576 return (key
== reference_type
578 || key
== unresolved_reference_type
);
581 bool isresolved () const
583 return (key
== reference_type
585 || key
== uninitialized_reference_type
);
588 void verify_dimensions (int ndims
)
590 // The way this is written, we don't need to check isarray().
591 if (key
== reference_type
)
593 jclass k
= data
.klass
;
594 while (k
->isArray () && ndims
> 0)
596 k
= k
->getComponentType ();
602 // We know KEY == unresolved_reference_type.
603 char *p
= data
.name
->data
;
604 while (*p
++ == '[' && ndims
-- > 0)
609 verify_fail ("array type has fewer dimensions than required");
612 // Merge OLD_TYPE into this. On error throw exception.
613 bool merge (type
& old_type
, bool local_semantics
= false)
615 bool changed
= false;
616 bool refo
= old_type
.isreference ();
617 bool refn
= isreference ();
620 if (old_type
.key
== null_type
)
622 else if (key
== null_type
)
627 else if (isinitialized () != old_type
.isinitialized ())
628 verify_fail ("merging initialized and uninitialized types");
631 if (! isinitialized ())
635 else if (old_type
.pc
== UNINIT
)
637 else if (pc
!= old_type
.pc
)
638 verify_fail ("merging different uninitialized types");
642 && ! old_type
.isresolved ()
643 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
645 // Types are identical.
652 jclass k
= data
.klass
;
653 jclass oldk
= old_type
.data
.klass
;
656 while (k
->isArray () && oldk
->isArray ())
659 k
= k
->getComponentType ();
660 oldk
= oldk
->getComponentType ();
663 // This loop will end when we hit Object.
666 if (is_assignable_from_slow (k
, oldk
))
668 k
= k
->getSuperclass ();
674 while (arraycount
> 0)
676 // FIXME: Class loader.
677 k
= _Jv_GetArrayClass (k
, NULL
);
685 else if (refo
|| refn
|| key
!= old_type
.key
)
689 // If we're merging into an "unused" slot, then we
690 // simply accept whatever we're merging from.
691 if (key
== unused_by_subroutine_type
)
696 else if (old_type
.key
== unused_by_subroutine_type
)
700 // If we already have an `unsuitable' type, then we
701 // don't need to change again.
702 else if (key
!= unsuitable_type
)
704 key
= unsuitable_type
;
709 verify_fail ("unmergeable type");
715 void print (void) const
720 case boolean_type
: c
= 'Z'; break;
721 case byte_type
: c
= 'B'; break;
722 case char_type
: c
= 'C'; break;
723 case short_type
: c
= 'S'; break;
724 case int_type
: c
= 'I'; break;
725 case long_type
: c
= 'J'; break;
726 case float_type
: c
= 'F'; break;
727 case double_type
: c
= 'D'; break;
728 case void_type
: c
= 'V'; break;
729 case unsuitable_type
: c
= '-'; break;
730 case return_address_type
: c
= 'r'; break;
731 case continuation_type
: c
= '+'; break;
732 case unused_by_subroutine_type
: c
= '_'; break;
733 case reference_type
: c
= 'L'; break;
734 case null_type
: c
= '@'; break;
735 case unresolved_reference_type
: c
= 'l'; break;
736 case uninitialized_reference_type
: c
= 'U'; break;
737 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
739 debug_print ("%c", c
);
741 #endif /* VERIFY_DEBUG */
744 // This class holds all the state information we need for a given
748 // Current top of stack.
750 // Current stack depth. This is like the top of stack but it
751 // includes wide variable information.
755 // The local variables.
757 // This is used in subroutines to keep track of which local
758 // variables have been accessed.
760 // If not 0, then we are in a subroutine. The value is the PC of
761 // the subroutine's entry point. We can use 0 as an exceptional
762 // value because PC=0 can never be a subroutine.
764 // This is used to keep a linked list of all the states which
765 // require re-verification. We use the PC to keep track.
767 // We keep track of the type of `this' specially. This is used to
768 // ensure that an instance initializer invokes another initializer
769 // on `this' before returning. We must keep track of this
770 // specially because otherwise we might be confused by code which
771 // assigns to locals[0] (overwriting `this') and then returns
772 // without really initializing.
775 // INVALID marks a state which is not on the linked list of states
776 // requiring reverification.
777 static const int INVALID
= -1;
778 // NO_NEXT marks the state at the end of the reverification list.
779 static const int NO_NEXT
= -2;
786 local_changed
= NULL
;
789 state (int max_stack
, int max_locals
)
794 stack
= new type
[max_stack
];
795 for (int i
= 0; i
< max_stack
; ++i
)
796 stack
[i
] = unsuitable_type
;
797 locals
= new type
[max_locals
];
798 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
799 for (int i
= 0; i
< max_locals
; ++i
)
801 locals
[i
] = unsuitable_type
;
802 local_changed
[i
] = false;
808 state (const state
*orig
, int max_stack
, int max_locals
,
809 bool ret_semantics
= false)
811 stack
= new type
[max_stack
];
812 locals
= new type
[max_locals
];
813 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
814 copy (orig
, max_stack
, max_locals
, ret_semantics
);
825 _Jv_Free (local_changed
);
828 void *operator new[] (size_t bytes
)
830 return _Jv_Malloc (bytes
);
833 void operator delete[] (void *mem
)
838 void *operator new (size_t bytes
)
840 return _Jv_Malloc (bytes
);
843 void operator delete (void *mem
)
848 void copy (const state
*copy
, int max_stack
, int max_locals
,
849 bool ret_semantics
= false)
851 stacktop
= copy
->stacktop
;
852 stackdepth
= copy
->stackdepth
;
853 subroutine
= copy
->subroutine
;
854 for (int i
= 0; i
< max_stack
; ++i
)
855 stack
[i
] = copy
->stack
[i
];
856 for (int i
= 0; i
< max_locals
; ++i
)
858 // See push_jump_merge to understand this case.
860 locals
[i
] = type (copy
->local_changed
[i
]
862 : unused_by_subroutine_type
);
864 locals
[i
] = copy
->locals
[i
];
865 local_changed
[i
] = copy
->local_changed
[i
];
867 this_type
= copy
->this_type
;
868 // Don't modify `next'.
871 // Modify this state to reflect entry to an exception handler.
872 void set_exception (type t
, int max_stack
)
877 for (int i
= stacktop
; i
< max_stack
; ++i
)
878 stack
[i
] = unsuitable_type
;
880 // FIXME: subroutine handling?
883 // Merge STATE_OLD into this state. Destructively modifies this
884 // state. Returns true if the new state was in fact changed.
885 // Will throw an exception if the states are not mergeable.
886 bool merge (state
*state_old
, bool ret_semantics
,
889 bool changed
= false;
891 // Special handling for `this'. If one or the other is
892 // uninitialized, then the merge is uninitialized.
893 if (this_type
.isinitialized ())
894 this_type
= state_old
->this_type
;
896 // Merge subroutine states. *THIS and *STATE_OLD must be in the
897 // same subroutine. Also, recursive subroutine calls must be
899 if (subroutine
== state_old
->subroutine
)
903 else if (subroutine
== 0)
905 subroutine
= state_old
->subroutine
;
909 verify_fail ("subroutines merged");
912 if (state_old
->stacktop
!= stacktop
)
913 verify_fail ("stack sizes differ");
914 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
916 if (stack
[i
].merge (state_old
->stack
[i
]))
920 // Merge local variables.
921 for (int i
= 0; i
< max_locals
; ++i
)
923 if (! ret_semantics
|| local_changed
[i
])
925 if (locals
[i
].merge (state_old
->locals
[i
], true))
932 // If we're in a subroutine, we must compute the union of
933 // all the changed local variables.
934 if (state_old
->local_changed
[i
])
941 // Throw an exception if there is an uninitialized object on the
942 // stack or in a local variable. EXCEPTION_SEMANTICS controls
943 // whether we're using backwards-branch or exception-handing
945 void check_no_uninitialized_objects (int max_locals
,
946 bool exception_semantics
= false)
948 if (! exception_semantics
)
950 for (int i
= 0; i
< stacktop
; ++i
)
951 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
952 verify_fail ("uninitialized object on stack");
955 for (int i
= 0; i
< max_locals
; ++i
)
956 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
957 verify_fail ("uninitialized object in local variable");
959 check_this_initialized ();
962 // Ensure that `this' has been initialized.
963 void check_this_initialized ()
965 if (this_type
.isreference () && ! this_type
.isinitialized ())
966 verify_fail ("`this' is uninitialized");
969 // Set type of `this'.
970 void set_this_type (const type
&k
)
975 // Note that a local variable was modified.
976 void note_variable (int index
)
979 local_changed
[index
] = true;
982 // Mark each `new'd object we know of that was allocated at PC as
984 void set_initialized (int pc
, int max_locals
)
986 for (int i
= 0; i
< stacktop
; ++i
)
987 stack
[i
].set_initialized (pc
);
988 for (int i
= 0; i
< max_locals
; ++i
)
989 locals
[i
].set_initialized (pc
);
990 this_type
.set_initialized (pc
);
993 // Return true if this state is the unmerged result of a `ret'.
994 bool is_unmerged_ret_state (int max_locals
) const
996 for (int i
= 0; i
< max_locals
; ++i
)
998 if (locals
[i
].key
== unused_by_subroutine_type
)
1005 void print (const char *leader
, int pc
,
1006 int max_stack
, int max_locals
) const
1008 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
1010 for (i
= 0; i
< stacktop
; ++i
)
1012 for (; i
< max_stack
; ++i
)
1014 debug_print (" [local] ");
1015 for (i
= 0; i
< max_locals
; ++i
)
1017 debug_print (" | %p\n", this);
1020 inline void print (const char *, int, int, int) const
1023 #endif /* VERIFY_DEBUG */
1028 if (current_state
->stacktop
<= 0)
1029 verify_fail ("stack empty", start_PC
);
1030 type r
= current_state
->stack
[--current_state
->stacktop
];
1031 current_state
->stackdepth
-= r
.depth ();
1032 if (current_state
->stackdepth
< 0)
1033 verify_fail ("stack empty", start_PC
);
1039 type r
= pop_raw ();
1041 verify_fail ("narrow pop of wide type", start_PC
);
1047 type r
= pop_raw ();
1049 verify_fail ("wide pop of narrow type", start_PC
);
1053 type
pop_type (type match
)
1056 type t
= pop_raw ();
1057 if (! match
.compatible (t
))
1058 verify_fail ("incompatible type on stack", start_PC
);
1062 // Pop a reference type or a return address.
1063 type
pop_ref_or_return ()
1065 type t
= pop_raw ();
1066 if (! t
.isreference () && t
.key
!= return_address_type
)
1067 verify_fail ("expected reference or return address on stack", start_PC
);
1071 void push_type (type t
)
1073 // If T is a numeric type like short, promote it to int.
1076 int depth
= t
.depth ();
1077 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1078 verify_fail ("stack overflow");
1079 current_state
->stack
[current_state
->stacktop
++] = t
;
1080 current_state
->stackdepth
+= depth
;
1083 void set_variable (int index
, type t
)
1085 // If T is a numeric type like short, promote it to int.
1088 int depth
= t
.depth ();
1089 if (index
> current_method
->max_locals
- depth
)
1090 verify_fail ("invalid local variable");
1091 current_state
->locals
[index
] = t
;
1092 current_state
->note_variable (index
);
1096 current_state
->locals
[index
+ 1] = continuation_type
;
1097 current_state
->note_variable (index
+ 1);
1099 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1101 current_state
->locals
[index
- 1] = unsuitable_type
;
1102 // There's no need to call note_variable here.
1106 type
get_variable (int index
, type t
)
1108 int depth
= t
.depth ();
1109 if (index
> current_method
->max_locals
- depth
)
1110 verify_fail ("invalid local variable", start_PC
);
1111 if (! t
.compatible (current_state
->locals
[index
]))
1112 verify_fail ("incompatible type in local variable", start_PC
);
1115 type
t (continuation_type
);
1116 if (! current_state
->locals
[index
+ 1].compatible (t
))
1117 verify_fail ("invalid local variable", start_PC
);
1119 return current_state
->locals
[index
];
1122 // Make sure ARRAY is an array type and that its elements are
1123 // compatible with type ELEMENT. Returns the actual element type.
1124 type
require_array_type (type array
, type element
)
1126 if (! array
.isarray ())
1127 verify_fail ("array required");
1129 type t
= array
.element_type ();
1130 if (! element
.compatible (t
))
1132 // Special case for byte arrays, which must also be boolean
1135 if (element
.key
== byte_type
)
1137 type
e2 (boolean_type
);
1138 ok
= e2
.compatible (t
);
1141 verify_fail ("incompatible array element type");
1144 // Return T and not ELEMENT, because T might be specialized.
1150 if (PC
>= current_method
->code_length
)
1151 verify_fail ("premature end of bytecode");
1152 return (jint
) bytecode
[PC
++] & 0xff;
1157 jint b1
= get_byte ();
1158 jint b2
= get_byte ();
1159 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1164 jint b1
= get_byte ();
1165 jint b2
= get_byte ();
1166 jshort s
= (b1
<< 8) | b2
;
1172 jint b1
= get_byte ();
1173 jint b2
= get_byte ();
1174 jint b3
= get_byte ();
1175 jint b4
= get_byte ();
1176 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1179 int compute_jump (int offset
)
1181 int npc
= start_PC
+ offset
;
1182 if (npc
< 0 || npc
>= current_method
->code_length
)
1183 verify_fail ("branch out of range", start_PC
);
1187 // Merge the indicated state into the state at the branch target and
1188 // schedule a new PC if there is a change. If RET_SEMANTICS is
1189 // true, then we are merging from a `ret' instruction into the
1190 // instruction after a `jsr'. This is a special case with its own
1191 // modified semantics.
1192 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1194 bool changed
= true;
1195 if (states
[npc
] == NULL
)
1197 // There's a weird situation here. If are examining the
1198 // branch that results from a `ret', and there is not yet a
1199 // state available at the branch target (the instruction just
1200 // after the `jsr'), then we have to construct a special kind
1201 // of state at that point for future merging. This special
1202 // state has the type `unused_by_subroutine_type' in each slot
1203 // which was not modified by the subroutine.
1204 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1205 current_method
->max_locals
, ret_semantics
);
1206 debug_print ("== New state in push_jump_merge\n");
1207 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1208 current_method
->max_locals
);
1212 debug_print ("== Merge states in push_jump_merge\n");
1213 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1214 current_method
->max_locals
);
1215 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1216 current_method
->max_locals
);
1217 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1218 current_method
->max_locals
);
1219 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1220 current_method
->max_locals
);
1223 if (changed
&& states
[npc
]->next
== state::INVALID
)
1225 // The merge changed the state, and the new PC isn't yet on our
1226 // list of PCs to re-verify.
1227 states
[npc
]->next
= next_verify_pc
;
1228 next_verify_pc
= npc
;
1232 void push_jump (int offset
)
1234 int npc
= compute_jump (offset
);
1236 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1237 push_jump_merge (npc
, current_state
);
1240 void push_exception_jump (type t
, int pc
)
1242 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1244 state
s (current_state
, current_method
->max_stack
,
1245 current_method
->max_locals
);
1246 s
.set_exception (t
, current_method
->max_stack
);
1247 push_jump_merge (pc
, &s
);
1252 int *prev_loc
= &next_verify_pc
;
1253 int npc
= next_verify_pc
;
1254 bool skipped
= false;
1256 while (npc
!= state::NO_NEXT
)
1258 // If the next available PC is an unmerged `ret' state, then
1259 // we aren't yet ready to handle it. That's because we would
1260 // need all kind of special cases to do so. So instead we
1261 // defer this jump until after we've processed it via a
1262 // fall-through. This has to happen because the instruction
1263 // before this one must be a `jsr'.
1264 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1266 *prev_loc
= states
[npc
]->next
;
1267 states
[npc
]->next
= state::INVALID
;
1272 prev_loc
= &states
[npc
]->next
;
1273 npc
= states
[npc
]->next
;
1276 // If we've skipped states and there is nothing else, that's a
1279 verify_fail ("pop_jump: can't happen");
1280 return state::NO_NEXT
;
1283 void invalidate_pc ()
1285 PC
= state::NO_NEXT
;
1288 void note_branch_target (int pc
, bool is_jsr_target
= false)
1290 // Don't check `pc <= PC', because we've advanced PC after
1291 // fetching the target and we haven't yet checked the next
1293 if (pc
< PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1294 verify_fail ("branch not to instruction start", start_PC
);
1295 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1298 // Record the jsr which called this instruction.
1299 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1301 info
->next
= jsr_ptrs
[pc
];
1302 jsr_ptrs
[pc
] = info
;
1306 void skip_padding ()
1308 while ((PC
% 4) > 0)
1309 if (get_byte () != 0)
1310 verify_fail ("found nonzero padding byte");
1313 // Return the subroutine to which the instruction at PC belongs.
1314 int get_subroutine (int pc
)
1316 if (states
[pc
] == NULL
)
1318 return states
[pc
]->subroutine
;
1321 // Do the work for a `ret' instruction. INDEX is the index into the
1323 void handle_ret_insn (int index
)
1325 get_variable (index
, return_address_type
);
1327 int csub
= current_state
->subroutine
;
1329 verify_fail ("no subroutine");
1331 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1333 // Temporarily modify the current state so it looks like we're
1334 // in the enclosing context.
1335 current_state
->subroutine
= get_subroutine (subr
->pc
);
1337 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1338 push_jump_merge (subr
->pc
, current_state
, true);
1341 current_state
->subroutine
= csub
;
1345 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1346 // sure this subroutine isn't already on the stack.
1347 void check_nonrecursive_call (int sub
, int dest
)
1352 verify_fail ("recursive subroutine call");
1353 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1354 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1357 void handle_jsr_insn (int offset
)
1359 int npc
= compute_jump (offset
);
1362 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1363 check_nonrecursive_call (current_state
->subroutine
, npc
);
1365 // Temporarily modify the current state so that it looks like we are
1366 // in the subroutine.
1367 push_type (return_address_type
);
1368 int save
= current_state
->subroutine
;
1369 current_state
->subroutine
= npc
;
1371 // Merge into the subroutine.
1372 push_jump_merge (npc
, current_state
);
1374 // Undo our modifications.
1375 current_state
->subroutine
= save
;
1376 pop_type (return_address_type
);
1379 jclass
construct_primitive_array_type (type_val prim
)
1385 k
= JvPrimClass (boolean
);
1388 k
= JvPrimClass (char);
1391 k
= JvPrimClass (float);
1394 k
= JvPrimClass (double);
1397 k
= JvPrimClass (byte
);
1400 k
= JvPrimClass (short);
1403 k
= JvPrimClass (int);
1406 k
= JvPrimClass (long);
1409 verify_fail ("unknown type in construct_primitive_array_type");
1411 k
= _Jv_GetArrayClass (k
, NULL
);
1415 // This pass computes the location of branch targets and also
1416 // instruction starts.
1417 void branch_prepass ()
1419 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1420 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1421 * current_method
->code_length
);
1423 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1429 bool last_was_jsr
= false;
1432 while (PC
< current_method
->code_length
)
1434 // Set `start_PC' early so that error checking can have the
1437 flags
[PC
] |= FLAG_INSN_START
;
1439 // If the previous instruction was a jsr, then the next
1440 // instruction is a branch target -- the branch being the
1441 // corresponding `ret'.
1443 note_branch_target (PC
);
1444 last_was_jsr
= false;
1446 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1450 case op_aconst_null
:
1586 case op_monitorenter
:
1587 case op_monitorexit
:
1595 case op_arraylength
:
1627 case op_invokespecial
:
1628 case op_invokestatic
:
1629 case op_invokevirtual
:
1633 case op_multianewarray
:
1639 last_was_jsr
= true;
1658 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1661 case op_tableswitch
:
1664 note_branch_target (compute_jump (get_int ()));
1665 jint low
= get_int ();
1666 jint hi
= get_int ();
1668 verify_fail ("invalid tableswitch", start_PC
);
1669 for (int i
= low
; i
<= hi
; ++i
)
1670 note_branch_target (compute_jump (get_int ()));
1674 case op_lookupswitch
:
1677 note_branch_target (compute_jump (get_int ()));
1678 int npairs
= get_int ();
1680 verify_fail ("too few pairs in lookupswitch", start_PC
);
1681 while (npairs
-- > 0)
1684 note_branch_target (compute_jump (get_int ()));
1689 case op_invokeinterface
:
1697 opcode
= (java_opcode
) get_byte ();
1699 if (opcode
== op_iinc
)
1705 last_was_jsr
= true;
1708 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1712 verify_fail ("unrecognized instruction in branch_prepass",
1716 // See if any previous branch tried to branch to the middle of
1717 // this instruction.
1718 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1720 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1721 verify_fail ("branch to middle of instruction", pc
);
1725 // Verify exception handlers.
1726 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1728 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1729 verify_fail ("exception handler not at instruction start",
1730 exception
[i
].handler_pc
);
1731 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1732 verify_fail ("exception start not at instruction start",
1733 exception
[i
].start_pc
);
1734 if (exception
[i
].end_pc
!= current_method
->code_length
1735 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1736 verify_fail ("exception end not at instruction start",
1737 exception
[i
].end_pc
);
1739 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1743 void check_pool_index (int index
)
1745 if (index
< 0 || index
>= current_class
->constants
.size
)
1746 verify_fail ("constant pool index out of range", start_PC
);
1749 type
check_class_constant (int index
)
1751 check_pool_index (index
);
1752 _Jv_Constants
*pool
= ¤t_class
->constants
;
1753 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1754 return type (pool
->data
[index
].clazz
);
1755 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1756 return type (pool
->data
[index
].utf8
);
1757 verify_fail ("expected class constant", start_PC
);
1760 type
check_constant (int index
)
1762 check_pool_index (index
);
1763 _Jv_Constants
*pool
= ¤t_class
->constants
;
1764 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1765 || pool
->tags
[index
] == JV_CONSTANT_String
)
1766 return type (&java::lang::String::class$
);
1767 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1768 return type (int_type
);
1769 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1770 return type (float_type
);
1771 verify_fail ("String, int, or float constant expected", start_PC
);
1774 type
check_wide_constant (int index
)
1776 check_pool_index (index
);
1777 _Jv_Constants
*pool
= ¤t_class
->constants
;
1778 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1779 return type (long_type
);
1780 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1781 return type (double_type
);
1782 verify_fail ("long or double constant expected", start_PC
);
1785 // Helper for both field and method. These are laid out the same in
1786 // the constant pool.
1787 type
handle_field_or_method (int index
, int expected
,
1788 _Jv_Utf8Const
**name
,
1789 _Jv_Utf8Const
**fmtype
)
1791 check_pool_index (index
);
1792 _Jv_Constants
*pool
= ¤t_class
->constants
;
1793 if (pool
->tags
[index
] != expected
)
1794 verify_fail ("didn't see expected constant", start_PC
);
1795 // Once we know we have a Fieldref or Methodref we assume that it
1796 // is correctly laid out in the constant pool. I think the code
1797 // in defineclass.cc guarantees this.
1798 _Jv_ushort class_index
, name_and_type_index
;
1799 _Jv_loadIndexes (&pool
->data
[index
],
1801 name_and_type_index
);
1802 _Jv_ushort name_index
, desc_index
;
1803 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1804 name_index
, desc_index
);
1806 *name
= pool
->data
[name_index
].utf8
;
1807 *fmtype
= pool
->data
[desc_index
].utf8
;
1809 return check_class_constant (class_index
);
1812 // Return field's type, compute class' type if requested.
1813 type
check_field_constant (int index
, type
*class_type
= NULL
)
1815 _Jv_Utf8Const
*name
, *field_type
;
1816 type ct
= handle_field_or_method (index
,
1817 JV_CONSTANT_Fieldref
,
1818 &name
, &field_type
);
1821 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1822 return type (field_type
);
1823 return get_type_val_for_signature (field_type
->data
[0]);
1826 type
check_method_constant (int index
, bool is_interface
,
1827 _Jv_Utf8Const
**method_name
,
1828 _Jv_Utf8Const
**method_signature
)
1830 return handle_field_or_method (index
,
1832 ? JV_CONSTANT_InterfaceMethodref
1833 : JV_CONSTANT_Methodref
),
1834 method_name
, method_signature
);
1837 type
get_one_type (char *&p
)
1855 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1859 // Casting to jchar here is ok since we are looking at an ASCII
1861 type_val rt
= get_type_val_for_signature (jchar (v
));
1863 if (arraycount
== 0)
1865 // Callers of this function eventually push their arguments on
1866 // the stack. So, promote them here.
1867 return type (rt
).promote ();
1870 jclass k
= construct_primitive_array_type (rt
);
1871 while (--arraycount
> 0)
1872 k
= _Jv_GetArrayClass (k
, NULL
);
1876 void compute_argument_types (_Jv_Utf8Const
*signature
,
1879 char *p
= signature
->data
;
1885 types
[i
++] = get_one_type (p
);
1888 type
compute_return_type (_Jv_Utf8Const
*signature
)
1890 char *p
= signature
->data
;
1894 return get_one_type (p
);
1897 void check_return_type (type onstack
)
1899 type rt
= compute_return_type (current_method
->self
->signature
);
1900 if (! rt
.compatible (onstack
))
1901 verify_fail ("incompatible return type", start_PC
);
1904 // Initialize the stack for the new method. Returns true if this
1905 // method is an instance initializer.
1906 bool initialize_stack ()
1909 bool is_init
= false;
1911 using namespace java::lang::reflect
;
1912 if (! Modifier::isStatic (current_method
->self
->accflags
))
1914 type
kurr (current_class
);
1915 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
1917 kurr
.set_uninitialized (type::SELF
);
1920 set_variable (0, kurr
);
1921 current_state
->set_this_type (kurr
);
1925 // We have to handle wide arguments specially here.
1926 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
1927 type arg_types
[arg_count
];
1928 compute_argument_types (current_method
->self
->signature
, arg_types
);
1929 for (int i
= 0; i
< arg_count
; ++i
)
1931 set_variable (var
, arg_types
[i
]);
1933 if (arg_types
[i
].iswide ())
1940 void verify_instructions_0 ()
1942 current_state
= new state (current_method
->max_stack
,
1943 current_method
->max_locals
);
1948 // True if we are verifying an instance initializer.
1949 bool this_is_init
= initialize_stack ();
1951 states
= (state
**) _Jv_Malloc (sizeof (state
*)
1952 * current_method
->code_length
);
1953 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1956 next_verify_pc
= state::NO_NEXT
;
1960 // If the PC was invalidated, get a new one from the work list.
1961 if (PC
== state::NO_NEXT
)
1964 if (PC
== state::INVALID
)
1965 verify_fail ("can't happen: saw state::INVALID");
1966 if (PC
== state::NO_NEXT
)
1968 // Set up the current state.
1969 current_state
->copy (states
[PC
], current_method
->max_stack
,
1970 current_method
->max_locals
);
1974 // Control can't fall off the end of the bytecode. We
1975 // only need to check this in the fall-through case,
1976 // because branch bounds are checked when they are
1978 if (PC
>= current_method
->code_length
)
1979 verify_fail ("fell off end");
1981 // We only have to do this checking in the situation where
1982 // control flow falls through from the previous
1983 // instruction. Otherwise merging is done at the time we
1985 if (states
[PC
] != NULL
)
1987 // We've already visited this instruction. So merge
1988 // the states together. If this yields no change then
1989 // we don't have to re-verify. However, if the new
1990 // state is an the result of an unmerged `ret', we
1991 // must continue through it.
1992 debug_print ("== Fall through merge\n");
1993 states
[PC
]->print ("Old", PC
, current_method
->max_stack
,
1994 current_method
->max_locals
);
1995 current_state
->print ("Cur", PC
, current_method
->max_stack
,
1996 current_method
->max_locals
);
1997 if (! current_state
->merge (states
[PC
], false,
1998 current_method
->max_locals
)
1999 && ! states
[PC
]->is_unmerged_ret_state (current_method
->max_locals
))
2001 debug_print ("== Fall through optimization\n");
2005 // Save a copy of it for later.
2006 states
[PC
]->copy (current_state
, current_method
->max_stack
,
2007 current_method
->max_locals
);
2008 current_state
->print ("New", PC
, current_method
->max_stack
,
2009 current_method
->max_locals
);
2013 // We only have to keep saved state at branch targets. If
2014 // we're at a branch target and the state here hasn't been set
2015 // yet, we set it now.
2016 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
2018 states
[PC
] = new state (current_state
, current_method
->max_stack
,
2019 current_method
->max_locals
);
2022 // Set this before handling exceptions so that debug output is
2026 // Update states for all active exception handlers. Ordinarily
2027 // there are not many exception handlers. So we simply run
2028 // through them all.
2029 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
2031 if (PC
>= exception
[i
].start_pc
&& PC
< exception
[i
].end_pc
)
2033 type
handler (&java::lang::Throwable::class$
);
2034 if (exception
[i
].handler_type
!= 0)
2035 handler
= check_class_constant (exception
[i
].handler_type
);
2036 push_exception_jump (handler
, exception
[i
].handler_pc
);
2040 current_state
->print (" ", PC
, current_method
->max_stack
,
2041 current_method
->max_locals
);
2042 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
2048 case op_aconst_null
:
2049 push_type (null_type
);
2059 push_type (int_type
);
2064 push_type (long_type
);
2070 push_type (float_type
);
2075 push_type (double_type
);
2080 push_type (int_type
);
2085 push_type (int_type
);
2089 push_type (check_constant (get_byte ()));
2092 push_type (check_constant (get_ushort ()));
2095 push_type (check_wide_constant (get_ushort ()));
2099 push_type (get_variable (get_byte (), int_type
));
2102 push_type (get_variable (get_byte (), long_type
));
2105 push_type (get_variable (get_byte (), float_type
));
2108 push_type (get_variable (get_byte (), double_type
));
2111 push_type (get_variable (get_byte (), reference_type
));
2118 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2124 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2130 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2136 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2142 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2145 pop_type (int_type
);
2146 push_type (require_array_type (pop_type (reference_type
),
2150 pop_type (int_type
);
2151 push_type (require_array_type (pop_type (reference_type
),
2155 pop_type (int_type
);
2156 push_type (require_array_type (pop_type (reference_type
),
2160 pop_type (int_type
);
2161 push_type (require_array_type (pop_type (reference_type
),
2165 pop_type (int_type
);
2166 push_type (require_array_type (pop_type (reference_type
),
2170 pop_type (int_type
);
2171 require_array_type (pop_type (reference_type
), byte_type
);
2172 push_type (int_type
);
2175 pop_type (int_type
);
2176 require_array_type (pop_type (reference_type
), char_type
);
2177 push_type (int_type
);
2180 pop_type (int_type
);
2181 require_array_type (pop_type (reference_type
), short_type
);
2182 push_type (int_type
);
2185 set_variable (get_byte (), pop_type (int_type
));
2188 set_variable (get_byte (), pop_type (long_type
));
2191 set_variable (get_byte (), pop_type (float_type
));
2194 set_variable (get_byte (), pop_type (double_type
));
2197 set_variable (get_byte (), pop_ref_or_return ());
2203 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2209 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2215 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2221 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2227 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2230 pop_type (int_type
);
2231 pop_type (int_type
);
2232 require_array_type (pop_type (reference_type
), int_type
);
2235 pop_type (long_type
);
2236 pop_type (int_type
);
2237 require_array_type (pop_type (reference_type
), long_type
);
2240 pop_type (float_type
);
2241 pop_type (int_type
);
2242 require_array_type (pop_type (reference_type
), float_type
);
2245 pop_type (double_type
);
2246 pop_type (int_type
);
2247 require_array_type (pop_type (reference_type
), double_type
);
2250 pop_type (reference_type
);
2251 pop_type (int_type
);
2252 require_array_type (pop_type (reference_type
), reference_type
);
2255 pop_type (int_type
);
2256 pop_type (int_type
);
2257 require_array_type (pop_type (reference_type
), byte_type
);
2260 pop_type (int_type
);
2261 pop_type (int_type
);
2262 require_array_type (pop_type (reference_type
), char_type
);
2265 pop_type (int_type
);
2266 pop_type (int_type
);
2267 require_array_type (pop_type (reference_type
), short_type
);
2294 type t2
= pop_raw ();
2309 type t
= pop_raw ();
2322 type t1
= pop_raw ();
2340 type t1
= pop_raw ();
2343 type t2
= pop_raw ();
2361 type t3
= pop_raw ();
2399 pop_type (int_type
);
2400 push_type (pop_type (int_type
));
2410 pop_type (long_type
);
2411 push_type (pop_type (long_type
));
2416 pop_type (int_type
);
2417 push_type (pop_type (long_type
));
2424 pop_type (float_type
);
2425 push_type (pop_type (float_type
));
2432 pop_type (double_type
);
2433 push_type (pop_type (double_type
));
2439 push_type (pop_type (int_type
));
2442 push_type (pop_type (long_type
));
2445 push_type (pop_type (float_type
));
2448 push_type (pop_type (double_type
));
2451 get_variable (get_byte (), int_type
);
2455 pop_type (int_type
);
2456 push_type (long_type
);
2459 pop_type (int_type
);
2460 push_type (float_type
);
2463 pop_type (int_type
);
2464 push_type (double_type
);
2467 pop_type (long_type
);
2468 push_type (int_type
);
2471 pop_type (long_type
);
2472 push_type (float_type
);
2475 pop_type (long_type
);
2476 push_type (double_type
);
2479 pop_type (float_type
);
2480 push_type (int_type
);
2483 pop_type (float_type
);
2484 push_type (long_type
);
2487 pop_type (float_type
);
2488 push_type (double_type
);
2491 pop_type (double_type
);
2492 push_type (int_type
);
2495 pop_type (double_type
);
2496 push_type (long_type
);
2499 pop_type (double_type
);
2500 push_type (float_type
);
2503 pop_type (long_type
);
2504 pop_type (long_type
);
2505 push_type (int_type
);
2509 pop_type (float_type
);
2510 pop_type (float_type
);
2511 push_type (int_type
);
2515 pop_type (double_type
);
2516 pop_type (double_type
);
2517 push_type (int_type
);
2525 pop_type (int_type
);
2526 push_jump (get_short ());
2534 pop_type (int_type
);
2535 pop_type (int_type
);
2536 push_jump (get_short ());
2540 pop_type (reference_type
);
2541 pop_type (reference_type
);
2542 push_jump (get_short ());
2545 push_jump (get_short ());
2549 handle_jsr_insn (get_short ());
2552 handle_ret_insn (get_byte ());
2554 case op_tableswitch
:
2556 pop_type (int_type
);
2558 push_jump (get_int ());
2559 jint low
= get_int ();
2560 jint high
= get_int ();
2561 // Already checked LOW -vs- HIGH.
2562 for (int i
= low
; i
<= high
; ++i
)
2563 push_jump (get_int ());
2568 case op_lookupswitch
:
2570 pop_type (int_type
);
2572 push_jump (get_int ());
2573 jint npairs
= get_int ();
2574 // Already checked NPAIRS >= 0.
2576 for (int i
= 0; i
< npairs
; ++i
)
2578 jint key
= get_int ();
2579 if (i
> 0 && key
<= lastkey
)
2580 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2582 push_jump (get_int ());
2588 check_return_type (pop_type (int_type
));
2592 check_return_type (pop_type (long_type
));
2596 check_return_type (pop_type (float_type
));
2600 check_return_type (pop_type (double_type
));
2604 check_return_type (pop_type (reference_type
));
2608 // We only need to check this when the return type is
2609 // void, because all instance initializers return void.
2611 current_state
->check_this_initialized ();
2612 check_return_type (void_type
);
2616 push_type (check_field_constant (get_ushort ()));
2619 pop_type (check_field_constant (get_ushort ()));
2624 type field
= check_field_constant (get_ushort (), &klass
);
2632 type field
= check_field_constant (get_ushort (), &klass
);
2635 // We have an obscure special case here: we can use
2636 // `putfield' on a field declared in this class, even if
2637 // `this' has not yet been initialized.
2638 if (! current_state
->this_type
.isinitialized ()
2639 && current_state
->this_type
.pc
== type::SELF
)
2640 klass
.set_uninitialized (type::SELF
);
2645 case op_invokevirtual
:
2646 case op_invokespecial
:
2647 case op_invokestatic
:
2648 case op_invokeinterface
:
2650 _Jv_Utf8Const
*method_name
, *method_signature
;
2652 = check_method_constant (get_ushort (),
2653 opcode
== op_invokeinterface
,
2656 int arg_count
= _Jv_count_arguments (method_signature
);
2657 if (opcode
== op_invokeinterface
)
2659 int nargs
= get_byte ();
2661 verify_fail ("too few arguments to invokeinterface",
2663 if (get_byte () != 0)
2664 verify_fail ("invokeinterface dummy byte is wrong",
2666 if (nargs
- 1 != arg_count
)
2667 verify_fail ("wrong argument count for invokeinterface",
2671 bool is_init
= false;
2672 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2675 if (opcode
!= op_invokespecial
)
2676 verify_fail ("can't invoke <init>", start_PC
);
2678 else if (method_name
->data
[0] == '<')
2679 verify_fail ("can't invoke method starting with `<'",
2682 // Pop arguments and check types.
2683 type arg_types
[arg_count
];
2684 compute_argument_types (method_signature
, arg_types
);
2685 for (int i
= arg_count
- 1; i
>= 0; --i
)
2686 pop_type (arg_types
[i
]);
2688 if (opcode
!= op_invokestatic
)
2690 type t
= class_type
;
2693 // In this case the PC doesn't matter.
2694 t
.set_uninitialized (type::UNINIT
);
2698 current_state
->set_initialized (t
.get_pc (),
2699 current_method
->max_locals
);
2702 type rt
= compute_return_type (method_signature
);
2710 type t
= check_class_constant (get_ushort ());
2711 if (t
.isarray () || t
.isinterface () || t
.isabstract ())
2712 verify_fail ("type is array, interface, or abstract",
2714 t
.set_uninitialized (start_PC
);
2721 int atype
= get_byte ();
2722 // We intentionally have chosen constants to make this
2724 if (atype
< boolean_type
|| atype
> long_type
)
2725 verify_fail ("type not primitive", start_PC
);
2726 pop_type (int_type
);
2727 push_type (construct_primitive_array_type (type_val (atype
)));
2731 pop_type (int_type
);
2732 push_type (check_class_constant (get_ushort ()).to_array ());
2734 case op_arraylength
:
2736 type t
= pop_type (reference_type
);
2738 verify_fail ("array type expected", start_PC
);
2739 push_type (int_type
);
2743 pop_type (type (&java::lang::Throwable::class$
));
2747 pop_type (reference_type
);
2748 push_type (check_class_constant (get_ushort ()));
2751 pop_type (reference_type
);
2752 check_class_constant (get_ushort ());
2753 push_type (int_type
);
2755 case op_monitorenter
:
2756 pop_type (reference_type
);
2758 case op_monitorexit
:
2759 pop_type (reference_type
);
2763 switch (get_byte ())
2766 push_type (get_variable (get_ushort (), int_type
));
2769 push_type (get_variable (get_ushort (), long_type
));
2772 push_type (get_variable (get_ushort (), float_type
));
2775 push_type (get_variable (get_ushort (), double_type
));
2778 push_type (get_variable (get_ushort (), reference_type
));
2781 set_variable (get_ushort (), pop_type (int_type
));
2784 set_variable (get_ushort (), pop_type (long_type
));
2787 set_variable (get_ushort (), pop_type (float_type
));
2790 set_variable (get_ushort (), pop_type (double_type
));
2793 set_variable (get_ushort (), pop_type (reference_type
));
2796 handle_ret_insn (get_short ());
2799 get_variable (get_ushort (), int_type
);
2803 verify_fail ("unrecognized wide instruction", start_PC
);
2807 case op_multianewarray
:
2809 type atype
= check_class_constant (get_ushort ());
2810 int dim
= get_byte ();
2812 verify_fail ("too few dimensions to multianewarray", start_PC
);
2813 atype
.verify_dimensions (dim
);
2814 for (int i
= 0; i
< dim
; ++i
)
2815 pop_type (int_type
);
2821 pop_type (reference_type
);
2822 push_jump (get_short ());
2825 push_jump (get_int ());
2829 handle_jsr_insn (get_int ());
2833 // Unrecognized opcode.
2834 verify_fail ("unrecognized instruction in verify_instructions_0",
2842 void verify_instructions ()
2845 verify_instructions_0 ();
2848 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
2850 // We just print the text as utf-8. This is just for debugging
2852 debug_print ("--------------------------------\n");
2853 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
2856 bytecode
= m
->bytecode ();
2857 exception
= m
->exceptions ();
2858 current_class
= m
->defining_class
;
2866 ~_Jv_BytecodeVerifier ()
2873 _Jv_Free (jsr_ptrs
);
2874 while (utf8_list
!= NULL
)
2876 linked_utf8
*n
= utf8_list
->next
;
2877 _Jv_Free (utf8_list
->val
);
2878 _Jv_Free (utf8_list
);
2885 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
2887 _Jv_BytecodeVerifier
v (meth
);
2888 v
.verify_instructions ();
2891 // FIXME: add more info, like PC, when required.
2893 verify_fail (char *s
, jint pc
)
2895 using namespace java::lang
;
2896 StringBuffer
*buf
= new StringBuffer ();
2898 buf
->append (JvNewStringLatin1 ("verification failed"));
2901 buf
->append (JvNewStringLatin1 (" at PC "));
2904 buf
->append (JvNewStringLatin1 (": "));
2905 buf
->append (JvNewStringLatin1 (s
));
2906 throw new java::lang::VerifyError (buf
->toString ());
2909 #endif /* INTERPRETER */