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;
67 static const int FLAG_JSR_TARGET
= 4;
76 // The PC corresponding to the start of the current instruction.
79 // The current state of the stack, locals, etc.
82 // We store the state at branch targets, for merging. This holds
86 // We keep a linked list of all the PCs which we must reverify.
87 // The link is done using the PC values. This is the head of the
91 // We keep some flags for each instruction. The values are the
92 // FLAG_* constants defined above.
95 // We need to keep track of which instructions can call a given
96 // subroutine. FIXME: this is inefficient. We keep a linked list
97 // of all calling `jsr's at at each jsr target.
100 // The current top of the stack, in terms of slots.
102 // The current depth of the stack. This will be larger than
103 // STACKTOP when wide types are on the stack.
106 // The bytecode itself.
107 unsigned char *bytecode
;
109 _Jv_InterpException
*exception
;
112 jclass current_class
;
114 _Jv_InterpMethod
*current_method
;
116 // A linked list of utf8 objects we allocate. This is really ugly,
117 // but without this our utf8 objects would be collected.
118 linked_utf8
*utf8_list
;
126 _Jv_Utf8Const
*make_utf8_const (char *s
, int len
)
128 _Jv_Utf8Const
*val
= _Jv_makeUtf8Const (s
, len
);
129 _Jv_Utf8Const
*r
= (_Jv_Utf8Const
*) _Jv_Malloc (sizeof (_Jv_Utf8Const
)
132 r
->length
= val
->length
;
134 memcpy (r
->data
, val
->data
, val
->length
+ 1);
136 linked_utf8
*lu
= (linked_utf8
*) _Jv_Malloc (sizeof (linked_utf8
));
138 lu
->next
= utf8_list
;
144 // This enum holds a list of tags for all the different types we
145 // need to handle. Reference types are treated specially by the
151 // The values for primitive types are chosen to correspond to values
152 // specified to newarray.
162 // Used when overwriting second word of a double or long in the
163 // local variables. Also used after merging local variable states
164 // to indicate an unusable value.
169 // There is an obscure special case which requires us to note when
170 // a local variable has not been used by a subroutine. See
171 // push_jump_merge for more information.
172 unused_by_subroutine_type
,
174 // Everything after `reference_type' must be a reference type.
177 unresolved_reference_type
,
178 uninitialized_reference_type
,
179 uninitialized_unresolved_reference_type
182 // Return the type_val corresponding to a primitive signature
183 // character. For instance `I' returns `int.class'.
184 static type_val
get_type_val_for_signature (jchar sig
)
217 verify_fail ("invalid signature");
222 // Return the type_val corresponding to a primitive class.
223 static type_val
get_type_val_for_signature (jclass k
)
225 return get_type_val_for_signature ((jchar
) k
->method_count
);
228 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
229 // TARGET haven't been prepared.
230 static bool is_assignable_from_slow (jclass target
, jclass source
)
232 // This will terminate when SOURCE==Object.
235 if (source
== target
)
238 if (target
->isPrimitive () || source
->isPrimitive ())
241 // _Jv_IsAssignableFrom can handle a target which is an
242 // interface even if it hasn't been prepared.
243 if ((target
->state
> JV_STATE_LINKED
|| target
->isInterface ())
244 && source
->state
> JV_STATE_LINKED
)
245 return _Jv_IsAssignableFrom (target
, source
);
247 if (target
->isArray ())
249 if (! source
->isArray ())
251 target
= target
->getComponentType ();
252 source
= source
->getComponentType ();
254 else if (target
->isInterface ())
256 for (int i
= 0; i
< source
->interface_count
; ++i
)
258 // We use a recursive call because we also need to
259 // check superinterfaces.
260 if (is_assignable_from_slow (target
, source
->interfaces
[i
]))
265 else if (target
== &java::lang::Object::class$
)
267 else if (source
->isInterface ()
268 || source
== &java::lang::Object::class$
)
271 source
= source
->getSuperclass ();
275 // This is used to keep track of which `jsr's correspond to a given
279 // PC of the instruction just after the jsr.
285 // The `type' class is used to represent a single type in the
291 // Some associated data.
294 // For a resolved reference type, this is a pointer to the class.
296 // For other reference types, this it the name of the class.
299 // This is used when constructing a new object. It is the PC of the
300 // `new' instruction which created the object. We use the special
301 // value -2 to mean that this is uninitialized, and the special
302 // value -1 for the case where the current method is itself the
306 static const int UNINIT
= -2;
307 static const int SELF
= -1;
309 // Basic constructor.
312 key
= unsuitable_type
;
317 // Make a new instance given the type tag. We assume a generic
318 // `reference_type' means Object.
323 if (key
== reference_type
)
324 data
.klass
= &java::lang::Object::class$
;
328 // Make a new instance given a class.
331 key
= reference_type
;
336 // Make a new instance given the name of a class.
337 type (_Jv_Utf8Const
*n
)
339 key
= unresolved_reference_type
;
352 // These operators are required because libgcj can't link in
354 void *operator new[] (size_t bytes
)
356 return _Jv_Malloc (bytes
);
359 void operator delete[] (void *mem
)
364 type
& operator= (type_val k
)
372 type
& operator= (const type
& t
)
380 // Promote a numeric type.
383 if (key
== boolean_type
|| key
== char_type
384 || key
== byte_type
|| key
== short_type
)
389 // If *THIS is an unresolved reference type, resolve it.
392 if (key
!= unresolved_reference_type
393 && key
!= uninitialized_unresolved_reference_type
)
396 // FIXME: class loader
397 using namespace java::lang
;
398 // We might see either kind of name. Sigh.
399 if (data
.name
->data
[0] == 'L'
400 && data
.name
->data
[data
.name
->length
- 1] == ';')
401 data
.klass
= _Jv_FindClassFromSignature (data
.name
->data
, NULL
);
403 data
.klass
= Class::forName (_Jv_NewStringUtf8Const (data
.name
),
405 key
= (key
== unresolved_reference_type
407 : uninitialized_reference_type
);
410 // Mark this type as the uninitialized result of `new'.
411 void set_uninitialized (int npc
)
413 if (key
== reference_type
)
414 key
= uninitialized_reference_type
;
415 else if (key
== unresolved_reference_type
)
416 key
= uninitialized_unresolved_reference_type
;
418 verify_fail ("internal error in type::uninitialized");
422 // Mark this type as now initialized.
423 void set_initialized (int npc
)
425 if (npc
!= UNINIT
&& pc
== npc
426 && (key
== uninitialized_reference_type
427 || key
== uninitialized_unresolved_reference_type
))
429 key
= (key
== uninitialized_reference_type
431 : unresolved_reference_type
);
437 // Return true if an object of type K can be assigned to a variable
438 // of type *THIS. Handle various special cases too. Might modify
439 // *THIS or K. Note however that this does not perform numeric
441 bool compatible (type
&k
)
443 // Any type is compatible with the unsuitable type.
444 if (key
== unsuitable_type
)
447 if (key
< reference_type
|| k
.key
< reference_type
)
450 // The `null' type is convertible to any reference type.
451 // FIXME: is this correct for THIS?
452 if (key
== null_type
|| k
.key
== null_type
)
455 // Any reference type is convertible to Object. This is a special
456 // case so we don't need to unnecessarily resolve a class.
457 if (key
== reference_type
458 && data
.klass
== &java::lang::Object::class$
)
461 // An initialized type and an uninitialized type are not
463 if (isinitialized () != k
.isinitialized ())
466 // Two uninitialized objects are compatible if either:
467 // * The PCs are identical, or
468 // * One PC is UNINIT.
469 if (! isinitialized ())
471 if (pc
!= k
.pc
&& pc
!= UNINIT
&& k
.pc
!= UNINIT
)
475 // Two unresolved types are equal if their names are the same.
478 && _Jv_equalUtf8Consts (data
.name
, k
.data
.name
))
481 // We must resolve both types and check assignability.
484 return is_assignable_from_slow (data
.klass
, k
.data
.klass
);
489 return key
== void_type
;
494 return key
== long_type
|| key
== double_type
;
497 // Return number of stack or local variable slots taken by this
501 return iswide () ? 2 : 1;
504 bool isarray () const
506 // We treat null_type as not an array. This is ok based on the
507 // current uses of this method.
508 if (key
== reference_type
)
509 return data
.klass
->isArray ();
510 else if (key
== unresolved_reference_type
)
511 return data
.name
->data
[0] == '[';
518 if (key
!= reference_type
)
520 return data
.klass
->isInterface ();
526 if (key
!= reference_type
)
528 using namespace java::lang::reflect
;
529 return Modifier::isAbstract (data
.klass
->getModifiers ());
532 // Return the element type of an array.
535 // FIXME: maybe should do string manipulation here.
537 if (key
!= reference_type
)
538 verify_fail ("programmer error in type::element_type()");
540 jclass k
= data
.klass
->getComponentType ();
541 if (k
->isPrimitive ())
542 return type (get_type_val_for_signature (k
));
546 // Return the array type corresponding to an initialized
547 // reference. We could expand this to work for other kinds of
548 // types, but currently we don't need to.
551 // Resolving isn't ideal, because it might force us to load
552 // another class, but it's easy. FIXME?
553 if (key
== unresolved_reference_type
)
556 if (key
== reference_type
)
557 return type (_Jv_GetArrayClass (data
.klass
,
558 data
.klass
->getClassLoader ()));
560 verify_fail ("internal error in type::to_array()");
563 bool isreference () const
565 return key
>= reference_type
;
573 bool isinitialized () const
575 return (key
== reference_type
577 || key
== unresolved_reference_type
);
580 bool isresolved () const
582 return (key
== reference_type
584 || key
== uninitialized_reference_type
);
587 void verify_dimensions (int ndims
)
589 // The way this is written, we don't need to check isarray().
590 if (key
== reference_type
)
592 jclass k
= data
.klass
;
593 while (k
->isArray () && ndims
> 0)
595 k
= k
->getComponentType ();
601 // We know KEY == unresolved_reference_type.
602 char *p
= data
.name
->data
;
603 while (*p
++ == '[' && ndims
-- > 0)
608 verify_fail ("array type has fewer dimensions than required");
611 // Merge OLD_TYPE into this. On error throw exception.
612 bool merge (type
& old_type
, bool local_semantics
= false)
614 bool changed
= false;
615 bool refo
= old_type
.isreference ();
616 bool refn
= isreference ();
619 if (old_type
.key
== null_type
)
621 else if (key
== null_type
)
626 else if (isinitialized () != old_type
.isinitialized ())
627 verify_fail ("merging initialized and uninitialized types");
630 if (! isinitialized ())
634 else if (old_type
.pc
== UNINIT
)
636 else if (pc
!= old_type
.pc
)
637 verify_fail ("merging different uninitialized types");
641 && ! old_type
.isresolved ()
642 && _Jv_equalUtf8Consts (data
.name
, old_type
.data
.name
))
644 // Types are identical.
651 jclass k
= data
.klass
;
652 jclass oldk
= old_type
.data
.klass
;
655 while (k
->isArray () && oldk
->isArray ())
658 k
= k
->getComponentType ();
659 oldk
= oldk
->getComponentType ();
662 // This loop will end when we hit Object.
665 if (is_assignable_from_slow (k
, oldk
))
667 k
= k
->getSuperclass ();
673 while (arraycount
> 0)
675 // FIXME: Class loader.
676 k
= _Jv_GetArrayClass (k
, NULL
);
684 else if (refo
|| refn
|| key
!= old_type
.key
)
688 // If we're merging into an "unused" slot, then we
689 // simply accept whatever we're merging from.
690 if (key
== unused_by_subroutine_type
)
695 else if (old_type
.key
== unused_by_subroutine_type
)
699 // If we already have an `unsuitable' type, then we
700 // don't need to change again.
701 else if (key
!= unsuitable_type
)
703 key
= unsuitable_type
;
708 verify_fail ("unmergeable type");
714 void print (void) const
719 case boolean_type
: c
= 'Z'; break;
720 case byte_type
: c
= 'B'; break;
721 case char_type
: c
= 'C'; break;
722 case short_type
: c
= 'S'; break;
723 case int_type
: c
= 'I'; break;
724 case long_type
: c
= 'J'; break;
725 case float_type
: c
= 'F'; break;
726 case double_type
: c
= 'D'; break;
727 case void_type
: c
= 'V'; break;
728 case unsuitable_type
: c
= '-'; break;
729 case return_address_type
: c
= 'r'; break;
730 case continuation_type
: c
= '+'; break;
731 case unused_by_subroutine_type
: c
= '_'; break;
732 case reference_type
: c
= 'L'; break;
733 case null_type
: c
= '@'; break;
734 case unresolved_reference_type
: c
= 'l'; break;
735 case uninitialized_reference_type
: c
= 'U'; break;
736 case uninitialized_unresolved_reference_type
: c
= 'u'; break;
738 debug_print ("%c", c
);
740 #endif /* VERIFY_DEBUG */
743 // This class holds all the state information we need for a given
747 // Current top of stack.
749 // Current stack depth. This is like the top of stack but it
750 // includes wide variable information.
754 // The local variables.
756 // This is used in subroutines to keep track of which local
757 // variables have been accessed.
759 // If not 0, then we are in a subroutine. The value is the PC of
760 // the subroutine's entry point. We can use 0 as an exceptional
761 // value because PC=0 can never be a subroutine.
763 // This is used to keep a linked list of all the states which
764 // require re-verification. We use the PC to keep track.
767 // INVALID marks a state which is not on the linked list of states
768 // requiring reverification.
769 static const int INVALID
= -1;
770 // NO_NEXT marks the state at the end of the reverification list.
771 static const int NO_NEXT
= -2;
777 local_changed
= NULL
;
780 state (int max_stack
, int max_locals
)
784 stack
= new type
[max_stack
];
785 for (int i
= 0; i
< max_stack
; ++i
)
786 stack
[i
] = unsuitable_type
;
787 locals
= new type
[max_locals
];
788 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
789 for (int i
= 0; i
< max_locals
; ++i
)
791 locals
[i
] = unsuitable_type
;
792 local_changed
[i
] = false;
798 state (const state
*orig
, int max_stack
, int max_locals
,
799 bool ret_semantics
= false)
801 stack
= new type
[max_stack
];
802 locals
= new type
[max_locals
];
803 local_changed
= (bool *) _Jv_Malloc (sizeof (bool) * max_locals
);
804 copy (orig
, max_stack
, max_locals
, ret_semantics
);
815 _Jv_Free (local_changed
);
818 void *operator new[] (size_t bytes
)
820 return _Jv_Malloc (bytes
);
823 void operator delete[] (void *mem
)
828 void *operator new (size_t bytes
)
830 return _Jv_Malloc (bytes
);
833 void operator delete (void *mem
)
838 void copy (const state
*copy
, int max_stack
, int max_locals
,
839 bool ret_semantics
= false)
841 stacktop
= copy
->stacktop
;
842 stackdepth
= copy
->stackdepth
;
843 subroutine
= copy
->subroutine
;
844 for (int i
= 0; i
< max_stack
; ++i
)
845 stack
[i
] = copy
->stack
[i
];
846 for (int i
= 0; i
< max_locals
; ++i
)
848 // See push_jump_merge to understand this case.
850 locals
[i
] = type (copy
->local_changed
[i
]
852 : unused_by_subroutine_type
);
854 locals
[i
] = copy
->locals
[i
];
855 local_changed
[i
] = copy
->local_changed
[i
];
857 // Don't modify `next'.
860 // Modify this state to reflect entry to an exception handler.
861 void set_exception (type t
, int max_stack
)
866 for (int i
= stacktop
; i
< max_stack
; ++i
)
867 stack
[i
] = unsuitable_type
;
869 // FIXME: subroutine handling?
872 // Merge STATE into this state. Destructively modifies this state.
873 // Returns true if the new state was in fact changed. Will throw an
874 // exception if the states are not mergeable.
875 bool merge (state
*state_old
, bool ret_semantics
,
878 bool changed
= false;
880 // Merge subroutine states. *THIS and *STATE_OLD must be in the
881 // same subroutine. Also, recursive subroutine calls must be
883 if (subroutine
== state_old
->subroutine
)
887 else if (subroutine
== 0)
889 subroutine
= state_old
->subroutine
;
893 verify_fail ("subroutines merged");
896 if (state_old
->stacktop
!= stacktop
)
897 verify_fail ("stack sizes differ");
898 for (int i
= 0; i
< state_old
->stacktop
; ++i
)
900 if (stack
[i
].merge (state_old
->stack
[i
]))
904 // Merge local variables.
905 for (int i
= 0; i
< max_locals
; ++i
)
907 if (! ret_semantics
|| local_changed
[i
])
909 if (locals
[i
].merge (state_old
->locals
[i
], true))
916 // If we're in a subroutine, we must compute the union of
917 // all the changed local variables.
918 if (state_old
->local_changed
[i
])
925 // Throw an exception if there is an uninitialized object on the
926 // stack or in a local variable. EXCEPTION_SEMANTICS controls
927 // whether we're using backwards-branch or exception-handing
929 void check_no_uninitialized_objects (int max_locals
,
930 bool exception_semantics
= false)
932 if (! exception_semantics
)
934 for (int i
= 0; i
< stacktop
; ++i
)
935 if (stack
[i
].isreference () && ! stack
[i
].isinitialized ())
936 verify_fail ("uninitialized object on stack");
939 for (int i
= 0; i
< max_locals
; ++i
)
940 if (locals
[i
].isreference () && ! locals
[i
].isinitialized ())
941 verify_fail ("uninitialized object in local variable");
944 // Note that a local variable was modified.
945 void note_variable (int index
)
948 local_changed
[index
] = true;
951 // Mark each `new'd object we know of that was allocated at PC as
953 void set_initialized (int pc
, int max_locals
)
955 for (int i
= 0; i
< stacktop
; ++i
)
956 stack
[i
].set_initialized (pc
);
957 for (int i
= 0; i
< max_locals
; ++i
)
958 locals
[i
].set_initialized (pc
);
961 // Return true if this state is the unmerged result of a `ret'.
962 bool is_unmerged_ret_state (int max_locals
) const
964 for (int i
= 0; i
< max_locals
; ++i
)
966 if (locals
[i
].key
== unused_by_subroutine_type
)
973 void print (const char *leader
, int pc
,
974 int max_stack
, int max_locals
) const
976 debug_print ("%s [%4d]: [stack] ", leader
, pc
);
978 for (i
= 0; i
< stacktop
; ++i
)
980 for (; i
< max_stack
; ++i
)
982 debug_print (" [local] ");
983 for (i
= 0; i
< max_locals
; ++i
)
985 debug_print (" | %p\n", this);
988 inline void print (const char *, int, int, int) const
991 #endif /* VERIFY_DEBUG */
996 if (current_state
->stacktop
<= 0)
997 verify_fail ("stack empty", start_PC
);
998 type r
= current_state
->stack
[--current_state
->stacktop
];
999 current_state
->stackdepth
-= r
.depth ();
1000 if (current_state
->stackdepth
< 0)
1001 verify_fail ("stack empty", start_PC
);
1007 type r
= pop_raw ();
1009 verify_fail ("narrow pop of wide type", start_PC
);
1015 type r
= pop_raw ();
1017 verify_fail ("wide pop of narrow type", start_PC
);
1021 type
pop_type (type match
)
1024 type t
= pop_raw ();
1025 if (! match
.compatible (t
))
1026 verify_fail ("incompatible type on stack", start_PC
);
1030 // Pop a reference type or a return address.
1031 type
pop_ref_or_return ()
1033 type t
= pop_raw ();
1034 if (! t
.isreference () && t
.key
!= return_address_type
)
1035 verify_fail ("expected reference or return address on stack", start_PC
);
1039 void push_type (type t
)
1041 // If T is a numeric type like short, promote it to int.
1044 int depth
= t
.depth ();
1045 if (current_state
->stackdepth
+ depth
> current_method
->max_stack
)
1046 verify_fail ("stack overflow");
1047 current_state
->stack
[current_state
->stacktop
++] = t
;
1048 current_state
->stackdepth
+= depth
;
1051 void set_variable (int index
, type t
)
1053 // If T is a numeric type like short, promote it to int.
1056 int depth
= t
.depth ();
1057 if (index
> current_method
->max_locals
- depth
)
1058 verify_fail ("invalid local variable");
1059 current_state
->locals
[index
] = t
;
1060 current_state
->note_variable (index
);
1064 current_state
->locals
[index
+ 1] = continuation_type
;
1065 current_state
->note_variable (index
+ 1);
1067 if (index
> 0 && current_state
->locals
[index
- 1].iswide ())
1069 current_state
->locals
[index
- 1] = unsuitable_type
;
1070 // There's no need to call note_variable here.
1074 type
get_variable (int index
, type t
)
1076 int depth
= t
.depth ();
1077 if (index
> current_method
->max_locals
- depth
)
1078 verify_fail ("invalid local variable", start_PC
);
1079 if (! t
.compatible (current_state
->locals
[index
]))
1080 verify_fail ("incompatible type in local variable", start_PC
);
1083 type
t (continuation_type
);
1084 if (! current_state
->locals
[index
+ 1].compatible (t
))
1085 verify_fail ("invalid local variable", start_PC
);
1087 return current_state
->locals
[index
];
1090 // Make sure ARRAY is an array type and that its elements are
1091 // compatible with type ELEMENT. Returns the actual element type.
1092 type
require_array_type (type array
, type element
)
1094 if (! array
.isarray ())
1095 verify_fail ("array required");
1097 type t
= array
.element_type ();
1098 if (! element
.compatible (t
))
1100 // Special case for byte arrays, which must also be boolean
1103 if (element
.key
== byte_type
)
1105 type
e2 (boolean_type
);
1106 ok
= e2
.compatible (t
);
1109 verify_fail ("incompatible array element type");
1112 // Return T and not ELEMENT, because T might be specialized.
1118 if (PC
>= current_method
->code_length
)
1119 verify_fail ("premature end of bytecode");
1120 return (jint
) bytecode
[PC
++] & 0xff;
1125 jint b1
= get_byte ();
1126 jint b2
= get_byte ();
1127 return (jint
) ((b1
<< 8) | b2
) & 0xffff;
1132 jint b1
= get_byte ();
1133 jint b2
= get_byte ();
1134 jshort s
= (b1
<< 8) | b2
;
1140 jint b1
= get_byte ();
1141 jint b2
= get_byte ();
1142 jint b3
= get_byte ();
1143 jint b4
= get_byte ();
1144 return (b1
<< 24) | (b2
<< 16) | (b3
<< 8) | b4
;
1147 int compute_jump (int offset
)
1149 int npc
= start_PC
+ offset
;
1150 if (npc
< 0 || npc
>= current_method
->code_length
)
1151 verify_fail ("branch out of range", start_PC
);
1155 // Merge the indicated state into the state at the branch target and
1156 // schedule a new PC if there is a change. If RET_SEMANTICS is
1157 // true, then we are merging from a `ret' instruction into the
1158 // instruction after a `jsr'. This is a special case with its own
1159 // modified semantics.
1160 void push_jump_merge (int npc
, state
*nstate
, bool ret_semantics
= false)
1162 bool changed
= true;
1163 if (states
[npc
] == NULL
)
1165 // There's a weird situation here. If are examining the
1166 // branch that results from a `ret', and there is not yet a
1167 // state available at the branch target (the instruction just
1168 // after the `jsr'), then we have to construct a special kind
1169 // of state at that point for future merging. This special
1170 // state has the type `unused_by_subroutine_type' in each slot
1171 // which was not modified by the subroutine.
1172 states
[npc
] = new state (nstate
, current_method
->max_stack
,
1173 current_method
->max_locals
, ret_semantics
);
1174 debug_print ("== New state in push_jump_merge\n");
1175 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1176 current_method
->max_locals
);
1180 debug_print ("== Merge states in push_jump_merge\n");
1181 nstate
->print ("Frm", start_PC
, current_method
->max_stack
,
1182 current_method
->max_locals
);
1183 states
[npc
]->print (" To", npc
, current_method
->max_stack
,
1184 current_method
->max_locals
);
1185 changed
= states
[npc
]->merge (nstate
, ret_semantics
,
1186 current_method
->max_locals
);
1187 states
[npc
]->print ("New", npc
, current_method
->max_stack
,
1188 current_method
->max_locals
);
1191 if (changed
&& states
[npc
]->next
== state::INVALID
)
1193 // The merge changed the state, and the new PC isn't yet on our
1194 // list of PCs to re-verify.
1195 states
[npc
]->next
= next_verify_pc
;
1196 next_verify_pc
= npc
;
1200 void push_jump (int offset
)
1202 int npc
= compute_jump (offset
);
1204 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1205 push_jump_merge (npc
, current_state
);
1208 void push_exception_jump (type t
, int pc
)
1210 current_state
->check_no_uninitialized_objects (current_method
->max_locals
,
1212 state
s (current_state
, current_method
->max_stack
,
1213 current_method
->max_locals
);
1214 s
.set_exception (t
, current_method
->max_stack
);
1215 push_jump_merge (pc
, &s
);
1220 int *prev_loc
= &next_verify_pc
;
1221 int npc
= next_verify_pc
;
1222 bool skipped
= false;
1224 while (npc
!= state::NO_NEXT
)
1226 // If the next available PC is an unmerged `ret' state, then
1227 // we aren't yet ready to handle it. That's because we would
1228 // need all kind of special cases to do so. So instead we
1229 // defer this jump until after we've processed it via a
1230 // fall-through. This has to happen because the instruction
1231 // before this one must be a `jsr'.
1232 if (! states
[npc
]->is_unmerged_ret_state (current_method
->max_locals
))
1234 *prev_loc
= states
[npc
]->next
;
1235 states
[npc
]->next
= state::INVALID
;
1240 prev_loc
= &states
[npc
]->next
;
1241 npc
= states
[npc
]->next
;
1244 // If we've skipped states and there is nothing else, that's a
1247 verify_fail ("pop_jump: can't happen");
1248 return state::NO_NEXT
;
1251 void invalidate_pc ()
1253 PC
= state::NO_NEXT
;
1256 void note_branch_target (int pc
, bool is_jsr_target
= false)
1258 if (pc
<= PC
&& ! (flags
[pc
] & FLAG_INSN_START
))
1259 verify_fail ("branch not to instruction start");
1260 flags
[pc
] |= FLAG_BRANCH_TARGET
;
1263 // Record the jsr which called this instruction.
1264 subr_info
*info
= (subr_info
*) _Jv_Malloc (sizeof (subr_info
));
1266 info
->next
= jsr_ptrs
[pc
];
1267 jsr_ptrs
[pc
] = info
;
1268 flags
[pc
] |= FLAG_JSR_TARGET
;
1272 void skip_padding ()
1274 while ((PC
% 4) > 0)
1275 if (get_byte () != 0)
1276 verify_fail ("found nonzero padding byte");
1279 // Return the subroutine to which the instruction at PC belongs.
1280 int get_subroutine (int pc
)
1282 if (states
[pc
] == NULL
)
1284 return states
[pc
]->subroutine
;
1287 // Do the work for a `ret' instruction. INDEX is the index into the
1289 void handle_ret_insn (int index
)
1291 get_variable (index
, return_address_type
);
1293 int csub
= current_state
->subroutine
;
1295 verify_fail ("no subroutine");
1297 for (subr_info
*subr
= jsr_ptrs
[csub
]; subr
!= NULL
; subr
= subr
->next
)
1299 // Temporarily modify the current state so it looks like we're
1300 // in the enclosing context.
1301 current_state
->subroutine
= get_subroutine (subr
->pc
);
1303 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1304 push_jump_merge (subr
->pc
, current_state
, true);
1307 current_state
->subroutine
= csub
;
1311 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1312 // sure this subroutine isn't already on the stack.
1313 void check_nonrecursive_call (int sub
, int dest
)
1318 verify_fail ("recursive subroutine call");
1319 for (subr_info
*info
= jsr_ptrs
[sub
]; info
!= NULL
; info
= info
->next
)
1320 check_nonrecursive_call (get_subroutine (info
->pc
), dest
);
1323 void handle_jsr_insn (int offset
)
1325 int npc
= compute_jump (offset
);
1328 current_state
->check_no_uninitialized_objects (current_method
->max_locals
);
1329 check_nonrecursive_call (current_state
->subroutine
, npc
);
1331 // Temporarily modify the current state so that it looks like we are
1332 // in the subroutine.
1333 push_type (return_address_type
);
1334 int save
= current_state
->subroutine
;
1335 current_state
->subroutine
= npc
;
1337 // Merge into the subroutine.
1338 push_jump_merge (npc
, current_state
);
1340 // Undo our modifications.
1341 current_state
->subroutine
= save
;
1342 pop_type (return_address_type
);
1345 jclass
construct_primitive_array_type (type_val prim
)
1351 k
= JvPrimClass (boolean
);
1354 k
= JvPrimClass (char);
1357 k
= JvPrimClass (float);
1360 k
= JvPrimClass (double);
1363 k
= JvPrimClass (byte
);
1366 k
= JvPrimClass (short);
1369 k
= JvPrimClass (int);
1372 k
= JvPrimClass (long);
1375 verify_fail ("unknown type in construct_primitive_array_type");
1377 k
= _Jv_GetArrayClass (k
, NULL
);
1381 // This pass computes the location of branch targets and also
1382 // instruction starts.
1383 void branch_prepass ()
1385 flags
= (char *) _Jv_Malloc (current_method
->code_length
);
1386 jsr_ptrs
= (subr_info
**) _Jv_Malloc (sizeof (subr_info
*)
1387 * current_method
->code_length
);
1389 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1395 bool last_was_jsr
= false;
1398 while (PC
< current_method
->code_length
)
1400 flags
[PC
] |= FLAG_INSN_START
;
1402 // If the previous instruction was a jsr, then the next
1403 // instruction is a branch target -- the branch being the
1404 // corresponding `ret'.
1406 note_branch_target (PC
);
1407 last_was_jsr
= false;
1410 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1414 case op_aconst_null
:
1550 case op_monitorenter
:
1551 case op_monitorexit
:
1559 case op_arraylength
:
1591 case op_invokespecial
:
1592 case op_invokestatic
:
1593 case op_invokevirtual
:
1597 case op_multianewarray
:
1603 last_was_jsr
= true;
1622 note_branch_target (compute_jump (get_short ()), last_was_jsr
);
1625 case op_tableswitch
:
1628 note_branch_target (compute_jump (get_int ()));
1629 jint low
= get_int ();
1630 jint hi
= get_int ();
1632 verify_fail ("invalid tableswitch", start_PC
);
1633 for (int i
= low
; i
<= hi
; ++i
)
1634 note_branch_target (compute_jump (get_int ()));
1638 case op_lookupswitch
:
1641 note_branch_target (compute_jump (get_int ()));
1642 int npairs
= get_int ();
1644 verify_fail ("too few pairs in lookupswitch", start_PC
);
1645 while (npairs
-- > 0)
1648 note_branch_target (compute_jump (get_int ()));
1653 case op_invokeinterface
:
1661 opcode
= (java_opcode
) get_byte ();
1663 if (opcode
== op_iinc
)
1669 last_was_jsr
= true;
1672 note_branch_target (compute_jump (get_int ()), last_was_jsr
);
1676 verify_fail ("unrecognized instruction in branch_prepass",
1680 // See if any previous branch tried to branch to the middle of
1681 // this instruction.
1682 for (int pc
= start_PC
+ 1; pc
< PC
; ++pc
)
1684 if ((flags
[pc
] & FLAG_BRANCH_TARGET
))
1685 verify_fail ("branch to middle of instruction", pc
);
1689 // Verify exception handlers.
1690 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1692 if (! (flags
[exception
[i
].handler_pc
] & FLAG_INSN_START
))
1693 verify_fail ("exception handler not at instruction start",
1694 exception
[i
].handler_pc
);
1695 if (! (flags
[exception
[i
].start_pc
] & FLAG_INSN_START
))
1696 verify_fail ("exception start not at instruction start",
1697 exception
[i
].start_pc
);
1698 if (exception
[i
].end_pc
!= current_method
->code_length
1699 && ! (flags
[exception
[i
].end_pc
] & FLAG_INSN_START
))
1700 verify_fail ("exception end not at instruction start",
1701 exception
[i
].end_pc
);
1703 flags
[exception
[i
].handler_pc
] |= FLAG_BRANCH_TARGET
;
1707 void check_pool_index (int index
)
1709 if (index
< 0 || index
>= current_class
->constants
.size
)
1710 verify_fail ("constant pool index out of range", start_PC
);
1713 type
check_class_constant (int index
)
1715 check_pool_index (index
);
1716 _Jv_Constants
*pool
= ¤t_class
->constants
;
1717 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedClass
)
1718 return type (pool
->data
[index
].clazz
);
1719 else if (pool
->tags
[index
] == JV_CONSTANT_Class
)
1720 return type (pool
->data
[index
].utf8
);
1721 verify_fail ("expected class constant", start_PC
);
1724 type
check_constant (int index
)
1726 check_pool_index (index
);
1727 _Jv_Constants
*pool
= ¤t_class
->constants
;
1728 if (pool
->tags
[index
] == JV_CONSTANT_ResolvedString
1729 || pool
->tags
[index
] == JV_CONSTANT_String
)
1730 return type (&java::lang::String::class$
);
1731 else if (pool
->tags
[index
] == JV_CONSTANT_Integer
)
1732 return type (int_type
);
1733 else if (pool
->tags
[index
] == JV_CONSTANT_Float
)
1734 return type (float_type
);
1735 verify_fail ("String, int, or float constant expected", start_PC
);
1738 type
check_wide_constant (int index
)
1740 check_pool_index (index
);
1741 _Jv_Constants
*pool
= ¤t_class
->constants
;
1742 if (pool
->tags
[index
] == JV_CONSTANT_Long
)
1743 return type (long_type
);
1744 else if (pool
->tags
[index
] == JV_CONSTANT_Double
)
1745 return type (double_type
);
1746 verify_fail ("long or double constant expected", start_PC
);
1749 // Helper for both field and method. These are laid out the same in
1750 // the constant pool.
1751 type
handle_field_or_method (int index
, int expected
,
1752 _Jv_Utf8Const
**name
,
1753 _Jv_Utf8Const
**fmtype
)
1755 check_pool_index (index
);
1756 _Jv_Constants
*pool
= ¤t_class
->constants
;
1757 if (pool
->tags
[index
] != expected
)
1758 verify_fail ("didn't see expected constant", start_PC
);
1759 // Once we know we have a Fieldref or Methodref we assume that it
1760 // is correctly laid out in the constant pool. I think the code
1761 // in defineclass.cc guarantees this.
1762 _Jv_ushort class_index
, name_and_type_index
;
1763 _Jv_loadIndexes (&pool
->data
[index
],
1765 name_and_type_index
);
1766 _Jv_ushort name_index
, desc_index
;
1767 _Jv_loadIndexes (&pool
->data
[name_and_type_index
],
1768 name_index
, desc_index
);
1770 *name
= pool
->data
[name_index
].utf8
;
1771 *fmtype
= pool
->data
[desc_index
].utf8
;
1773 return check_class_constant (class_index
);
1776 // Return field's type, compute class' type if requested.
1777 type
check_field_constant (int index
, type
*class_type
= NULL
)
1779 _Jv_Utf8Const
*name
, *field_type
;
1780 type ct
= handle_field_or_method (index
,
1781 JV_CONSTANT_Fieldref
,
1782 &name
, &field_type
);
1785 if (field_type
->data
[0] == '[' || field_type
->data
[0] == 'L')
1786 return type (field_type
);
1787 return get_type_val_for_signature (field_type
->data
[0]);
1790 type
check_method_constant (int index
, bool is_interface
,
1791 _Jv_Utf8Const
**method_name
,
1792 _Jv_Utf8Const
**method_signature
)
1794 return handle_field_or_method (index
,
1796 ? JV_CONSTANT_InterfaceMethodref
1797 : JV_CONSTANT_Methodref
),
1798 method_name
, method_signature
);
1801 type
get_one_type (char *&p
)
1819 _Jv_Utf8Const
*name
= make_utf8_const (start
, p
- start
);
1823 // Casting to jchar here is ok since we are looking at an ASCII
1825 type_val rt
= get_type_val_for_signature (jchar (v
));
1827 if (arraycount
== 0)
1829 // Callers of this function eventually push their arguments on
1830 // the stack. So, promote them here.
1831 return type (rt
).promote ();
1834 jclass k
= construct_primitive_array_type (rt
);
1835 while (--arraycount
> 0)
1836 k
= _Jv_GetArrayClass (k
, NULL
);
1840 void compute_argument_types (_Jv_Utf8Const
*signature
,
1843 char *p
= signature
->data
;
1849 types
[i
++] = get_one_type (p
);
1852 type
compute_return_type (_Jv_Utf8Const
*signature
)
1854 char *p
= signature
->data
;
1858 return get_one_type (p
);
1861 void check_return_type (type onstack
)
1863 type rt
= compute_return_type (current_method
->self
->signature
);
1864 if (! rt
.compatible (onstack
))
1865 verify_fail ("incompatible return type", start_PC
);
1868 void verify_instructions_0 ()
1870 current_state
= new state (current_method
->max_stack
,
1871 current_method
->max_locals
);
1879 using namespace java::lang::reflect
;
1880 if (! Modifier::isStatic (current_method
->self
->accflags
))
1882 type
kurr (current_class
);
1883 if (_Jv_equalUtf8Consts (current_method
->self
->name
, gcj::init_name
))
1884 kurr
.set_uninitialized (type::SELF
);
1885 set_variable (0, kurr
);
1889 // We have to handle wide arguments specially here.
1890 int arg_count
= _Jv_count_arguments (current_method
->self
->signature
);
1891 type arg_types
[arg_count
];
1892 compute_argument_types (current_method
->self
->signature
, arg_types
);
1893 for (int i
= 0; i
< arg_count
; ++i
)
1895 set_variable (var
, arg_types
[i
]);
1897 if (arg_types
[i
].iswide ())
1902 states
= (state
**) _Jv_Malloc (sizeof (state
*)
1903 * current_method
->code_length
);
1904 for (int i
= 0; i
< current_method
->code_length
; ++i
)
1907 next_verify_pc
= state::NO_NEXT
;
1911 // If the PC was invalidated, get a new one from the work list.
1912 if (PC
== state::NO_NEXT
)
1915 if (PC
== state::INVALID
)
1916 verify_fail ("can't happen: saw state::INVALID");
1917 if (PC
== state::NO_NEXT
)
1919 // Set up the current state.
1920 current_state
->copy (states
[PC
], current_method
->max_stack
,
1921 current_method
->max_locals
);
1925 // Control can't fall off the end of the bytecode. We
1926 // only need to check this in the fall-through case,
1927 // because branch bounds are checked when they are
1929 if (PC
>= current_method
->code_length
)
1930 verify_fail ("fell off end");
1932 // We only have to do this checking in the situation where
1933 // control flow falls through from the previous
1934 // instruction. Otherwise merging is done at the time we
1936 if (states
[PC
] != NULL
)
1938 // We've already visited this instruction. So merge
1939 // the states together. If this yields no change then
1940 // we don't have to re-verify. However, if the new
1941 // state is an the result of an unmerged `ret', we
1942 // must continue through it.
1943 debug_print ("== Fall through merge\n");
1944 states
[PC
]->print ("Old", PC
, current_method
->max_stack
,
1945 current_method
->max_locals
);
1946 current_state
->print ("Cur", PC
, current_method
->max_stack
,
1947 current_method
->max_locals
);
1948 if (! current_state
->merge (states
[PC
], false,
1949 current_method
->max_locals
)
1950 && ! states
[PC
]->is_unmerged_ret_state (current_method
->max_locals
))
1952 debug_print ("== Fall through optimization\n");
1956 // Save a copy of it for later.
1957 states
[PC
]->copy (current_state
, current_method
->max_stack
,
1958 current_method
->max_locals
);
1959 current_state
->print ("New", PC
, current_method
->max_stack
,
1960 current_method
->max_locals
);
1964 // We only have to keep saved state at branch targets. If
1965 // we're at a branch target and the state here hasn't been set
1966 // yet, we set it now.
1967 if (states
[PC
] == NULL
&& (flags
[PC
] & FLAG_BRANCH_TARGET
))
1969 states
[PC
] = new state (current_state
, current_method
->max_stack
,
1970 current_method
->max_locals
);
1973 // Set this before handling exceptions so that debug output is
1977 // Update states for all active exception handlers. Ordinarily
1978 // there are not many exception handlers. So we simply run
1979 // through them all.
1980 for (int i
= 0; i
< current_method
->exc_count
; ++i
)
1982 if (PC
>= exception
[i
].start_pc
&& PC
< exception
[i
].end_pc
)
1984 type
handler (&java::lang::Throwable::class$
);
1985 if (exception
[i
].handler_type
!= 0)
1986 handler
= check_class_constant (exception
[i
].handler_type
);
1987 push_exception_jump (handler
, exception
[i
].handler_pc
);
1991 current_state
->print (" ", PC
, current_method
->max_stack
,
1992 current_method
->max_locals
);
1993 java_opcode opcode
= (java_opcode
) bytecode
[PC
++];
1999 case op_aconst_null
:
2000 push_type (null_type
);
2010 push_type (int_type
);
2015 push_type (long_type
);
2021 push_type (float_type
);
2026 push_type (double_type
);
2031 push_type (int_type
);
2036 push_type (int_type
);
2040 push_type (check_constant (get_byte ()));
2043 push_type (check_constant (get_ushort ()));
2046 push_type (check_wide_constant (get_ushort ()));
2050 push_type (get_variable (get_byte (), int_type
));
2053 push_type (get_variable (get_byte (), long_type
));
2056 push_type (get_variable (get_byte (), float_type
));
2059 push_type (get_variable (get_byte (), double_type
));
2062 push_type (get_variable (get_byte (), reference_type
));
2069 push_type (get_variable (opcode
- op_iload_0
, int_type
));
2075 push_type (get_variable (opcode
- op_lload_0
, long_type
));
2081 push_type (get_variable (opcode
- op_fload_0
, float_type
));
2087 push_type (get_variable (opcode
- op_dload_0
, double_type
));
2093 push_type (get_variable (opcode
- op_aload_0
, reference_type
));
2096 pop_type (int_type
);
2097 push_type (require_array_type (pop_type (reference_type
),
2101 pop_type (int_type
);
2102 push_type (require_array_type (pop_type (reference_type
),
2106 pop_type (int_type
);
2107 push_type (require_array_type (pop_type (reference_type
),
2111 pop_type (int_type
);
2112 push_type (require_array_type (pop_type (reference_type
),
2116 pop_type (int_type
);
2117 push_type (require_array_type (pop_type (reference_type
),
2121 pop_type (int_type
);
2122 require_array_type (pop_type (reference_type
), byte_type
);
2123 push_type (int_type
);
2126 pop_type (int_type
);
2127 require_array_type (pop_type (reference_type
), char_type
);
2128 push_type (int_type
);
2131 pop_type (int_type
);
2132 require_array_type (pop_type (reference_type
), short_type
);
2133 push_type (int_type
);
2136 set_variable (get_byte (), pop_type (int_type
));
2139 set_variable (get_byte (), pop_type (long_type
));
2142 set_variable (get_byte (), pop_type (float_type
));
2145 set_variable (get_byte (), pop_type (double_type
));
2148 set_variable (get_byte (), pop_ref_or_return ());
2154 set_variable (opcode
- op_istore_0
, pop_type (int_type
));
2160 set_variable (opcode
- op_lstore_0
, pop_type (long_type
));
2166 set_variable (opcode
- op_fstore_0
, pop_type (float_type
));
2172 set_variable (opcode
- op_dstore_0
, pop_type (double_type
));
2178 set_variable (opcode
- op_astore_0
, pop_ref_or_return ());
2181 pop_type (int_type
);
2182 pop_type (int_type
);
2183 require_array_type (pop_type (reference_type
), int_type
);
2186 pop_type (long_type
);
2187 pop_type (int_type
);
2188 require_array_type (pop_type (reference_type
), long_type
);
2191 pop_type (float_type
);
2192 pop_type (int_type
);
2193 require_array_type (pop_type (reference_type
), float_type
);
2196 pop_type (double_type
);
2197 pop_type (int_type
);
2198 require_array_type (pop_type (reference_type
), double_type
);
2201 pop_type (reference_type
);
2202 pop_type (int_type
);
2203 require_array_type (pop_type (reference_type
), reference_type
);
2206 pop_type (int_type
);
2207 pop_type (int_type
);
2208 require_array_type (pop_type (reference_type
), byte_type
);
2211 pop_type (int_type
);
2212 pop_type (int_type
);
2213 require_array_type (pop_type (reference_type
), char_type
);
2216 pop_type (int_type
);
2217 pop_type (int_type
);
2218 require_array_type (pop_type (reference_type
), short_type
);
2245 type t2
= pop_raw ();
2260 type t
= pop_raw ();
2273 type t1
= pop_raw ();
2291 type t1
= pop_raw ();
2294 type t2
= pop_raw ();
2312 type t3
= pop_raw ();
2350 pop_type (int_type
);
2351 push_type (pop_type (int_type
));
2361 pop_type (long_type
);
2362 push_type (pop_type (long_type
));
2367 pop_type (int_type
);
2368 push_type (pop_type (long_type
));
2375 pop_type (float_type
);
2376 push_type (pop_type (float_type
));
2383 pop_type (double_type
);
2384 push_type (pop_type (double_type
));
2390 push_type (pop_type (int_type
));
2393 push_type (pop_type (long_type
));
2396 push_type (pop_type (float_type
));
2399 push_type (pop_type (double_type
));
2402 get_variable (get_byte (), int_type
);
2406 pop_type (int_type
);
2407 push_type (long_type
);
2410 pop_type (int_type
);
2411 push_type (float_type
);
2414 pop_type (int_type
);
2415 push_type (double_type
);
2418 pop_type (long_type
);
2419 push_type (int_type
);
2422 pop_type (long_type
);
2423 push_type (float_type
);
2426 pop_type (long_type
);
2427 push_type (double_type
);
2430 pop_type (float_type
);
2431 push_type (int_type
);
2434 pop_type (float_type
);
2435 push_type (long_type
);
2438 pop_type (float_type
);
2439 push_type (double_type
);
2442 pop_type (double_type
);
2443 push_type (int_type
);
2446 pop_type (double_type
);
2447 push_type (long_type
);
2450 pop_type (double_type
);
2451 push_type (float_type
);
2454 pop_type (long_type
);
2455 pop_type (long_type
);
2456 push_type (int_type
);
2460 pop_type (float_type
);
2461 pop_type (float_type
);
2462 push_type (int_type
);
2466 pop_type (double_type
);
2467 pop_type (double_type
);
2468 push_type (int_type
);
2476 pop_type (int_type
);
2477 push_jump (get_short ());
2485 pop_type (int_type
);
2486 pop_type (int_type
);
2487 push_jump (get_short ());
2491 pop_type (reference_type
);
2492 pop_type (reference_type
);
2493 push_jump (get_short ());
2496 push_jump (get_short ());
2500 handle_jsr_insn (get_short ());
2503 handle_ret_insn (get_byte ());
2505 case op_tableswitch
:
2507 pop_type (int_type
);
2509 push_jump (get_int ());
2510 jint low
= get_int ();
2511 jint high
= get_int ();
2512 // Already checked LOW -vs- HIGH.
2513 for (int i
= low
; i
<= high
; ++i
)
2514 push_jump (get_int ());
2519 case op_lookupswitch
:
2521 pop_type (int_type
);
2523 push_jump (get_int ());
2524 jint npairs
= get_int ();
2525 // Already checked NPAIRS >= 0.
2527 for (int i
= 0; i
< npairs
; ++i
)
2529 jint key
= get_int ();
2530 if (i
> 0 && key
<= lastkey
)
2531 verify_fail ("lookupswitch pairs unsorted", start_PC
);
2533 push_jump (get_int ());
2539 check_return_type (pop_type (int_type
));
2543 check_return_type (pop_type (long_type
));
2547 check_return_type (pop_type (float_type
));
2551 check_return_type (pop_type (double_type
));
2555 check_return_type (pop_type (reference_type
));
2559 check_return_type (void_type
);
2563 push_type (check_field_constant (get_ushort ()));
2566 pop_type (check_field_constant (get_ushort ()));
2571 type field
= check_field_constant (get_ushort (), &klass
);
2579 type field
= check_field_constant (get_ushort (), &klass
);
2585 case op_invokevirtual
:
2586 case op_invokespecial
:
2587 case op_invokestatic
:
2588 case op_invokeinterface
:
2590 _Jv_Utf8Const
*method_name
, *method_signature
;
2592 = check_method_constant (get_ushort (),
2593 opcode
== op_invokeinterface
,
2596 int arg_count
= _Jv_count_arguments (method_signature
);
2597 if (opcode
== op_invokeinterface
)
2599 int nargs
= get_byte ();
2601 verify_fail ("too few arguments to invokeinterface",
2603 if (get_byte () != 0)
2604 verify_fail ("invokeinterface dummy byte is wrong",
2606 if (nargs
- 1 != arg_count
)
2607 verify_fail ("wrong argument count for invokeinterface",
2611 bool is_init
= false;
2612 if (_Jv_equalUtf8Consts (method_name
, gcj::init_name
))
2615 if (opcode
!= op_invokespecial
)
2616 verify_fail ("can't invoke <init>", start_PC
);
2618 else if (method_name
->data
[0] == '<')
2619 verify_fail ("can't invoke method starting with `<'",
2622 // Pop arguments and check types.
2623 type arg_types
[arg_count
];
2624 compute_argument_types (method_signature
, arg_types
);
2625 for (int i
= arg_count
- 1; i
>= 0; --i
)
2626 pop_type (arg_types
[i
]);
2628 if (opcode
!= op_invokestatic
)
2630 type t
= class_type
;
2633 // In this case the PC doesn't matter.
2634 t
.set_uninitialized (type::UNINIT
);
2638 current_state
->set_initialized (t
.get_pc (),
2639 current_method
->max_locals
);
2642 type rt
= compute_return_type (method_signature
);
2650 type t
= check_class_constant (get_ushort ());
2651 if (t
.isarray () || t
.isinterface () || t
.isabstract ())
2652 verify_fail ("type is array, interface, or abstract",
2654 t
.set_uninitialized (start_PC
);
2661 int atype
= get_byte ();
2662 // We intentionally have chosen constants to make this
2664 if (atype
< boolean_type
|| atype
> long_type
)
2665 verify_fail ("type not primitive", start_PC
);
2666 pop_type (int_type
);
2667 push_type (construct_primitive_array_type (type_val (atype
)));
2671 pop_type (int_type
);
2672 push_type (check_class_constant (get_ushort ()).to_array ());
2674 case op_arraylength
:
2676 type t
= pop_type (reference_type
);
2678 verify_fail ("array type expected", start_PC
);
2679 push_type (int_type
);
2683 pop_type (type (&java::lang::Throwable::class$
));
2687 pop_type (reference_type
);
2688 push_type (check_class_constant (get_ushort ()));
2691 pop_type (reference_type
);
2692 check_class_constant (get_ushort ());
2693 push_type (int_type
);
2695 case op_monitorenter
:
2696 pop_type (reference_type
);
2698 case op_monitorexit
:
2699 pop_type (reference_type
);
2703 switch (get_byte ())
2706 push_type (get_variable (get_ushort (), int_type
));
2709 push_type (get_variable (get_ushort (), long_type
));
2712 push_type (get_variable (get_ushort (), float_type
));
2715 push_type (get_variable (get_ushort (), double_type
));
2718 push_type (get_variable (get_ushort (), reference_type
));
2721 set_variable (get_ushort (), pop_type (int_type
));
2724 set_variable (get_ushort (), pop_type (long_type
));
2727 set_variable (get_ushort (), pop_type (float_type
));
2730 set_variable (get_ushort (), pop_type (double_type
));
2733 set_variable (get_ushort (), pop_type (reference_type
));
2736 handle_ret_insn (get_short ());
2739 get_variable (get_ushort (), int_type
);
2743 verify_fail ("unrecognized wide instruction", start_PC
);
2747 case op_multianewarray
:
2749 type atype
= check_class_constant (get_ushort ());
2750 int dim
= get_byte ();
2752 verify_fail ("too few dimensions to multianewarray", start_PC
);
2753 atype
.verify_dimensions (dim
);
2754 for (int i
= 0; i
< dim
; ++i
)
2755 pop_type (int_type
);
2761 pop_type (reference_type
);
2762 push_jump (get_short ());
2765 push_jump (get_int ());
2769 handle_jsr_insn (get_int ());
2773 // Unrecognized opcode.
2774 verify_fail ("unrecognized instruction in verify_instructions_0",
2782 void verify_instructions ()
2785 verify_instructions_0 ();
2788 _Jv_BytecodeVerifier (_Jv_InterpMethod
*m
)
2790 // We just print the text as utf-8. This is just for debugging
2792 debug_print ("--------------------------------\n");
2793 debug_print ("-- Verifying method `%s'\n", m
->self
->name
->data
);
2796 bytecode
= m
->bytecode ();
2797 exception
= m
->exceptions ();
2798 current_class
= m
->defining_class
;
2806 ~_Jv_BytecodeVerifier ()
2813 _Jv_Free (jsr_ptrs
);
2814 while (utf8_list
!= NULL
)
2816 linked_utf8
*n
= utf8_list
->next
;
2817 _Jv_Free (utf8_list
->val
);
2818 _Jv_Free (utf8_list
);
2825 _Jv_VerifyMethod (_Jv_InterpMethod
*meth
)
2827 _Jv_BytecodeVerifier
v (meth
);
2828 v
.verify_instructions ();
2831 // FIXME: add more info, like PC, when required.
2833 verify_fail (char *s
, jint pc
)
2835 using namespace java::lang
;
2836 StringBuffer
*buf
= new StringBuffer ();
2838 buf
->append (JvNewStringLatin1 ("verification failed"));
2841 buf
->append (JvNewStringLatin1 (" at PC "));
2844 buf
->append (JvNewStringLatin1 (": "));
2845 buf
->append (JvNewStringLatin1 (s
));
2846 throw new java::lang::VerifyError (buf
->toString ());
2849 #endif /* INTERPRETER */