OSDN Git Service

PR fortran/19754
[pf3gnuchains/gcc-fork.git] / gcc / java / verify-impl.c
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 /* Written by Tom Tromey <tromey@redhat.com>  */
10
11 /* Uncomment this to enable debugging output.  */
12 /* #define VERIFY_DEBUG */
13
14 #include "config.h"
15
16 #include "verify.h"
17
18 /* Hack to work around namespace pollution from java-tree.h.  */
19 #undef current_class
20
21 #ifdef VERIFY_DEBUG
22 #include <stdio.h>
23 #endif /* VERIFY_DEBUG */
24
25 /* This is used to mark states which are not scheduled for
26    verification. */
27 #define INVALID_STATE ((state *) -1)
28
29 #ifdef VERIFY_DEBUG
30 static void
31 debug_print (const char *fmt, ...)
32 {
33   va_list ap;
34   va_start (ap, fmt);
35   vfprintf (stderr, fmt, ap);
36   va_end (ap);
37 }
38 #else
39 static void
40 debug_print (const char *fmt ATTRIBUTE_UNUSED, ...)
41 {
42 }    
43 #endif /* VERIFY_DEBUG */
44
45 #if 0
46 static void debug_print (const char *fmt, ...)
47   __attribute__ ((format (printf, 1, 2)));
48
49 static void
50 debug_print (const char *fmt, ...)
51 {
52 #ifdef VERIFY_DEBUG
53   va_list ap;
54   va_start (ap, fmt);
55   vfprintf (stderr, fmt, ap);
56   va_end (ap);
57 #endif /* VERIFY_DEBUG */
58 }
59 #endif
60
61 /* This started as a fairly ordinary verifier, and for the most part
62    it remains so.  It works in the obvious way, by modeling the effect
63    of each opcode as it is encountered.  For most opcodes, this is a
64    straightforward operation.
65
66    This verifier does not do type merging.  It used to, but this
67    results in difficulty verifying some relatively simple code
68    involving interfaces, and it pushed some verification work into the
69    interpreter.
70
71    Instead of merging reference types, when we reach a point where two
72    flows of control merge, we simply keep the union of reference types
73    from each branch.  Then, when we need to verify a fact about a
74    reference on the stack (e.g., that it is compatible with the
75    argument type of a method), we check to ensure that all possible
76    types satisfy the requirement.
77
78    Another area this verifier differs from the norm is in its handling
79    of subroutines.  The JVM specification has some confusing things to
80    say about subroutines.  For instance, it makes claims about not
81    allowing subroutines to merge and it rejects recursive subroutines.
82    For the most part these are red herrings; we used to try to follow
83    these things but they lead to problems.  For example, the notion of
84    "being in a subroutine" is not well-defined: is an exception
85    handler in a subroutine?  If you never execute the `ret' but
86    instead `goto 1' do you remain in the subroutine?
87
88    For clarity on what is really required for type safety, read
89    "Simple Verification Technique for Complex Java Bytecode
90    Subroutines" by Alessandro Coglio.  Among other things this paper
91    shows that recursive subroutines are not harmful to type safety.
92    We implement something similar to what he proposes.  Note that this
93    means that this verifier will accept code that is rejected by some
94    other verifiers.
95
96    For those not wanting to read the paper, the basic observation is
97    that we can maintain split states in subroutines.  We maintain one
98    state for each calling `jsr'.  In other words, we re-verify a
99    subroutine once for each caller, using the exact types held by the
100    callers (as opposed to the old approach of merging types and
101    keeping a bitmap registering what did or did not change).  This
102    approach lets us continue to verify correctly even when a
103    subroutine is exited via `goto' or `athrow' and not `ret'.
104
105    In some other areas the JVM specification is (mildly) incorrect,
106    so we diverge.  For instance, you cannot
107    violate type safety by allocating an object with `new' and then
108    failing to initialize it, no matter how one branches or where one
109    stores the uninitialized reference.  See "Improving the official
110    specification of Java bytecode verification" by Alessandro Coglio.
111
112    Note that there's no real point in enforcing that padding bytes or
113    the mystery byte of invokeinterface must be 0, but we do that
114    regardless.
115
116    The verifier is currently neither completely lazy nor eager when it
117    comes to loading classes.  It tries to represent types by name when
118    possible, and then loads them when it needs to verify a fact about
119    the type.  Checking types by name is valid because we only use
120    names which come from the current class' constant pool.  Since all
121    such names are looked up using the same class loader, there is no
122    danger that we might be fooled into comparing different types with
123    the same name.
124
125    In the future we plan to allow for a completely lazy mode of
126    operation, where the verifier will construct a list of type
127    assertions to be checked later.
128
129    Some test cases for the verifier live in the "verify" module of the
130    Mauve test suite.  However, some of these are presently
131    (2004-01-20) believed to be incorrect.  (More precisely the notion
132    of "correct" is not well-defined, and this verifier differs from
133    others while remaining type-safe.)  Some other tests live in the
134    libgcj test suite.
135
136    This verifier is also written to be pluggable.  This means that it
137    is intended for use in a variety of environments, not just libgcj.
138    As a result the verifier expects a number of type and method
139    declarations to be declared in "verify.h".  The intent is that you
140    recompile the verifier for your particular environment.  This
141    approach was chosen so that operations could be inlined in verify.h
142    as much as possible.
143
144    See the verify.h that accompanies this copy of the verifier to see
145    what types, preprocessor defines, and functions must be declared.
146    The interface is ad hoc, but was defined so that it could be
147    implemented to connect to a pure C program.
148 */
149
150 #define FLAG_INSN_START 1
151 #define FLAG_BRANCH_TARGET 2
152 #define FLAG_INSN_SEEN 4
153
154 struct state;
155 struct type;
156 struct ref_intersection;
157
158 typedef struct state state;
159 typedef struct type type;
160 typedef struct ref_intersection ref_intersection;
161
162 /*typedef struct state_list state_list;*/
163
164 typedef struct state_list
165 {
166   state *val;
167   struct state_list *next;
168 } state_list;
169
170 typedef struct vfy_string_list
171 {
172   vfy_string val;
173   struct vfy_string_list *next;
174 } vfy_string_list;
175
176 typedef struct verifier_context
177 {
178   /* The current PC.  */
179   int PC;
180   /* The PC corresponding to the start of the current instruction.  */
181   int start_PC;
182
183   /* The current state of the stack, locals, etc.  */
184   state *current_state;
185
186   /* At each branch target we keep a linked list of all the states we
187      can process at that point.  We'll only have multiple states at a
188      given PC if they both have different return-address types in the
189      same stack or local slot.  This array is indexed by PC and holds
190      the list of all such states.  */
191   state_list **states;
192
193   /* We keep a linked list of all the states which we must reverify.
194      This is the head of the list.  */
195   state *next_verify_state;
196
197   /* We keep some flags for each instruction.  The values are the
198      FLAG_* constants defined above.  This is an array indexed by PC.  */
199   char *flags;
200
201   /* The bytecode itself.  */
202   const unsigned char *bytecode;
203   /* The exceptions.  */
204   vfy_exception *exception;
205
206   /* Defining class.  */
207   vfy_jclass current_class;
208   /* This method.  */
209   vfy_method *current_method;
210
211   /* A linked list of utf8 objects we allocate.  */
212   vfy_string_list *utf8_list;
213
214   /* A linked list of all ref_intersection objects we allocate.  */
215   ref_intersection *isect_list;
216 } verifier_context;
217
218 /* The current verifier's state data. This is maintained by
219    {push/pop}_verifier_context to provide a shorthand form to access
220    the verification state. */
221 static GTY(()) verifier_context *vfr;
222
223 /* Local function declarations.  */
224 bool type_initialized (type *t);
225 int ref_count_dimensions (ref_intersection *ref);
226
227 #if 0
228   /* Create a new Utf-8 constant and return it.  We do this to avoid
229      having our Utf-8 constants prematurely collected.  */
230   static vfy_string
231   make_utf8_const (const char *s, int len)
232   {
233     vfy_string val = vfy_make_string (s, len);
234     vfy_string_list *lu = vfy_alloc (sizeof (vfy_string_list));
235     lu->val = val;
236     lu->next = vfr->utf8_list;
237     vfr->utf8_list = lu;
238
239     return val;
240   }
241 #endif
242
243 static void
244 verify_fail_pc (const char *s, int pc)
245 {
246   vfy_fail (s, pc, vfr->current_class, vfr->current_method);  
247 }
248
249 static void
250 verify_fail (const char *s)
251 {
252   verify_fail_pc (s, -1);
253 }
254
255 /* This enum holds a list of tags for all the different types we
256    need to handle.  Reference types are treated specially by the
257    type class.  */
258 typedef enum type_val
259 {
260   void_type,
261
262   /* The values for primitive types are chosen to correspond to values
263      specified to newarray. */
264   boolean_type = 4,
265   char_type = 5,
266   float_type = 6,
267   double_type = 7,
268   byte_type = 8,
269   short_type = 9,
270   int_type = 10,
271   long_type = 11,
272
273   /* Used when overwriting second word of a double or long in the
274      local variables.  Also used after merging local variable states
275      to indicate an unusable value.  */
276   unsuitable_type,
277   return_address_type,
278   /* This is the second word of a two-word value, i.e., a double or
279      a long.  */
280   continuation_type,
281
282   /* Everything after `reference_type' must be a reference type.  */
283   reference_type,
284   null_type,
285   uninitialized_reference_type
286 } type_val;
287
288 /* This represents a merged class type.  Some verifiers (including
289    earlier versions of this one) will compute the intersection of
290    two class types when merging states.  However, this loses
291    critical information about interfaces implemented by the various
292    classes.  So instead we keep track of all the actual classes that
293    have been merged.  */
294 struct ref_intersection
295 {
296   /* Whether or not this type has been resolved.  */
297   bool is_resolved;
298
299   /* Actual type data.  */
300   union
301   {
302     /* For a resolved reference type, this is a pointer to the class.  */
303     vfy_jclass klass;
304     /* For other reference types, this it the name of the class.  */
305     vfy_string name;
306   } data;
307
308   /* Link to the next reference in the intersection.  */
309   ref_intersection *ref_next;
310
311   /* This is used to keep track of all the allocated
312      ref_intersection objects, so we can free them.
313      FIXME: we should allocate these in chunks.  */
314   ref_intersection *alloc_next;
315 };
316
317 static ref_intersection *
318 make_ref (void)
319 {
320   ref_intersection *new_ref = 
321     (ref_intersection *) vfy_alloc (sizeof (ref_intersection));
322
323   new_ref->alloc_next = vfr->isect_list;
324   vfr->isect_list = new_ref;
325   return new_ref;
326 }
327
328 static ref_intersection *
329 clone_ref (ref_intersection *dup)
330 {
331   ref_intersection *new_ref = make_ref ();
332
333   new_ref->is_resolved = dup->is_resolved;
334   new_ref->data = dup->data;
335   return new_ref;
336 }
337
338 static void 
339 resolve_ref (ref_intersection *ref)
340 {
341   if (ref->is_resolved)
342     return;
343   ref->data.klass = vfy_find_class (vfr->current_class, ref->data.name);
344   ref->is_resolved = true;
345 }
346
347 static bool 
348 refs_equal (ref_intersection *ref1, ref_intersection *ref2)
349 {
350   if (! ref1->is_resolved && ! ref2->is_resolved
351       && vfy_strings_equal (ref1->data.name, ref2->data.name))
352     return true;
353   if (! ref1->is_resolved)
354     resolve_ref (ref1);
355   if (! ref2->is_resolved)
356     resolve_ref (ref2);
357   return ref1->data.klass == ref2->data.klass;
358 }
359
360 /* Merge REF1 type into REF2, returning the result.  This will
361    return REF2 if all the classes in THIS already appear in
362    REF2.  */
363 static ref_intersection *
364 merge_refs (ref_intersection *ref1, ref_intersection *ref2)
365 {
366   ref_intersection *tail = ref2;
367   for (; ref1 != NULL; ref1 = ref1->ref_next)
368     {
369       bool add = true;
370       ref_intersection *iter;
371       for (iter = ref2; iter != NULL; iter = iter->ref_next)
372         {
373           if (refs_equal (ref1, iter))
374             {
375               add = false;
376               break;
377             }
378         }
379
380       if (add)
381         {
382           ref_intersection *new_tail = clone_ref (ref1);
383           new_tail->ref_next = tail;
384           tail = new_tail;
385         }
386     }
387   return tail;
388 }
389
390 /* See if an object of type SOURCE can be assigned to an object of
391    type TARGET.  This might resolve classes in one chain or the other.  */
392 static bool 
393 ref_compatible (ref_intersection *target, ref_intersection *source)
394 {
395   for (; target != NULL; target = target->ref_next)
396     {
397       ref_intersection *source_iter = source;
398
399       for (; source_iter != NULL; source_iter = source_iter->ref_next)
400         {
401           /* Avoid resolving if possible.  */
402           if (! target->is_resolved
403               && ! source_iter->is_resolved
404               && vfy_strings_equal (target->data.name,
405                                     source_iter->data.name))
406             continue;
407
408           if (! target->is_resolved)
409             resolve_ref (target);
410           if (! source_iter->is_resolved)
411             resolve_ref (source_iter);
412
413           if (! vfy_is_assignable_from (target->data.klass,
414                                         source_iter->data.klass))
415             return false;
416         }
417     }
418
419   return true;
420 }
421
422 static bool 
423 ref_isarray (ref_intersection *ref)
424 {
425   /* assert (ref_next == NULL);  */
426   if (ref->is_resolved)
427     return vfy_is_array (ref->data.klass);
428   else
429     return vfy_string_bytes (ref->data.name)[0] == '[';
430 }
431
432 static bool
433 ref_isinterface (ref_intersection *ref)
434 {
435   /* assert (ref_next == NULL);  */
436   if (! ref->is_resolved)
437     resolve_ref (ref);
438   return vfy_is_interface (ref->data.klass);
439 }
440
441 static bool
442 ref_isabstract (ref_intersection *ref)
443 {
444   /* assert (ref_next == NULL); */
445   if (! ref->is_resolved)
446     resolve_ref (ref);
447   return vfy_is_abstract (ref->data.klass);
448 }
449
450 static vfy_jclass 
451 ref_getclass (ref_intersection *ref)
452 {
453   if (! ref->is_resolved)
454     resolve_ref (ref);
455   return ref->data.klass;
456 }
457
458 int
459 ref_count_dimensions (ref_intersection *ref)
460 {
461   int ndims = 0;
462   if (ref->is_resolved)
463     {
464       vfy_jclass k = ref->data.klass;
465       while (vfy_is_array (k))
466         {
467           k = vfy_get_component_type (k);
468           ++ndims;
469         }
470     }
471   else
472     {
473       const char *p = vfy_string_bytes (ref->data.name);
474       while (*p++ == '[')
475         ++ndims;
476     }
477   return ndims;
478 }
479
480 /* Return the type_val corresponding to a primitive signature
481    character.  For instance `I' returns `int.class'.  */
482 static type_val
483 get_type_val_for_signature (char sig)
484 {
485   type_val rt;
486   switch (sig)
487     {
488     case 'Z':
489       rt = boolean_type;
490       break;
491     case 'B':
492       rt = byte_type;
493       break;
494     case 'C':
495       rt = char_type;
496       break;
497     case 'S':
498       rt = short_type;
499       break;
500     case 'I':
501       rt = int_type;
502       break;
503     case 'J':
504       rt = long_type;
505       break;
506     case 'F':
507       rt = float_type;
508       break;
509     case 'D':
510       rt = double_type;
511       break;
512     case 'V':
513       rt = void_type;
514       break;
515     default:
516       verify_fail ("invalid signature");
517       return null_type;
518     }
519   return rt;
520 }
521
522 /* Return the type_val corresponding to a primitive class.  */
523 static type_val
524 get_type_val_for_primtype (vfy_jclass k)
525 {
526   return get_type_val_for_signature (vfy_get_primitive_char (k));
527 }
528
529 /* The `type' class is used to represent a single type in the verifier.  */
530 struct type
531 {
532   /* The type key.  */
533   type_val key;
534
535   /* For reference types, the representation of the type.  */
536   ref_intersection *klass;
537
538   /* This is used in two situations.
539   
540      First, when constructing a new object, it is the PC of the
541      `new' instruction which created the object.  We use the special
542      value UNINIT to mean that this is uninitialized.  The special
543      value SELF is used for the case where the current method is
544      itself the <init> method.  the special value EITHER is used
545      when we may optionally allow either an uninitialized or
546      initialized reference to match.
547   
548      Second, when the key is return_address_type, this holds the PC
549      of the instruction following the `jsr'.  */
550   int pc;
551
552 #define UNINIT -2
553 #define SELF -1
554 #define EITHER -3
555 };
556
557 #if 0
558 /* Basic constructor.  */
559 static void
560 init_type (type *t)
561 {
562   t->key = unsuitable_type;
563   t->klass = NULL;
564   t->pc = UNINIT;
565 }
566 #endif
567
568 /* Make a new instance given the type tag.  We assume a generic
569    `reference_type' means Object.  */
570 static void
571 init_type_from_tag (type *t, type_val k)
572 {
573   t->key = k;
574   /* For reference_type, if KLASS==NULL then that means we are
575      looking for a generic object of any kind, including an
576      uninitialized reference.  */
577   t->klass = NULL;
578   t->pc = UNINIT;
579 }
580
581 /* Make a type for the given type_val tag K.  */
582 static type
583 make_type (type_val k)
584 {
585   type t;
586   init_type_from_tag (&t, k);
587   return t;
588 }
589
590 /* Make a new instance given a class.  */
591 static void
592 init_type_from_class (type *t, vfy_jclass k)
593 {
594   t->key = reference_type;
595   t->klass = make_ref ();
596   t->klass->is_resolved = true;
597   t->klass->data.klass = k;
598   t->klass->ref_next = NULL;
599   t->pc = UNINIT;
600 }
601
602 static type
603 make_type_from_class (vfy_jclass k)
604 {
605   type t;
606   init_type_from_class (&t, k);
607   return t;
608 }
609
610 static void
611 init_type_from_string (type *t, vfy_string n)
612 {
613   t->key = reference_type;
614   t->klass = make_ref ();
615   t->klass->is_resolved = false;
616   t->klass->data.name = n;
617   t->klass->ref_next = NULL;
618   t->pc = UNINIT;
619 }
620
621 static type
622 make_type_from_string (vfy_string n)
623 {
624   type t;
625   init_type_from_string (&t, n);
626   return t;
627 }
628
629 #if 0
630     /* Make a new instance given the name of a class.  */
631     type (vfy_string n)
632     {
633       key = reference_type;
634       klass = new ref_intersection (n, verifier);
635       pc = UNINIT;
636     }
637
638     /* Copy constructor.  */
639     static type copy_type (type *t)
640     {
641       type copy;
642       copy.key = t->key;
643       copy.klass = t->klass;
644       copy.pc = t->pc;
645       return copy;
646     }
647 #endif
648
649 /* Promote a numeric type.  */
650 static void
651 vfy_promote_type (type *t)
652 {
653   if (t->key == boolean_type || t->key == char_type
654       || t->key == byte_type || t->key == short_type)
655     t->key = int_type;
656 }
657 #define promote_type vfy_promote_type
658
659 /* Mark this type as the uninitialized result of `new'.  */
660 static void
661 type_set_uninitialized (type *t, int npc)
662 {
663   if (t->key == reference_type)
664     t->key = uninitialized_reference_type;
665   else
666     verify_fail ("internal error in type::uninitialized");
667   t->pc = npc;
668 }
669
670 /* Mark this type as now initialized.  */
671 static void
672 type_set_initialized (type *t, int npc)
673 {
674   if (npc != UNINIT && t->pc == npc && t->key == uninitialized_reference_type)
675     {
676       t->key = reference_type;
677       t->pc = UNINIT;
678     }
679 }
680
681 /* Mark this type as a particular return address.  */
682 static void type_set_return_address (type *t, int npc)
683 {
684   t->pc = npc;
685 }
686
687 /* Return true if this type and type OTHER are considered
688    mergeable for the purposes of state merging.  This is related
689    to subroutine handling.  For this purpose two types are
690    considered unmergeable if they are both return-addresses but
691    have different PCs.  */
692 static bool
693 type_state_mergeable_p (type *t1, type *t2)
694 {
695   return (t1->key != return_address_type
696           || t2->key != return_address_type
697           || t1->pc == t2->pc);
698 }
699
700 /* Return true if an object of type K can be assigned to a variable
701    of type T.  Handle various special cases too.  Might modify
702    T or K.  Note however that this does not perform numeric
703    promotion.  */
704 static bool 
705 types_compatible (type *t, type *k)
706 {
707   /* Any type is compatible with the unsuitable type.  */
708   if (k->key == unsuitable_type)
709     return true;
710
711   if (t->key < reference_type || k->key < reference_type)
712     return t->key == k->key;
713
714   /* The `null' type is convertible to any initialized reference
715      type.  */
716   if (t->key == null_type)
717     return k->key != uninitialized_reference_type;
718   if (k->key == null_type)
719     return t->key != uninitialized_reference_type;
720
721   /* A special case for a generic reference.  */
722   if (t->klass == NULL)
723     return true;
724   if (k->klass == NULL)
725     verify_fail ("programmer error in type::compatible");
726
727   /* Handle the special 'EITHER' case, which is only used in a
728      special case of 'putfield'.  Note that we only need to handle
729      this on the LHS of a check.  */
730   if (! type_initialized (t) && t->pc == EITHER)
731     {
732       /* If the RHS is uninitialized, it must be an uninitialized
733          'this'.  */
734       if (! type_initialized (k) && k->pc != SELF)
735         return false;
736     }
737   else if (type_initialized (t) != type_initialized (k))
738     {
739       /* An initialized type and an uninitialized type are not
740          otherwise compatible.  */
741       return false;
742     }
743   else
744     {
745       /* Two uninitialized objects are compatible if either:
746        * The PCs are identical, or
747        * One PC is UNINIT.  */
748       if (type_initialized (t))
749         {
750           if (t->pc != k->pc && t->pc != UNINIT && k->pc != UNINIT)
751             return false;
752         }
753     }
754
755   return ref_compatible (t->klass, k->klass);
756 }
757
758 /* Return true if two types are equal.  Only valid for reference
759    types.  */
760 static bool
761 types_equal (type *t1, type *t2)
762 {
763   if ((t1->key != reference_type && t1->key != uninitialized_reference_type)
764       || (t2->key != reference_type
765           && t2->key != uninitialized_reference_type))
766     return false;
767   /* Only single-ref types are allowed.  */
768   if (t1->klass->ref_next || t2->klass->ref_next)
769     return false;
770   return refs_equal (t1->klass, t2->klass);
771 }
772
773 static bool
774 type_isvoid (type *t)
775 {
776   return t->key == void_type;
777 }
778
779 static bool
780 type_iswide (type *t)
781 {
782   return t->key == long_type || t->key == double_type;
783 }
784
785 /* Return number of stack or local variable slots taken by this type.  */  
786 static int
787 type_depth (type *t)
788 {
789   return type_iswide (t) ? 2 : 1;
790 }
791
792 static bool
793 type_isarray (type *t)
794 {
795   /* We treat null_type as not an array.  This is ok based on the
796      current uses of this method.  */
797   if (t->key == reference_type)
798     return ref_isarray (t->klass);
799   return false;
800 }
801
802 static bool
803 type_isnull (type *t)
804 {
805   return t->key == null_type;
806 }
807
808 static bool
809 type_isinterface (type *t)
810 {
811   if (t->key != reference_type)
812     return false;
813   return ref_isinterface (t->klass);
814 }
815
816 static bool
817 type_isabstract (type *t)
818 {
819   if (t->key != reference_type)
820     return false;
821   return ref_isabstract (t->klass);
822 }
823
824 /* Return the element type of an array.  */
825 static type
826 type_array_element (type *t)
827 {
828   type et;
829   vfy_jclass k;
830
831   if (t->key != reference_type)
832     verify_fail ("programmer error in type::element_type()");
833
834   k = vfy_get_component_type (ref_getclass (t->klass));
835   if (vfy_is_primitive (k))
836     init_type_from_tag (&et, get_type_val_for_primtype (k));
837   else
838     init_type_from_class (&et, k);
839   return et;
840 }
841
842 /* Return the array type corresponding to an initialized
843    reference.  We could expand this to work for other kinds of
844    types, but currently we don't need to.  */
845 static type 
846 type_to_array (type *t)
847 {
848   type at;
849   vfy_jclass k;
850
851   if (t->key != reference_type)
852     verify_fail ("internal error in type::to_array()");
853
854   k = ref_getclass (t->klass);
855   init_type_from_class (&at, vfy_get_array_class (k));
856   return at;
857 }
858
859 static bool
860 type_isreference (type *t)
861 {
862   return t->key >= reference_type;
863 }
864
865 static int
866 type_get_pc (type *t)
867 {
868   return t->pc;
869 }
870
871 bool
872 type_initialized (type *t)
873 {
874   return t->key == reference_type || t->key == null_type;
875 }
876
877 #if 0
878 static bool
879 type_isresolved (type *t)
880 {
881   return (t->key == reference_type
882           || t->key == null_type
883           || t->key == uninitialized_reference_type);
884 }
885 #endif
886
887 static void
888 type_verify_dimensions (type *t, int ndims)
889 {
890   /* The way this is written, we don't need to check isarray().  */
891   if (t->key != reference_type)
892     verify_fail ("internal error in verify_dimensions:"
893                            " not a reference type");
894
895   if (ref_count_dimensions (t->klass) < ndims)
896     verify_fail ("array type has fewer dimensions"
897                            " than required");
898 }
899
900 /* Merge OLD_TYPE into this.  On error throw exception.  Return
901    true if the merge caused a type change.  */
902 static bool
903 merge_types (type *t, type *old_type, bool local_semantics)
904 {
905   bool changed = false;
906   bool refo = type_isreference (old_type);
907   bool refn = type_isreference (t);
908   if (refo && refn)
909     {
910       if (old_type->key == null_type)
911         ;
912       else if (t->key == null_type)
913         {
914           *t = *old_type;
915           changed = true;
916         }
917       else if (type_initialized (t) != type_initialized (old_type))
918         verify_fail ("merging initialized and uninitialized types");
919       else
920         {
921           ref_intersection *merged;
922           if (! type_initialized (t))
923             {
924               if (t->pc == UNINIT)
925                 t->pc = old_type->pc;
926               else if (old_type->pc == UNINIT)
927                 ;
928               else if (t->pc != old_type->pc)
929                 verify_fail ("merging different uninitialized types");
930             }
931
932           merged = merge_refs (old_type->klass, t->klass);
933           if (merged != t->klass)
934             {
935               t->klass = merged;
936               changed = true;
937             }
938         }
939     }
940   else if (refo || refn || t->key != old_type->key)
941     {
942       if (local_semantics)
943         {
944           /* If we already have an `unsuitable' type, then we
945              don't need to change again.  */
946           if (t->key != unsuitable_type)
947             {
948               t->key = unsuitable_type;
949               changed = true;
950             }
951         }
952       else
953         verify_fail ("unmergeable type");
954     }
955   return changed;
956 }
957
958 #ifdef VERIFY_DEBUG
959 static void 
960 type_print (type *t)
961 {
962   char c = '?';
963   switch (t->key)
964     {
965     case boolean_type: c = 'Z'; break;
966     case byte_type: c = 'B'; break;
967     case char_type: c = 'C'; break;
968     case short_type: c = 'S'; break;
969     case int_type: c = 'I'; break;
970     case long_type: c = 'J'; break;
971     case float_type: c = 'F'; break;
972     case double_type: c = 'D'; break;
973     case void_type: c = 'V'; break;
974     case unsuitable_type: c = '-'; break;
975     case return_address_type: c = 'r'; break;
976     case continuation_type: c = '+'; break;
977     case reference_type: c = 'L'; break;
978     case null_type: c = '@'; break;
979     case uninitialized_reference_type: c = 'U'; break;
980     }
981   debug_print ("%c", c);
982 }
983 #endif /* VERIFY_DEBUG */
984
985 /* This class holds all the state information we need for a given
986    location. */
987 struct state
988 {
989   /* The current top of the stack, in terms of slots.  */
990   int stacktop;
991   /* The current depth of the stack.  This will be larger than
992      STACKTOP when wide types are on the stack.  */
993   int stackdepth;
994   /* The stack.  */
995   type *stack;
996   /* The local variables.  */
997   type *locals;
998   /* We keep track of the type of `this' specially.  This is used to
999      ensure that an instance initializer invokes another initializer
1000      on `this' before returning.  We must keep track of this
1001      specially because otherwise we might be confused by code which
1002      assigns to locals[0] (overwriting `this') and then returns
1003      without really initializing.  */
1004   type this_type;
1005
1006   /* The PC for this state.  This is only valid on states which are
1007      permanently attached to a given PC.  For an object like
1008      `current_state', which is used transiently, this has no
1009      meaning.  */
1010   int pc;
1011   /* We keep a linked list of all states requiring reverification.
1012      If this is the special value INVALID_STATE then this state is
1013      not on the list.  NULL marks the end of the linked list.  */
1014   state *next;
1015 };
1016
1017 /* NO_NEXT is the PC value meaning that a new state must be
1018    acquired from the verification list.  */
1019 #define NO_NEXT -1
1020
1021 #if 0
1022 static void
1023 init_state (state *s)
1024 {
1025   s->stack = NULL;
1026   s->locals = NULL;
1027   s->next = INVALID_STATE;  
1028 }
1029 #endif
1030
1031 static void
1032 init_state_with_stack (state *s, int max_stack, int max_locals)
1033 {
1034   int i;
1035   s->stacktop = 0;
1036   s->stackdepth = 0;
1037   s->stack = (type *) vfy_alloc (max_stack * sizeof (type));
1038   for (i = 0; i < max_stack; ++i)
1039     init_type_from_tag (&s->stack[i], unsuitable_type);
1040   s->locals = (type *) vfy_alloc (max_locals * sizeof (type));
1041   for (i = 0; i < max_locals; ++i)
1042     init_type_from_tag (&s->locals[i], unsuitable_type);
1043   init_type_from_tag (&s->this_type, unsuitable_type);
1044   s->pc = NO_NEXT;
1045   s->next = INVALID_STATE;
1046 }
1047
1048 static void
1049 copy_state (state *s, state *copy, int max_stack, int max_locals)
1050 {
1051   int i;
1052   s->stacktop = copy->stacktop;
1053   s->stackdepth = copy->stackdepth;
1054   for (i = 0; i < max_stack; ++i)
1055     s->stack[i] = copy->stack[i];
1056   for (i = 0; i < max_locals; ++i)
1057     s->locals[i] = copy->locals[i];
1058
1059   s->this_type = copy->this_type;
1060   /* Don't modify `next' or `pc'. */
1061 }
1062
1063 static void
1064 copy_state_with_stack (state *s, state *orig, int max_stack, int max_locals)
1065 {
1066   init_state_with_stack (s, max_stack, max_locals);
1067   copy_state (s, orig, max_stack, max_locals);
1068 }
1069
1070 /* Allocate a new state, copying ORIG. */
1071 static state *
1072 make_state_copy (state *orig, int max_stack, int max_locals)
1073 {
1074   state *s = vfy_alloc (sizeof (state));
1075   copy_state_with_stack (s, orig, max_stack, max_locals);
1076   return s;
1077 }
1078
1079 static state *
1080 make_state (int max_stack, int max_locals)
1081 {
1082   state *s = vfy_alloc (sizeof (state));
1083   init_state_with_stack (s, max_stack, max_locals);
1084   return s;
1085 }
1086
1087 #if 0
1088 static void
1089 free_state (state *s)
1090 {
1091   if (s->stack != NULL)
1092     vfy_free (s->stack);
1093   if (s->locals != NULL)
1094     vfy_free (s->locals);
1095 }
1096 #endif
1097
1098 #if 0
1099     void *operator new[] (size_t bytes)
1100     {
1101       return vfy_alloc (bytes);
1102     }
1103
1104     void operator delete[] (void *mem)
1105     {
1106       vfy_free (mem);
1107     }
1108
1109     void *operator new (size_t bytes)
1110     {
1111       return vfy_alloc (bytes);
1112     }
1113
1114     void operator delete (void *mem)
1115     {
1116       vfy_free (mem);
1117     }
1118 #endif
1119
1120 /* Modify this state to reflect entry to an exception handler.  */
1121 static void
1122 state_set_exception (state *s, type *t, int max_stack)
1123 {
1124   int i;
1125   s->stackdepth = 1;
1126   s->stacktop = 1;
1127   s->stack[0] = *t;
1128   for (i = s->stacktop; i < max_stack; ++i)
1129     init_type_from_tag (&s->stack[i], unsuitable_type);
1130 }
1131
1132 static int
1133 state_get_pc (state *s)
1134 {
1135   return s->pc;
1136 }
1137
1138 #if 0
1139 static void
1140 set_pc (state *s, int npc)
1141 {
1142   s->pc = npc;
1143 }
1144 #endif
1145
1146 /* Merge STATE_OLD into this state.  Destructively modifies this
1147    state.  Returns true if the new state was in fact changed.
1148    Will throw an exception if the states are not mergeable.  */
1149 static bool
1150 merge_states (state *s, state *state_old, int max_locals)
1151 {
1152   int i;
1153   bool changed = false;
1154
1155   /* Special handling for `this'.  If one or the other is
1156      uninitialized, then the merge is uninitialized.  */
1157   if (type_initialized (&s->this_type))
1158     s->this_type = state_old->this_type;
1159
1160   /* Merge stacks.  */
1161   if (state_old->stacktop != s->stacktop)  /* FIXME stackdepth instead?  */
1162     verify_fail ("stack sizes differ");
1163   for (i = 0; i < state_old->stacktop; ++i)
1164     {
1165       if (merge_types (&s->stack[i], &state_old->stack[i], false))
1166         changed = true;
1167     }
1168
1169   /* Merge local variables.  */
1170   for (i = 0; i < max_locals; ++i)
1171     {
1172       if (merge_types (&s->locals[i], &state_old->locals[i], true))
1173         changed = true;
1174     }
1175
1176   return changed;
1177 }
1178
1179 /* Ensure that `this' has been initialized.  */
1180 static void
1181 state_check_this_initialized (state *s)
1182 {
1183   if (type_isreference (&s->this_type) && ! type_initialized (&s->this_type))
1184     verify_fail ("`this' is uninitialized");
1185 }
1186
1187 /* Set type of `this'.  */
1188 static void
1189 state_set_this_type (state *s, type *k)
1190 {
1191   s->this_type = *k;
1192 }
1193
1194 /* Mark each `new'd object we know of that was allocated at PC as
1195    initialized.  */
1196 static void
1197 state_set_initialized (state *s, int pc, int max_locals)
1198 {
1199   int i;
1200   for (i = 0; i < s->stacktop; ++i)
1201     type_set_initialized (&s->stack[i], pc);
1202   for (i = 0; i < max_locals; ++i)
1203     type_set_initialized (&s->locals[i], pc);
1204   type_set_initialized (&s->this_type, pc);
1205 }
1206
1207 /* This tests to see whether two states can be considered "merge
1208    compatible".  If both states have a return-address in the same
1209    slot, and the return addresses are different, then they are not
1210    compatible and we must not try to merge them.  */
1211 static bool
1212 state_mergeable_p (state *s, state *other, int max_locals)
1213
1214 {
1215   int i;
1216
1217   /* This is tricky: if the stack sizes differ, then not only are
1218      these not mergeable, but in fact we should give an error, as
1219      we've found two execution paths that reach a branch target
1220      with different stack depths.  FIXME stackdepth instead?  */
1221   if (s->stacktop != other->stacktop)
1222     verify_fail ("stack sizes differ");
1223
1224   for (i = 0; i < s->stacktop; ++i)
1225     if (! type_state_mergeable_p (&s->stack[i], &other->stack[i]))
1226       return false;
1227   for (i = 0; i < max_locals; ++i)
1228     if (! type_state_mergeable_p (&s->locals[i], &other->locals[i]))
1229       return false;
1230   return true;
1231 }
1232
1233 static void
1234 state_reverify (state *s)
1235 {
1236   if (s->next == INVALID_STATE)
1237     {
1238       s->next = vfr->next_verify_state;
1239       vfr->next_verify_state = s;
1240     }
1241 }
1242
1243 #ifdef VERIFY_DEBUG
1244 static void 
1245 debug_print_state (state *s, const char *leader, int pc, int max_stack, 
1246                    int max_locals)
1247 {
1248   int i;
1249   debug_print ("%s [%4d]:   [stack] ", leader, pc);
1250   for (i = 0; i < s->stacktop; ++i)
1251     type_print (&s->stack[i]);
1252   for (; i < max_stack; ++i)
1253     debug_print (".");
1254   debug_print ("    [local] ");
1255   for (i = 0; i < max_locals; ++i)
1256     type_print (&s->locals[i]);
1257   debug_print (" | %p\n", s);
1258 }
1259 #else
1260 static void
1261 debug_print_state (state *s ATTRIBUTE_UNUSED, 
1262                    const char *leader ATTRIBUTE_UNUSED, 
1263                    int pc ATTRIBUTE_UNUSED, int max_stack ATTRIBUTE_UNUSED, 
1264                    int max_locals ATTRIBUTE_UNUSED)
1265 {
1266 }
1267 #endif /* VERIFY_DEBUG */
1268
1269 static type
1270 pop_raw (void)
1271 {
1272   type r;
1273   state *s = vfr->current_state;
1274   if (s->stacktop <= 0)
1275     verify_fail ("stack empty");
1276   r = s->stack[--s->stacktop];
1277   s->stackdepth -= type_depth (&r);
1278   if (s->stackdepth < 0)
1279     verify_fail_pc ("stack empty", vfr->start_PC);
1280   return r;
1281 }
1282
1283 static type
1284 pop32 (void)
1285 {
1286   type r = pop_raw ();
1287   if (type_iswide (&r))
1288     verify_fail ("narrow pop of wide type");
1289   return r;
1290 }
1291
1292 static type
1293 vfy_pop_type_t (type match)
1294 {
1295   type t;
1296   vfy_promote_type (&match);
1297   t = pop_raw ();
1298   if (! types_compatible (&match, &t))
1299     verify_fail ("incompatible type on stack");
1300   return t;
1301 }
1302
1303 static type
1304 vfy_pop_type (type_val match)
1305 {
1306   type t = make_type (match);
1307   return vfy_pop_type_t (t);
1308 }
1309
1310 #define pop_type vfy_pop_type
1311 #define pop_type_t vfy_pop_type_t
1312
1313 /* Pop a reference which is guaranteed to be initialized.  MATCH
1314    doesn't have to be a reference type; in this case this acts like
1315    pop_type.  */
1316 static type
1317 pop_init_ref_t (type match)
1318 {
1319   type t = pop_raw ();
1320   if (type_isreference (&t) && ! type_initialized (&t))
1321     verify_fail ("initialized reference required");
1322   else if (! types_compatible (&match, &t))
1323     verify_fail ("incompatible type on stack");
1324   return t;
1325 }
1326
1327 static type
1328 pop_init_ref (type_val match)
1329 {
1330   type t = make_type (match);
1331   return pop_init_ref_t (t);
1332 }
1333
1334 /* Pop a reference type or a return address.  */
1335 static type
1336 pop_ref_or_return (void)
1337 {
1338   type t = pop_raw ();
1339   if (! type_isreference (&t) && t.key != return_address_type)
1340     verify_fail ("expected reference or return address on stack");
1341   return t;
1342 }
1343
1344 static void
1345 vfy_push_type_t (type t)
1346 {
1347   int depth;
1348   state *s = vfr->current_state;
1349   /* If T is a numeric type like short, promote it to int.  */
1350   promote_type (&t);
1351
1352   depth = type_depth (&t);
1353
1354   if (s->stackdepth + depth > vfr->current_method->max_stack)
1355     verify_fail ("stack overflow");
1356   s->stack[s->stacktop++] = t;
1357   s->stackdepth += depth;
1358 }
1359
1360 static void
1361 vfy_push_type (type_val tval)
1362 {
1363   type t = make_type (tval);
1364   vfy_push_type_t (t);
1365 }
1366
1367 #define push_type vfy_push_type
1368 #define push_type_t vfy_push_type_t
1369
1370 static void
1371 set_variable (int index, type t)
1372 {
1373   int depth;
1374   state *s = vfr->current_state;
1375   /* If T is a numeric type like short, promote it to int.  */
1376   promote_type (&t);
1377
1378   depth = type_depth (&t);
1379   if (index > vfr->current_method->max_locals - depth)
1380     verify_fail ("invalid local variable");
1381   s->locals[index] = t;
1382
1383   if (depth == 2)
1384     init_type_from_tag (&s->locals[index + 1], continuation_type);
1385   if (index > 0 && type_iswide (&s->locals[index - 1]))
1386     init_type_from_tag (&s->locals[index - 1], unsuitable_type);
1387 }
1388
1389 static type
1390 get_variable_t (int index, type *t)
1391 {
1392   state *s = vfr->current_state;
1393   int depth = type_depth (t);
1394   if (index > vfr->current_method->max_locals - depth)
1395     verify_fail ("invalid local variable");
1396   if (! types_compatible (t, &s->locals[index]))
1397     verify_fail ("incompatible type in local variable");
1398   if (depth == 2)
1399     {
1400       type cont = make_type (continuation_type);
1401       if (! types_compatible (&s->locals[index + 1], &cont))
1402         verify_fail ("invalid local variable");
1403     }
1404   return s->locals[index];
1405 }
1406
1407 static type
1408 get_variable (int index, type_val v)
1409 {
1410   type t = make_type (v);
1411   return get_variable_t (index, &t);
1412 }
1413
1414 /* Make sure ARRAY is an array type and that its elements are
1415    compatible with type ELEMENT.  Returns the actual element type.  */
1416 static type
1417 require_array_type_t (type array, type element)
1418 {
1419   type t;
1420   /* An odd case.  Here we just pretend that everything went ok.  If
1421      the requested element type is some kind of reference, return
1422      the null type instead.  */
1423   if (type_isnull (&array))
1424     return type_isreference (&element) ? make_type (null_type) : element;
1425
1426   if (! type_isarray (&array))
1427     verify_fail ("array required");
1428
1429   t = type_array_element (&array);
1430   if (! types_compatible (&element, &t))
1431     {
1432       /* Special case for byte arrays, which must also be boolean
1433          arrays.  */
1434       bool ok = true;
1435       if (element.key == byte_type)
1436         {
1437           type e2 = make_type (boolean_type);
1438           ok = types_compatible (&e2, &t);
1439         }
1440       if (! ok)
1441         verify_fail ("incompatible array element type");
1442     }
1443
1444   /* Return T and not ELEMENT, because T might be specialized.  */
1445   return t;
1446 }
1447
1448 static type
1449 require_array_type (type array, type_val element)
1450 {
1451   type t = make_type (element);
1452   return require_array_type_t (array, t);
1453 }
1454
1455 static jint
1456 get_byte (void)
1457 {
1458   if (vfr->PC >= vfr->current_method->code_length)
1459     verify_fail ("premature end of bytecode");
1460   return (jint) vfr->bytecode[vfr->PC++] & 0xff;
1461 }
1462
1463 static jint
1464 get_ushort (void)
1465 {
1466   jint b1 = get_byte ();
1467   jint b2 = get_byte ();
1468   return (jint) ((b1 << 8) | b2) & 0xffff;
1469 }
1470
1471 static jint
1472 get_short (void)
1473 {
1474   signed char b1 = (signed char) get_byte ();
1475   jint b2 = get_byte ();
1476   jshort s = (b1 << 8) | b2;
1477   return (jint) s;
1478 }
1479
1480 static jint
1481 get_int (void)
1482 {
1483   jint b1 = get_byte ();
1484   jint b2 = get_byte ();
1485   jint b3 = get_byte ();
1486   jint b4 = get_byte ();
1487   jword result = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1488   /* In the compiler, 'jint' might have more than 32 bits, so we must
1489      sign extend.  */
1490   return WORD_TO_INT (result);
1491 }
1492
1493 static int
1494 compute_jump (int offset)
1495 {
1496   int npc = vfr->start_PC + offset;
1497   if (npc < 0 || npc >= vfr->current_method->code_length)
1498     verify_fail_pc ("branch out of range", vfr->start_PC);
1499   return npc;
1500 }
1501
1502 /* Add a new state to the state list at NPC.  */
1503 static state *
1504 add_new_state (int npc, state *old_state)
1505 {
1506   state_list *nlink;
1507   vfy_method *current_method = vfr->current_method;
1508   state *new_state = make_state_copy (old_state, current_method->max_stack,
1509                                       current_method->max_locals);
1510   debug_print ("== New state in add_new_state\n");
1511   debug_print_state (new_state, "New", npc, current_method->max_stack,
1512                     current_method->max_locals);
1513
1514   nlink = vfy_alloc (sizeof (state_list));
1515   nlink->val = new_state;
1516   nlink->next = vfr->states[npc];
1517   vfr->states[npc] = nlink;
1518   new_state->pc = npc;
1519   return new_state;
1520 }
1521
1522 /* Merge the indicated state into the state at the branch target and
1523    schedule a new PC if there is a change.  NPC is the PC of the
1524    branch target, and FROM_STATE is the state at the source of the
1525    branch.  This method returns true if the destination state
1526    changed and requires reverification, false otherwise.  */
1527 static void
1528 merge_into (int npc, state *from_state)
1529 {
1530   /* Iterate over all target states and merge our state into each,
1531      if applicable.  FIXME one improvement we could make here is
1532      "state destruction".  Merging a new state into an existing one
1533      might cause a return_address_type to be merged to
1534      unsuitable_type.  In this case the resulting state may now be
1535      mergeable with other states currently held in parallel at this
1536      location.  So in this situation we could pairwise compare and
1537      reduce the number of parallel states.  */
1538   state_list *iter;
1539   bool applicable = false;
1540   for (iter = vfr->states[npc]; iter != NULL; iter = iter->next)
1541     {
1542       state *new_state = iter->val;
1543       vfy_method *current_method = vfr->current_method;
1544
1545       if (state_mergeable_p (new_state, from_state,
1546                                         current_method->max_locals))
1547         {
1548           bool changed;
1549           applicable = true;
1550
1551           debug_print ("== Merge states in merge_into\n");
1552           debug_print_state (from_state, "Frm", vfr->start_PC, current_method->max_stack,
1553                              current_method->max_locals);
1554           debug_print_state (new_state, " To", npc, current_method->max_stack,
1555                             current_method->max_locals);
1556           changed = merge_states (new_state, from_state,
1557                                   current_method->max_locals);
1558           debug_print_state (new_state, "New", npc, current_method->max_stack,
1559                             current_method->max_locals);
1560
1561           if (changed)
1562             state_reverify (new_state);
1563         }
1564     }
1565
1566   if (! applicable)
1567     {
1568       /* Either we don't yet have a state at NPC, or we have a
1569          return-address type that is in conflict with all existing
1570          state.  So, we need to create a new entry.  */
1571       state *new_state = add_new_state (npc, from_state);
1572       /* A new state added in this way must always be reverified.  */
1573       state_reverify (new_state);
1574     }
1575 }
1576
1577 static void
1578 push_jump (int offset)
1579 {
1580   int npc = compute_jump (offset);
1581   /* According to the JVM Spec, we need to check for uninitialized
1582      objects here.  However, this does not actually affect type
1583      safety, and the Eclipse java compiler generates code that
1584      violates this constraint.  */
1585   merge_into (npc, vfr->current_state);
1586 }
1587
1588 static void
1589 push_exception_jump (type t, int pc)
1590 {
1591   state s;
1592   /* According to the JVM Spec, we need to check for uninitialized
1593      objects here.  However, this does not actually affect type
1594      safety, and the Eclipse java compiler generates code that
1595      violates this constraint.  */
1596   copy_state_with_stack (&s, vfr->current_state, 
1597                          vfr->current_method->max_stack,
1598                          vfr->current_method->max_locals);
1599   if (vfr->current_method->max_stack < 1)
1600     verify_fail ("stack overflow at exception handler");
1601   state_set_exception (&s, &t, vfr->current_method->max_stack);
1602   merge_into (pc, &s);
1603   /* FIXME: leak.. need free_state or GC */
1604 }
1605
1606 static state *
1607 pop_jump (void)
1608 {
1609   state *new_state = vfr->next_verify_state;
1610   if (new_state == INVALID_STATE)
1611     verify_fail ("programmer error in pop_jump");
1612   if (new_state != NULL)
1613     {
1614       vfr->next_verify_state = new_state->next;
1615       new_state->next = INVALID_STATE;
1616     }
1617   return new_state;
1618 }
1619
1620 static void
1621 invalidate_pc (void)
1622 {
1623   vfr->PC = NO_NEXT;
1624 }
1625
1626 static void
1627 note_branch_target (int pc)
1628 {
1629   /* Don't check `pc <= PC', because we've advanced PC after
1630      fetching the target and we haven't yet checked the next
1631      instruction.  */
1632   if (pc < vfr->PC && ! (vfr->flags[pc] & FLAG_INSN_START))
1633     verify_fail_pc ("branch not to instruction start", vfr->start_PC);
1634   vfr->flags[pc] |= FLAG_BRANCH_TARGET;
1635 }
1636
1637 static void
1638 skip_padding (void)
1639 {
1640   while ((vfr->PC % 4) > 0)
1641     if (get_byte () != 0)
1642       verify_fail ("found nonzero padding byte");
1643 }
1644
1645 /* Do the work for a `ret' instruction.  INDEX is the index into the
1646    local variables.  */
1647 static void
1648 handle_ret_insn (int index)
1649 {
1650   type ret = make_type (return_address_type);
1651   type ret_addr = get_variable_t (index, &ret);
1652   /* It would be nice if we could do this.  However, the JVM Spec
1653      doesn't say that this is what happens.  It is implied that
1654      reusing a return address is invalid, but there's no actual
1655      prohibition against it.  */
1656   /* set_variable (index, unsuitable_type); */
1657
1658   int npc = type_get_pc (&ret_addr);
1659   /* We might be returning to a `jsr' that is at the end of the
1660      bytecode.  This is ok if we never return from the called
1661      subroutine, but if we see this here it is an error.  */
1662   if (npc >= vfr->current_method->code_length)
1663     verify_fail ("fell off end");
1664
1665   /* According to the JVM Spec, we need to check for uninitialized
1666      objects here.  However, this does not actually affect type
1667      safety, and the Eclipse java compiler generates code that
1668      violates this constraint.  */
1669   merge_into (npc, vfr->current_state);
1670   invalidate_pc ();
1671 }
1672
1673 static void handle_jsr_insn (int offset)
1674 {
1675   type ret_addr;
1676   int npc = compute_jump (offset);
1677
1678   /* According to the JVM Spec, we need to check for uninitialized
1679      objects here.  However, this does not actually affect type
1680      safety, and the Eclipse java compiler generates code that
1681      violates this constraint.  */
1682
1683   /* Modify our state as appropriate for entry into a subroutine.  */
1684   ret_addr = make_type (return_address_type);
1685   type_set_return_address (&ret_addr, vfr->PC);
1686   vfy_push_type_t (ret_addr);
1687   merge_into (npc, vfr->current_state);
1688   invalidate_pc ();
1689 }
1690
1691 static vfy_jclass
1692 construct_primitive_array_type (type_val prim)
1693 {
1694   vfy_jclass k = NULL;
1695   switch (prim)
1696     {
1697     case boolean_type:
1698     case char_type:
1699     case float_type:
1700     case double_type:
1701     case byte_type:
1702     case short_type:
1703     case int_type:
1704     case long_type:
1705       k = vfy_get_primitive_type ((int) prim);
1706       break;
1707
1708     /* These aren't used here but we call them out to avoid
1709        warnings.  */
1710     case void_type:
1711     case unsuitable_type:
1712     case return_address_type:
1713     case continuation_type:
1714     case reference_type:
1715     case null_type:
1716     case uninitialized_reference_type:
1717     default:
1718       verify_fail ("unknown type in construct_primitive_array_type");
1719     }
1720   k = vfy_get_array_class (k);
1721   return k;
1722 }
1723
1724 /* This pass computes the location of branch targets and also
1725    instruction starts.  */
1726 static void
1727 branch_prepass (void)
1728 {
1729   int i, pc;
1730   vfr->flags = (char *) vfy_alloc (vfr->current_method->code_length);
1731
1732   for (i = 0; i < vfr->current_method->code_length; ++i)
1733     vfr->flags[i] = 0;
1734
1735   vfr->PC = 0;
1736   while (vfr->PC < vfr->current_method->code_length)
1737     {
1738       java_opcode opcode;
1739       /* Set `start_PC' early so that error checking can have the
1740          correct value.  */
1741       vfr->start_PC = vfr->PC;
1742       vfr->flags[vfr->PC] |= FLAG_INSN_START;
1743
1744       opcode = (java_opcode) vfr->bytecode[vfr->PC++];
1745       switch (opcode)
1746         {
1747         case op_nop:
1748         case op_aconst_null:
1749         case op_iconst_m1:
1750         case op_iconst_0:
1751         case op_iconst_1:
1752         case op_iconst_2:
1753         case op_iconst_3:
1754         case op_iconst_4:
1755         case op_iconst_5:
1756         case op_lconst_0:
1757         case op_lconst_1:
1758         case op_fconst_0:
1759         case op_fconst_1:
1760         case op_fconst_2:
1761         case op_dconst_0:
1762         case op_dconst_1:
1763         case op_iload_0:
1764         case op_iload_1:
1765         case op_iload_2:
1766         case op_iload_3:
1767         case op_lload_0:
1768         case op_lload_1:
1769         case op_lload_2:
1770         case op_lload_3:
1771         case op_fload_0:
1772         case op_fload_1:
1773         case op_fload_2:
1774         case op_fload_3:
1775         case op_dload_0:
1776         case op_dload_1:
1777         case op_dload_2:
1778         case op_dload_3:
1779         case op_aload_0:
1780         case op_aload_1:
1781         case op_aload_2:
1782         case op_aload_3:
1783         case op_iaload:
1784         case op_laload:
1785         case op_faload:
1786         case op_daload:
1787         case op_aaload:
1788         case op_baload:
1789         case op_caload:
1790         case op_saload:
1791         case op_istore_0:
1792         case op_istore_1:
1793         case op_istore_2:
1794         case op_istore_3:
1795         case op_lstore_0:
1796         case op_lstore_1:
1797         case op_lstore_2:
1798         case op_lstore_3:
1799         case op_fstore_0:
1800         case op_fstore_1:
1801         case op_fstore_2:
1802         case op_fstore_3:
1803         case op_dstore_0:
1804         case op_dstore_1:
1805         case op_dstore_2:
1806         case op_dstore_3:
1807         case op_astore_0:
1808         case op_astore_1:
1809         case op_astore_2:
1810         case op_astore_3:
1811         case op_iastore:
1812         case op_lastore:
1813         case op_fastore:
1814         case op_dastore:
1815         case op_aastore:
1816         case op_bastore:
1817         case op_castore:
1818         case op_sastore:
1819         case op_pop:
1820         case op_pop2:
1821         case op_dup:
1822         case op_dup_x1:
1823         case op_dup_x2:
1824         case op_dup2:
1825         case op_dup2_x1:
1826         case op_dup2_x2:
1827         case op_swap:
1828         case op_iadd:
1829         case op_isub:
1830         case op_imul:
1831         case op_idiv:
1832         case op_irem:
1833         case op_ishl:
1834         case op_ishr:
1835         case op_iushr:
1836         case op_iand:
1837         case op_ior:
1838         case op_ixor:
1839         case op_ladd:
1840         case op_lsub:
1841         case op_lmul:
1842         case op_ldiv:
1843         case op_lrem:
1844         case op_lshl:
1845         case op_lshr:
1846         case op_lushr:
1847         case op_land:
1848         case op_lor:
1849         case op_lxor:
1850         case op_fadd:
1851         case op_fsub:
1852         case op_fmul:
1853         case op_fdiv:
1854         case op_frem:
1855         case op_dadd:
1856         case op_dsub:
1857         case op_dmul:
1858         case op_ddiv:
1859         case op_drem:
1860         case op_ineg:
1861         case op_i2b:
1862         case op_i2c:
1863         case op_i2s:
1864         case op_lneg:
1865         case op_fneg:
1866         case op_dneg:
1867         case op_i2l:
1868         case op_i2f:
1869         case op_i2d:
1870         case op_l2i:
1871         case op_l2f:
1872         case op_l2d:
1873         case op_f2i:
1874         case op_f2l:
1875         case op_f2d:
1876         case op_d2i:
1877         case op_d2l:
1878         case op_d2f:
1879         case op_lcmp:
1880         case op_fcmpl:
1881         case op_fcmpg:
1882         case op_dcmpl:
1883         case op_dcmpg:
1884         case op_monitorenter:
1885         case op_monitorexit:
1886         case op_ireturn:
1887         case op_lreturn:
1888         case op_freturn:
1889         case op_dreturn:
1890         case op_areturn:
1891         case op_return:
1892         case op_athrow:
1893         case op_arraylength:
1894           break;
1895
1896         case op_bipush:
1897         case op_ldc:
1898         case op_iload:
1899         case op_lload:
1900         case op_fload:
1901         case op_dload:
1902         case op_aload:
1903         case op_istore:
1904         case op_lstore:
1905         case op_fstore:
1906         case op_dstore:
1907         case op_astore:
1908         case op_ret:
1909         case op_newarray:
1910           get_byte ();
1911           break;
1912
1913         case op_iinc:
1914         case op_sipush:
1915         case op_ldc_w:
1916         case op_ldc2_w:
1917         case op_getstatic:
1918         case op_getfield:
1919         case op_putfield:
1920         case op_putstatic:
1921         case op_new:
1922         case op_anewarray:
1923         case op_instanceof:
1924         case op_checkcast:
1925         case op_invokespecial:
1926         case op_invokestatic:
1927         case op_invokevirtual:
1928           get_short ();
1929           break;
1930
1931         case op_multianewarray:
1932           get_short ();
1933           get_byte ();
1934           break;
1935
1936         case op_jsr:
1937         case op_ifeq:
1938         case op_ifne:
1939         case op_iflt:
1940         case op_ifge:
1941         case op_ifgt:
1942         case op_ifle:
1943         case op_if_icmpeq:
1944         case op_if_icmpne:
1945         case op_if_icmplt:
1946         case op_if_icmpge:
1947         case op_if_icmpgt:
1948         case op_if_icmple:
1949         case op_if_acmpeq:
1950         case op_if_acmpne:
1951         case op_ifnull:
1952         case op_ifnonnull:
1953         case op_goto:
1954           note_branch_target (compute_jump (get_short ()));
1955           break;
1956
1957         case op_tableswitch:
1958           {
1959             jint low, hi;
1960             skip_padding ();
1961             note_branch_target (compute_jump (get_int ()));
1962             low = get_int ();
1963             hi = get_int ();
1964             if (low > hi)
1965               verify_fail_pc ("invalid tableswitch", vfr->start_PC);
1966             for (i = low; i <= hi; ++i)
1967               note_branch_target (compute_jump (get_int ()));
1968           }
1969           break;
1970
1971         case op_lookupswitch:
1972           {
1973             int npairs;
1974             skip_padding ();
1975             note_branch_target (compute_jump (get_int ()));
1976             npairs = get_int ();
1977             if (npairs < 0)
1978               verify_fail_pc ("too few pairs in lookupswitch", vfr->start_PC);
1979             while (npairs-- > 0)
1980               {
1981                 get_int ();
1982                 note_branch_target (compute_jump (get_int ()));
1983               }
1984           }
1985           break;
1986
1987         case op_invokeinterface:
1988           get_short ();
1989           get_byte ();
1990           get_byte ();
1991           break;
1992
1993         case op_wide:
1994           {
1995             opcode = (java_opcode) get_byte ();
1996             get_short ();
1997             if (opcode == op_iinc)
1998               get_short ();
1999           }
2000           break;
2001
2002         case op_jsr_w:
2003         case op_goto_w:
2004           note_branch_target (compute_jump (get_int ()));
2005           break;
2006
2007 #if 0
2008         /* These are unused here, but we call them out explicitly
2009            so that -Wswitch-enum doesn't complain.  */
2010         case op_putfield_1:
2011         case op_putfield_2:
2012         case op_putfield_4:
2013         case op_putfield_8:
2014         case op_putfield_a:
2015         case op_putstatic_1:
2016         case op_putstatic_2:
2017         case op_putstatic_4:
2018         case op_putstatic_8:
2019         case op_putstatic_a:
2020         case op_getfield_1:
2021         case op_getfield_2s:
2022         case op_getfield_2u:
2023         case op_getfield_4:
2024         case op_getfield_8:
2025         case op_getfield_a:
2026         case op_getstatic_1:
2027         case op_getstatic_2s:
2028         case op_getstatic_2u:
2029         case op_getstatic_4:
2030         case op_getstatic_8:
2031         case op_getstatic_a:
2032 #endif /* VFY_FAST_OPCODES  */
2033         default:
2034           verify_fail_pc ("unrecognized instruction in branch_prepass",
2035                           vfr->start_PC);
2036         }
2037
2038       /* See if any previous branch tried to branch to the middle of
2039          this instruction.  */
2040       for (pc = vfr->start_PC + 1; pc < vfr->PC; ++pc)
2041         {
2042           if ((vfr->flags[pc] & FLAG_BRANCH_TARGET))
2043             verify_fail_pc ("branch to middle of instruction", pc);
2044         }
2045     }
2046
2047   /* Verify exception handlers.  */
2048   for (i = 0; i < vfr->current_method->exc_count; ++i)
2049     {
2050       int handler, start, end, htype;
2051       vfy_get_exception (vfr->exception, i, &handler, &start, &end, &htype);
2052       if (! (vfr->flags[handler] & FLAG_INSN_START))
2053         verify_fail_pc ("exception handler not at instruction start", 
2054                         handler);
2055       if (! (vfr->flags[start] & FLAG_INSN_START))
2056         verify_fail_pc ("exception start not at instruction start", start);
2057       if (end != vfr->current_method->code_length
2058           && ! (vfr->flags[end] & FLAG_INSN_START))
2059         verify_fail_pc ("exception end not at instruction start", end);
2060
2061       vfr->flags[handler] |= FLAG_BRANCH_TARGET;
2062     }
2063 }
2064
2065 static void
2066 check_pool_index (int index)
2067 {
2068   if (index < 0 || index >= vfy_get_constants_size (vfr->current_class))
2069     verify_fail_pc ("constant pool index out of range", vfr->start_PC);
2070 }
2071
2072 static type
2073 check_class_constant (int index)
2074 {
2075   type t;
2076   vfy_constants *pool;
2077
2078   check_pool_index (index);
2079   pool = vfy_get_constants (vfr->current_class);
2080   if (vfy_tag (pool, index) == JV_CONSTANT_ResolvedClass)
2081     init_type_from_class (&t, vfy_get_pool_class (pool, index));
2082   else if (vfy_tag (pool, index) == JV_CONSTANT_Class)
2083     init_type_from_string (&t, vfy_get_pool_string (pool, index));
2084   else
2085     verify_fail_pc ("expected class constant", vfr->start_PC);      
2086   return t;
2087 }
2088
2089 static type
2090 check_constant (int index)
2091 {
2092   type t;
2093   vfy_constants *pool;
2094
2095   check_pool_index (index);
2096   pool = vfy_get_constants (vfr->current_class);
2097   if (vfy_tag (pool, index) == JV_CONSTANT_ResolvedString
2098       || vfy_tag (pool, index) == JV_CONSTANT_String)
2099     init_type_from_class (&t, vfy_string_type ());
2100   else if (vfy_tag (pool, index) == JV_CONSTANT_Integer)
2101     init_type_from_tag (&t, int_type);
2102   else if (vfy_tag (pool, index) == JV_CONSTANT_Float)
2103     init_type_from_tag (&t, float_type);
2104   else
2105     verify_fail_pc ("String, int, or float constant expected", vfr->start_PC);
2106   return t;
2107 }
2108
2109 static type
2110 check_wide_constant (int index)
2111 {
2112   type t;
2113   vfy_constants *pool;
2114
2115   check_pool_index (index);
2116   pool = vfy_get_constants (vfr->current_class);
2117   if (vfy_tag (pool, index) == JV_CONSTANT_Long)
2118     init_type_from_tag (&t, long_type);
2119   else if (vfy_tag (pool, index) == JV_CONSTANT_Double)
2120     init_type_from_tag (&t, double_type);
2121   else
2122     verify_fail_pc ("long or double constant expected", vfr->start_PC);
2123   return t;
2124 }
2125
2126 /* Helper for both field and method.  These are laid out the same in
2127    the constant pool.  */
2128 static type
2129 handle_field_or_method (int index, int expected,
2130                         vfy_string *name, vfy_string *fmtype)
2131 {
2132   vfy_uint_16 class_index, name_and_type_index;
2133   vfy_uint_16 name_index, desc_index;
2134   vfy_constants *pool;
2135
2136   check_pool_index (index);
2137   pool = vfy_get_constants (vfr->current_class);
2138   if (vfy_tag (pool, index) != expected)
2139     verify_fail_pc ("didn't see expected constant", vfr->start_PC);
2140   /* Once we know we have a Fieldref or Methodref we assume that it
2141      is correctly laid out in the constant pool.  I think the code
2142      in defineclass.cc guarantees this.  */
2143   vfy_load_indexes (pool, index, &class_index, &name_and_type_index);
2144   vfy_load_indexes (pool, name_and_type_index, &name_index, &desc_index);
2145
2146   *name = vfy_get_pool_string (pool, name_index);
2147   *fmtype = vfy_get_pool_string (pool, desc_index);
2148
2149   return check_class_constant (class_index);
2150 }
2151
2152 /* Return field's type, compute class' type if requested.  If
2153    PUTFIELD is true, use the special 'putfield' semantics.  */
2154 static type
2155 check_field_constant (int index, type *class_type, bool putfield)
2156 {
2157   vfy_string name, field_type;
2158   const char *typec;
2159   int len;
2160   type t;
2161
2162   type ct = handle_field_or_method (index,
2163                                     JV_CONSTANT_Fieldref,
2164                                     &name, &field_type);
2165   if (class_type)
2166     *class_type = ct;
2167   typec = vfy_string_bytes (field_type);
2168   len = vfy_string_length (field_type);
2169   if (typec[0] == '[' || typec[0] == 'L')
2170     init_type_from_string (&t, field_type);
2171   else
2172     init_type_from_tag (&t, get_type_val_for_signature (typec[0]));
2173
2174   /* We have an obscure special case here: we can use `putfield' on a
2175      field declared in this class, even if `this' has not yet been
2176      initialized.  */
2177   if (putfield
2178       && ! type_initialized (&vfr->current_state->this_type)
2179       && vfr->current_state->this_type.pc == SELF
2180       && types_equal (&vfr->current_state->this_type, &ct)
2181       && vfy_class_has_field (vfr->current_class, name, field_type))
2182     /* Note that we don't actually know whether we're going to match
2183        against 'this' or some other object of the same type.  So,
2184        here we set things up so that it doesn't matter.  This relies
2185        on knowing what our caller is up to.  */
2186     type_set_uninitialized (class_type, EITHER);
2187
2188   return t;
2189 }
2190
2191 static type
2192 check_method_constant (int index, bool is_interface,
2193                             vfy_string *method_name,
2194                             vfy_string *method_signature)
2195 {
2196   return handle_field_or_method (index,
2197                                  (is_interface
2198                                   ? JV_CONSTANT_InterfaceMethodref
2199                                   : JV_CONSTANT_Methodref),
2200                                  method_name, method_signature);
2201 }
2202
2203 static char *
2204 get_one_type (char *p, type *t)
2205 {
2206   const char *start = p;
2207   vfy_jclass k;
2208   type_val rt;
2209   char v;
2210
2211   int arraycount = 0;
2212   while (*p == '[')
2213     {
2214       ++arraycount;
2215       ++p;
2216     }
2217
2218   v = *p++;
2219
2220   if (v == 'L')
2221     {
2222       vfy_string name;
2223       while (*p != ';')
2224         ++p;
2225       ++p;
2226       name = vfy_get_string (start, p - start);
2227       *t = make_type_from_string (name);
2228       return p;
2229     }
2230
2231   /* Casting to jchar here is ok since we are looking at an ASCII
2232      character.  */
2233   rt = get_type_val_for_signature (v);
2234
2235   if (arraycount == 0)
2236     {
2237       /* Callers of this function eventually push their arguments on
2238          the stack.  So, promote them here.  */
2239       type new_t = make_type (rt);
2240       vfy_promote_type (&new_t);
2241       *t = new_t;
2242       return p;
2243     }
2244
2245   k = construct_primitive_array_type (rt);
2246   while (--arraycount > 0)
2247     k = vfy_get_array_class (k);
2248   *t = make_type_from_class (k);
2249   return p;
2250 }
2251
2252 static void 
2253 compute_argument_types (vfy_string signature, type *types)
2254 {
2255   int i;
2256   char *p = (char *) vfy_string_bytes (signature);
2257
2258   /* Skip `('.  */
2259   ++p;
2260
2261   i = 0;
2262   while (*p != ')')
2263     p = get_one_type (p, &types[i++]);
2264 }
2265
2266 static type
2267 compute_return_type (vfy_string signature)
2268 {
2269   char *p = (char *) vfy_string_bytes (signature);
2270   type t;
2271   while (*p != ')')
2272     ++p;
2273   ++p;
2274   get_one_type (p, &t);
2275   return t;
2276 }
2277
2278 static void
2279 check_return_type (type onstack)
2280 {
2281   type rt = compute_return_type (vfy_get_signature (vfr->current_method));
2282   if (! types_compatible (&rt, &onstack))
2283     verify_fail ("incompatible return type");
2284 }
2285
2286 /* Initialize the stack for the new method.  Returns true if this
2287    method is an instance initializer.  */
2288 static bool
2289 initialize_stack (void)
2290 {
2291   int arg_count, i;
2292   int var = 0;
2293   bool is_init = vfy_strings_equal (vfy_get_method_name (vfr->current_method),
2294                                     vfy_init_name());
2295   bool is_clinit = vfy_strings_equal (vfy_get_method_name (vfr->current_method),
2296                                       vfy_clinit_name());
2297
2298   if (! vfy_is_static (vfr->current_method))
2299     {
2300       type kurr = make_type_from_class (vfr->current_class);
2301       if (is_init)
2302         {
2303           type_set_uninitialized (&kurr, SELF);
2304           is_init = true;
2305         }
2306       else if (is_clinit)
2307         verify_fail ("<clinit> method must be static");
2308       set_variable (0, kurr);
2309       state_set_this_type (vfr->current_state, &kurr);
2310       ++var;
2311     }
2312   else
2313     {
2314       if (is_init)
2315         verify_fail ("<init> method must be non-static");
2316     }
2317
2318   /* We have to handle wide arguments specially here.  */
2319   arg_count = vfy_count_arguments (vfy_get_signature (vfr->current_method));
2320   {
2321     type *arg_types = (type *) vfy_alloc (arg_count * sizeof (type));
2322     compute_argument_types (vfy_get_signature (vfr->current_method), arg_types);
2323     for (i = 0; i < arg_count; ++i)
2324       {
2325         set_variable (var, arg_types[i]);
2326         ++var;
2327         if (type_iswide (&arg_types[i]))
2328           ++var;
2329       }
2330     vfy_free (arg_types);
2331   }
2332
2333   return is_init;
2334 }
2335
2336 static void
2337 verify_instructions_0 (void)
2338 {
2339   int i;
2340   bool this_is_init;
2341
2342   vfr->current_state = make_state (vfr->current_method->max_stack,
2343                                    vfr->current_method->max_locals);
2344
2345   vfr->PC = 0;
2346   vfr->start_PC = 0;
2347
2348   /*  True if we are verifying an instance initializer.  */
2349   this_is_init = initialize_stack ();
2350
2351   vfr->states = (state_list **) vfy_alloc (sizeof (state_list *)
2352                                       * vfr->current_method->code_length);
2353
2354   for (i = 0; i < vfr->current_method->code_length; ++i)
2355     vfr->states[i] = NULL;
2356
2357   vfr->next_verify_state = NULL;
2358
2359   while (true)
2360     {
2361       java_opcode opcode;
2362
2363       /* If the PC was invalidated, get a new one from the work list.  */
2364       if (vfr->PC == NO_NEXT)
2365         {
2366           state *new_state = pop_jump ();
2367           /* If it is null, we're done.  */
2368           if (new_state == NULL)
2369             break;
2370
2371           vfr->PC = state_get_pc (new_state);
2372           debug_print ("== State pop from pending list\n");
2373           /* Set up the current state.  */
2374           copy_state (vfr->current_state, new_state, 
2375             vfr->current_method->max_stack, vfr->current_method->max_locals);
2376         }
2377       else
2378         {
2379           /* We only have to do this checking in the situation where
2380              control flow falls through from the previous
2381              instruction.  Otherwise merging is done at the time we
2382              push the branch.  */
2383           if (vfr->states[vfr->PC] != NULL)
2384             {
2385               /* We've already visited this instruction.  So merge
2386                  the states together.  It is simplest, but not most
2387                  efficient, to just always invalidate the PC here.  */
2388               merge_into (vfr->PC, vfr->current_state);
2389               invalidate_pc ();
2390               continue;
2391             }
2392         }
2393
2394       /* Control can't fall off the end of the bytecode.  We need to
2395          check this in both cases, not just the fall-through case,
2396          because we don't check to see whether a `jsr' appears at
2397          the end of the bytecode until we process a `ret'.  */
2398       if (vfr->PC >= vfr->current_method->code_length)
2399         verify_fail ("fell off end");
2400       vfr->flags[vfr->PC] |= FLAG_INSN_SEEN;
2401
2402       /* We only have to keep saved state at branch targets.  If
2403          we're at a branch target and the state here hasn't been set
2404          yet, we set it now.  You might notice that `ret' targets
2405          won't necessarily have FLAG_BRANCH_TARGET set.  This
2406          doesn't matter, since those states will be filled in by
2407          merge_into.  */
2408       /* Note that other parts of the compiler assume that there is a
2409          label with a type map at PC=0.  */
2410       if (vfr->states[vfr->PC] == NULL
2411           && (vfr->PC == 0 || (vfr->flags[vfr->PC] & FLAG_BRANCH_TARGET) != 0))
2412         add_new_state (vfr->PC, vfr->current_state);
2413
2414       /* Set this before handling exceptions so that debug output is
2415          sane.  */
2416       vfr->start_PC = vfr->PC;
2417
2418       /* Update states for all active exception handlers.  Ordinarily
2419          there are not many exception handlers.  So we simply run
2420          through them all.  */
2421       for (i = 0; i < vfr->current_method->exc_count; ++i)
2422         {
2423           int hpc, start, end, htype;
2424           vfy_get_exception (vfr->exception, i, &hpc, &start, &end, &htype);
2425           if (vfr->PC >= start && vfr->PC < end)
2426             {
2427               type handler = make_type_from_class (vfy_throwable_type ());
2428               if (htype != 0)
2429                 handler = check_class_constant (htype);
2430               push_exception_jump (handler, hpc);
2431             }
2432         }
2433
2434
2435       debug_print_state (vfr->current_state, "   ", vfr->PC, 
2436                          vfr->current_method->max_stack,
2437                          vfr->current_method->max_locals);
2438       opcode = (java_opcode) vfr->bytecode[vfr->PC++];
2439       switch (opcode)
2440         {
2441         case op_nop:
2442           break;
2443
2444         case op_aconst_null:
2445           push_type (null_type);
2446           break;
2447
2448         case op_iconst_m1:
2449         case op_iconst_0:
2450         case op_iconst_1:
2451         case op_iconst_2:
2452         case op_iconst_3:
2453         case op_iconst_4:
2454         case op_iconst_5:
2455           push_type (int_type);
2456           break;
2457
2458         case op_lconst_0:
2459         case op_lconst_1:
2460           push_type (long_type);
2461           break;
2462
2463         case op_fconst_0:
2464         case op_fconst_1:
2465         case op_fconst_2:
2466           push_type (float_type);
2467           break;
2468
2469         case op_dconst_0:
2470         case op_dconst_1:
2471           push_type (double_type);
2472           break;
2473
2474         case op_bipush:
2475           get_byte ();
2476           push_type (int_type);
2477           break;
2478
2479         case op_sipush:
2480           get_short ();
2481           push_type (int_type);
2482           break;
2483
2484         case op_ldc:
2485           push_type_t (check_constant (get_byte ()));
2486           break;
2487         case op_ldc_w:
2488           push_type_t (check_constant (get_ushort ()));
2489           break;
2490         case op_ldc2_w:
2491           push_type_t (check_wide_constant (get_ushort ()));
2492           break;
2493
2494         case op_iload:
2495           push_type_t (get_variable (get_byte (), int_type));
2496           break;
2497         case op_lload:
2498           push_type_t (get_variable (get_byte (), long_type));
2499           break;
2500         case op_fload:
2501           push_type_t (get_variable (get_byte (), float_type));
2502           break;
2503         case op_dload:
2504           push_type_t (get_variable (get_byte (), double_type));
2505           break;
2506         case op_aload:
2507           push_type_t (get_variable (get_byte (), reference_type));
2508           break;
2509
2510         case op_iload_0:
2511         case op_iload_1:
2512         case op_iload_2:
2513         case op_iload_3:
2514           push_type_t (get_variable (opcode - op_iload_0, int_type));
2515           break;
2516         case op_lload_0:
2517         case op_lload_1:
2518         case op_lload_2:
2519         case op_lload_3:
2520           push_type_t (get_variable (opcode - op_lload_0, long_type));
2521           break;
2522         case op_fload_0:
2523         case op_fload_1:
2524         case op_fload_2:
2525         case op_fload_3:
2526           push_type_t (get_variable (opcode - op_fload_0, float_type));
2527           break;
2528         case op_dload_0:
2529         case op_dload_1:
2530         case op_dload_2:
2531         case op_dload_3:
2532           push_type_t (get_variable (opcode - op_dload_0, double_type));
2533           break;
2534         case op_aload_0:
2535         case op_aload_1:
2536         case op_aload_2:
2537         case op_aload_3:
2538           push_type_t (get_variable (opcode - op_aload_0, reference_type));
2539           break;
2540         case op_iaload:
2541           pop_type (int_type);
2542           push_type_t (require_array_type (pop_init_ref (reference_type),
2543                                          int_type));
2544           break;
2545         case op_laload:
2546           pop_type (int_type);
2547           push_type_t (require_array_type (pop_init_ref (reference_type),
2548                                          long_type));
2549           break;
2550         case op_faload:
2551           pop_type (int_type);
2552           push_type_t (require_array_type (pop_init_ref (reference_type),
2553                                          float_type));
2554           break;
2555         case op_daload:
2556           pop_type (int_type);
2557           push_type_t (require_array_type (pop_init_ref (reference_type),
2558                                          double_type));
2559           break;
2560         case op_aaload:
2561           pop_type (int_type);
2562           push_type_t (require_array_type (pop_init_ref (reference_type),
2563                                          reference_type));
2564           break;
2565         case op_baload:
2566           pop_type (int_type);
2567           require_array_type (pop_init_ref (reference_type), byte_type);
2568           push_type (int_type);
2569           break;
2570         case op_caload:
2571           pop_type (int_type);
2572           require_array_type (pop_init_ref (reference_type), char_type);
2573           push_type (int_type);
2574           break;
2575         case op_saload:
2576           pop_type (int_type);
2577           require_array_type (pop_init_ref (reference_type), short_type);
2578           push_type (int_type);
2579           break;
2580         case op_istore:
2581           set_variable (get_byte (), pop_type (int_type));
2582           break;
2583         case op_lstore:
2584           set_variable (get_byte (), pop_type (long_type));
2585           break;
2586         case op_fstore:
2587           set_variable (get_byte (), pop_type (float_type));
2588           break;
2589         case op_dstore:
2590           set_variable (get_byte (), pop_type (double_type));
2591           break;
2592         case op_astore:
2593           set_variable (get_byte (), pop_ref_or_return ());
2594           break;
2595         case op_istore_0:
2596         case op_istore_1:
2597         case op_istore_2:
2598         case op_istore_3:
2599           set_variable (opcode - op_istore_0, pop_type (int_type));
2600           break;
2601         case op_lstore_0:
2602         case op_lstore_1:
2603         case op_lstore_2:
2604         case op_lstore_3:
2605           set_variable (opcode - op_lstore_0, pop_type (long_type));
2606           break;
2607         case op_fstore_0:
2608         case op_fstore_1:
2609         case op_fstore_2:
2610         case op_fstore_3:
2611           set_variable (opcode - op_fstore_0, pop_type (float_type));
2612           break;
2613         case op_dstore_0:
2614         case op_dstore_1:
2615         case op_dstore_2:
2616         case op_dstore_3:
2617           set_variable (opcode - op_dstore_0, pop_type (double_type));
2618           break;
2619         case op_astore_0:
2620         case op_astore_1:
2621         case op_astore_2:
2622         case op_astore_3:
2623           set_variable (opcode - op_astore_0, pop_ref_or_return ());
2624           break;
2625         case op_iastore:
2626           pop_type (int_type);
2627           pop_type (int_type);
2628           require_array_type (pop_init_ref (reference_type), int_type);
2629           break;
2630         case op_lastore:
2631           pop_type (long_type);
2632           pop_type (int_type);
2633           require_array_type (pop_init_ref (reference_type), long_type);
2634           break;
2635         case op_fastore:
2636           pop_type (float_type);
2637           pop_type (int_type);
2638           require_array_type (pop_init_ref (reference_type), float_type);
2639           break;
2640         case op_dastore:
2641           pop_type (double_type);
2642           pop_type (int_type);
2643           require_array_type (pop_init_ref (reference_type), double_type);
2644           break;
2645         case op_aastore:
2646           pop_type (reference_type);
2647           pop_type (int_type);
2648           require_array_type (pop_init_ref (reference_type), reference_type);
2649           break;
2650         case op_bastore:
2651           pop_type (int_type);
2652           pop_type (int_type);
2653           require_array_type (pop_init_ref (reference_type), byte_type);
2654           break;
2655         case op_castore:
2656           pop_type (int_type);
2657           pop_type (int_type);
2658           require_array_type (pop_init_ref (reference_type), char_type);
2659           break;
2660         case op_sastore:
2661           pop_type (int_type);
2662           pop_type (int_type);
2663           require_array_type (pop_init_ref (reference_type), short_type);
2664           break;
2665         case op_pop:
2666           pop32 ();
2667           break;
2668         case op_pop2:
2669           {
2670             type t = pop_raw ();
2671             if (! type_iswide (&t))
2672               pop32 ();
2673           }
2674           break;
2675         case op_dup:
2676           {
2677             type t = pop32 ();
2678             push_type_t (t);
2679             push_type_t (t);
2680           }
2681           break;
2682         case op_dup_x1:
2683           {
2684             type t1 = pop32 ();
2685             type t2 = pop32 ();
2686             push_type_t (t1);
2687             push_type_t (t2);
2688             push_type_t (t1);
2689           }
2690           break;
2691         case op_dup_x2:
2692           {
2693             type t1 = pop32 ();
2694             type t2 = pop_raw ();
2695             if (! type_iswide (&t2))
2696               {
2697                 type t3 = pop32 ();
2698                 push_type_t (t1);
2699                 push_type_t (t3);
2700               }
2701             else
2702               push_type_t (t1);
2703             push_type_t (t2);
2704             push_type_t (t1);
2705           }
2706           break;
2707         case op_dup2:
2708           {
2709             type t = pop_raw ();
2710             if (! type_iswide (&t))
2711               {
2712                 type t2 = pop32 ();
2713                 push_type_t (t2);
2714                 push_type_t (t);
2715                 push_type_t (t2);
2716               }
2717             else
2718               push_type_t (t);
2719             push_type_t (t);
2720           }
2721           break;
2722         case op_dup2_x1:
2723           {
2724             type t1 = pop_raw ();
2725             type t2 = pop32 ();
2726             if (! type_iswide (&t1))
2727               {
2728                 type t3 = pop32 ();
2729                 push_type_t (t2);
2730                 push_type_t (t1);
2731                 push_type_t (t3);
2732               }
2733             else
2734               push_type_t (t1);
2735             push_type_t (t2);
2736             push_type_t (t1);
2737           }
2738           break;
2739         case op_dup2_x2:
2740           {
2741             type t1 = pop_raw ();
2742             if (type_iswide (&t1))
2743               {
2744                 type t2 = pop_raw ();
2745                 if (type_iswide (&t2))
2746                   {
2747                     push_type_t (t1);
2748                     push_type_t (t2);
2749                   }
2750                 else
2751                   {
2752                     type t3 = pop32 ();
2753                     push_type_t (t1);
2754                     push_type_t (t3);
2755                     push_type_t (t2);
2756                   }
2757                 push_type_t (t1);
2758               }
2759             else
2760               {
2761                 type t2 = pop32 ();
2762                 type t3 = pop_raw ();
2763                 if (type_iswide (&t3))
2764                   {
2765                     push_type_t (t2);
2766                     push_type_t (t1);
2767                   }
2768                 else
2769                   {
2770                     type t4 = pop32 ();
2771                     push_type_t (t2);
2772                     push_type_t (t1);
2773                     push_type_t (t4);
2774                   }
2775                 push_type_t (t3);
2776                 push_type_t (t2);
2777                 push_type_t (t1);
2778               }
2779           }
2780           break;
2781         case op_swap:
2782           {
2783             type t1 = pop32 ();
2784             type t2 = pop32 ();
2785             push_type_t (t1);
2786             push_type_t (t2);
2787           }
2788           break;
2789         case op_iadd:
2790         case op_isub:
2791         case op_imul:
2792         case op_idiv:
2793         case op_irem:
2794         case op_ishl:
2795         case op_ishr:
2796         case op_iushr:
2797         case op_iand:
2798         case op_ior:
2799         case op_ixor:
2800           pop_type (int_type);
2801           push_type_t (pop_type (int_type));
2802           break;
2803         case op_ladd:
2804         case op_lsub:
2805         case op_lmul:
2806         case op_ldiv:
2807         case op_lrem:
2808         case op_land:
2809         case op_lor:
2810         case op_lxor:
2811           pop_type (long_type);
2812           push_type_t (pop_type (long_type));
2813           break;
2814         case op_lshl:
2815         case op_lshr:
2816         case op_lushr:
2817           pop_type (int_type);
2818           push_type_t (pop_type (long_type));
2819           break;
2820         case op_fadd:
2821         case op_fsub:
2822         case op_fmul:
2823         case op_fdiv:
2824         case op_frem:
2825           pop_type (float_type);
2826           push_type_t (pop_type (float_type));
2827           break;
2828         case op_dadd:
2829         case op_dsub:
2830         case op_dmul:
2831         case op_ddiv:
2832         case op_drem:
2833           pop_type (double_type);
2834           push_type_t (pop_type (double_type));
2835           break;
2836         case op_ineg:
2837         case op_i2b:
2838         case op_i2c:
2839         case op_i2s:
2840           push_type_t (pop_type (int_type));
2841           break;
2842         case op_lneg:
2843           push_type_t (pop_type (long_type));
2844           break;
2845         case op_fneg:
2846           push_type_t (pop_type (float_type));
2847           break;
2848         case op_dneg:
2849           push_type_t (pop_type (double_type));
2850           break;
2851         case op_iinc:
2852           get_variable (get_byte (), int_type);
2853           get_byte ();
2854           break;
2855         case op_i2l:
2856           pop_type (int_type);
2857           push_type (long_type);
2858           break;
2859         case op_i2f:
2860           pop_type (int_type);
2861           push_type (float_type);
2862           break;
2863         case op_i2d:
2864           pop_type (int_type);
2865           push_type (double_type);
2866           break;
2867         case op_l2i:
2868           pop_type (long_type);
2869           push_type (int_type);
2870           break;
2871         case op_l2f:
2872           pop_type (long_type);
2873           push_type (float_type);
2874           break;
2875         case op_l2d:
2876           pop_type (long_type);
2877           push_type (double_type);
2878           break;
2879         case op_f2i:
2880           pop_type (float_type);
2881           push_type (int_type);
2882           break;
2883         case op_f2l:
2884           pop_type (float_type);
2885           push_type (long_type);
2886           break;
2887         case op_f2d:
2888           pop_type (float_type);
2889           push_type (double_type);
2890           break;
2891         case op_d2i:
2892           pop_type (double_type);
2893           push_type (int_type);
2894           break;
2895         case op_d2l:
2896           pop_type (double_type);
2897           push_type (long_type);
2898           break;
2899         case op_d2f:
2900           pop_type (double_type);
2901           push_type (float_type);
2902           break;
2903         case op_lcmp:
2904           pop_type (long_type);
2905           pop_type (long_type);
2906           push_type (int_type);
2907           break;
2908         case op_fcmpl:
2909         case op_fcmpg:
2910           pop_type (float_type);
2911           pop_type (float_type);
2912           push_type (int_type);
2913           break;
2914         case op_dcmpl:
2915         case op_dcmpg:
2916           pop_type (double_type);
2917           pop_type (double_type);
2918           push_type (int_type);
2919           break;
2920         case op_ifeq:
2921         case op_ifne:
2922         case op_iflt:
2923         case op_ifge:
2924         case op_ifgt:
2925         case op_ifle:
2926           pop_type (int_type);
2927           push_jump (get_short ());
2928           break;
2929         case op_if_icmpeq:
2930         case op_if_icmpne:
2931         case op_if_icmplt:
2932         case op_if_icmpge:
2933         case op_if_icmpgt:
2934         case op_if_icmple:
2935           pop_type (int_type);
2936           pop_type (int_type);
2937           push_jump (get_short ());
2938           break;
2939         case op_if_acmpeq:
2940         case op_if_acmpne:
2941           pop_type (reference_type);
2942           pop_type (reference_type);
2943           push_jump (get_short ());
2944           break;
2945         case op_goto:
2946           push_jump (get_short ());
2947           invalidate_pc ();
2948           break;
2949         case op_jsr:
2950           handle_jsr_insn (get_short ());
2951           break;
2952         case op_ret:
2953           handle_ret_insn (get_byte ());
2954           break;
2955         case op_tableswitch:
2956           {
2957             int i;
2958             jint low, high;
2959             pop_type (int_type);
2960             skip_padding ();
2961             push_jump (get_int ());
2962             low = get_int ();
2963             high = get_int ();
2964             /* Already checked LOW -vs- HIGH.  */
2965             for (i = low; i <= high; ++i)
2966               push_jump (get_int ());
2967             invalidate_pc ();
2968           }
2969           break;
2970
2971         case op_lookupswitch:
2972           {
2973             int i;
2974             jint npairs, lastkey;
2975
2976             pop_type (int_type);
2977             skip_padding ();
2978             push_jump (get_int ());
2979             npairs = get_int ();
2980             /* Already checked NPAIRS >= 0.  */
2981             lastkey = 0;
2982             for (i = 0; i < npairs; ++i)
2983               {
2984                 jint key = get_int ();
2985                 if (i > 0 && key <= lastkey)
2986                   verify_fail_pc ("lookupswitch pairs unsorted", vfr->start_PC);
2987                 lastkey = key;
2988                 push_jump (get_int ());
2989               }
2990             invalidate_pc ();
2991           }
2992           break;
2993         case op_ireturn:
2994           check_return_type (pop_type (int_type));
2995           invalidate_pc ();
2996           break;
2997         case op_lreturn:
2998           check_return_type (pop_type (long_type));
2999           invalidate_pc ();
3000           break;
3001         case op_freturn:
3002           check_return_type (pop_type (float_type));
3003           invalidate_pc ();
3004           break;
3005         case op_dreturn:
3006           check_return_type (pop_type (double_type));
3007           invalidate_pc ();
3008           break;
3009         case op_areturn:
3010           check_return_type (pop_init_ref (reference_type));
3011           invalidate_pc ();
3012           break;
3013         case op_return:
3014           /* We only need to check this when the return type is
3015              void, because all instance initializers return void.  */
3016           if (this_is_init)
3017             state_check_this_initialized (vfr->current_state);
3018           check_return_type (make_type (void_type));
3019           invalidate_pc ();
3020           break;
3021         case op_getstatic:
3022           push_type_t (check_field_constant (get_ushort (), NULL, false));
3023           break;
3024         case op_putstatic:
3025           pop_type_t (check_field_constant (get_ushort (), NULL, false));
3026           break;
3027         case op_getfield:
3028           {
3029             type klass;
3030             type field = check_field_constant (get_ushort (), &klass, false);
3031             pop_type_t (klass);
3032             push_type_t (field);
3033           }
3034           break;
3035         case op_putfield:
3036           {
3037             type klass;
3038             type field = check_field_constant (get_ushort (), &klass, true);
3039             pop_type_t (field);
3040             pop_type_t (klass);
3041           }
3042           break;
3043
3044         case op_invokevirtual:
3045         case op_invokespecial:
3046         case op_invokestatic:
3047         case op_invokeinterface:
3048           {
3049             vfy_string method_name, method_signature;
3050             const char *namec;
3051             int i, arg_count;
3052             type rt;
3053             bool is_init = false;
3054
3055             type class_type
3056               = check_method_constant (get_ushort (),
3057                                        opcode == op_invokeinterface,
3058                                        &method_name,
3059                                        &method_signature);
3060             /* NARGS is only used when we're processing
3061                invokeinterface.  It is simplest for us to compute it
3062                here and then verify it later.  */
3063             int nargs = 0;
3064             if (opcode == op_invokeinterface)
3065               {
3066                 nargs = get_byte ();
3067                 if (get_byte () != 0)
3068                   verify_fail ("invokeinterface dummy byte is wrong");
3069               }
3070
3071             namec = vfy_string_bytes (method_name);
3072
3073             if (vfy_strings_equal (method_name, vfy_init_name()))
3074               {
3075                 is_init = true;
3076                 if (opcode != op_invokespecial)
3077                   verify_fail ("can't invoke <init>");
3078               }
3079             else if (namec[0] == '<')
3080               verify_fail ("can't invoke method starting with `<'");
3081
3082             arg_count = vfy_count_arguments (method_signature);
3083             {
3084               /* Pop arguments and check types.  */
3085               type *arg_types = (type *) vfy_alloc (arg_count * sizeof (type));
3086
3087               compute_argument_types (method_signature, arg_types);
3088               for (i = arg_count - 1; i >= 0; --i)
3089                 {
3090                   /* This is only used for verifying the byte for
3091                      invokeinterface.  */
3092                   nargs -= type_depth (&arg_types[i]);
3093                   pop_init_ref_t (arg_types[i]);
3094                 }
3095
3096               vfy_free (arg_types);
3097             }
3098
3099             if (opcode == op_invokeinterface
3100                 && nargs != 1)
3101               verify_fail ("wrong argument count for invokeinterface");
3102
3103             if (opcode != op_invokestatic)
3104               {
3105                 type raw;
3106                 type t = class_type;
3107                 if (is_init)
3108                   {
3109                     /* In this case the PC doesn't matter.  */
3110                     type_set_uninitialized (&t, UNINIT);
3111                     /* FIXME: check to make sure that the <init>
3112                        call is to the right class.
3113                        It must either be super or an exact class
3114                        match.  */
3115                   }
3116                 raw = pop_raw ();
3117                 if (! types_compatible (&t, &raw))
3118                   verify_fail ("incompatible type on stack");
3119
3120                 if (is_init)              
3121                   state_set_initialized (vfr->current_state, 
3122                     type_get_pc (&raw), vfr->current_method->max_locals);
3123               }
3124
3125             rt = compute_return_type (method_signature);
3126             if (! type_isvoid (&rt))
3127               push_type_t (rt);
3128           }
3129           break;
3130
3131         case op_new:
3132           {
3133             type t = check_class_constant (get_ushort ());
3134             if (type_isarray (&t) || type_isinterface (&t)
3135                 || type_isabstract (&t))
3136               verify_fail ("type is array, interface, or abstract");
3137             type_set_uninitialized (&t, vfr->start_PC);
3138             push_type_t (t);
3139           }
3140           break;
3141
3142         case op_newarray:
3143           {
3144             int atype = get_byte ();
3145             type t;
3146             /* We intentionally have chosen constants to make this
3147                valid.  */
3148             if (atype < boolean_type || atype > long_type)
3149               verify_fail_pc ("type not primitive", vfr->start_PC);
3150             pop_type (int_type);
3151             init_type_from_class (&t, construct_primitive_array_type (atype));
3152             push_type_t (t);
3153           }
3154           break;
3155         case op_anewarray:
3156           {
3157             type t;
3158             pop_type (int_type);
3159             t = check_class_constant (get_ushort ());
3160             push_type_t (type_to_array (&t));
3161           }
3162           break;
3163         case op_arraylength:
3164           {
3165             type t = pop_init_ref (reference_type);
3166             if (! type_isarray (&t) && ! type_isnull (&t))
3167               verify_fail ("array type expected");
3168             push_type (int_type);
3169           }
3170           break;
3171         case op_athrow:
3172           pop_type_t (make_type_from_class (vfy_throwable_type ()));
3173           invalidate_pc ();
3174           break;
3175         case op_checkcast:
3176           pop_init_ref (reference_type);
3177           push_type_t (check_class_constant (get_ushort ()));
3178           break;
3179         case op_instanceof:
3180           pop_init_ref (reference_type);
3181           check_class_constant (get_ushort ());
3182           push_type (int_type);
3183           break;
3184         case op_monitorenter:
3185           pop_init_ref (reference_type);
3186           break;
3187         case op_monitorexit:
3188           pop_init_ref (reference_type);
3189           break;
3190         case op_wide:
3191           {
3192             switch (get_byte ())
3193               {
3194               case op_iload:
3195                 push_type_t (get_variable (get_ushort (), int_type));
3196                 break;
3197               case op_lload:
3198                 push_type_t (get_variable (get_ushort (), long_type));
3199                 break;
3200               case op_fload:
3201                 push_type_t (get_variable (get_ushort (), float_type));
3202                 break;
3203               case op_dload:
3204                 push_type_t (get_variable (get_ushort (), double_type));
3205                 break;
3206               case op_aload:
3207                 push_type_t (get_variable (get_ushort (), reference_type));
3208                 break;
3209               case op_istore:
3210                 set_variable (get_ushort (), pop_type (int_type));
3211                 break;
3212               case op_lstore:
3213                 set_variable (get_ushort (), pop_type (long_type));
3214                 break;
3215               case op_fstore:
3216                 set_variable (get_ushort (), pop_type (float_type));
3217                 break;
3218               case op_dstore:
3219                 set_variable (get_ushort (), pop_type (double_type));
3220                 break;
3221               case op_astore:
3222                 set_variable (get_ushort (), pop_init_ref (reference_type));
3223                 break;
3224               case op_ret:
3225                 handle_ret_insn (get_short ());
3226                 break;
3227               case op_iinc:
3228                 get_variable (get_ushort (), int_type);
3229                 get_short ();
3230                 break;
3231               default:
3232                 verify_fail_pc ("unrecognized wide instruction", vfr->start_PC);
3233               }
3234           }
3235           break;
3236         case op_multianewarray:
3237           {
3238             int i;
3239             type atype = check_class_constant (get_ushort ());
3240             int dim = get_byte ();
3241             if (dim < 1)
3242               verify_fail_pc ("too few dimensions to multianewarray", vfr->start_PC);
3243             type_verify_dimensions (&atype, dim);
3244             for (i = 0; i < dim; ++i)
3245               pop_type (int_type);
3246             push_type_t (atype);
3247           }
3248           break;
3249         case op_ifnull:
3250         case op_ifnonnull:
3251           pop_type (reference_type);
3252           push_jump (get_short ());
3253           break;
3254         case op_goto_w:
3255           push_jump (get_int ());
3256           invalidate_pc ();
3257           break;
3258         case op_jsr_w:
3259           handle_jsr_insn (get_int ());
3260           break;
3261
3262 #if 0
3263         /* These are unused here, but we call them out explicitly
3264            so that -Wswitch-enum doesn't complain.  */
3265         case op_putfield_1:
3266         case op_putfield_2:
3267         case op_putfield_4:
3268         case op_putfield_8:
3269         case op_putfield_a:
3270         case op_putstatic_1:
3271         case op_putstatic_2:
3272         case op_putstatic_4:
3273         case op_putstatic_8:
3274         case op_putstatic_a:
3275         case op_getfield_1:
3276         case op_getfield_2s:
3277         case op_getfield_2u:
3278         case op_getfield_4:
3279         case op_getfield_8:
3280         case op_getfield_a:
3281         case op_getstatic_1:
3282         case op_getstatic_2s:
3283         case op_getstatic_2u:
3284         case op_getstatic_4:
3285         case op_getstatic_8:
3286         case op_getstatic_a:
3287 #endif
3288         default:
3289           /* Unrecognized opcode.  */
3290           verify_fail_pc ("unrecognized instruction in verify_instructions_0",
3291                        vfr->start_PC);
3292         }
3293     }
3294 }
3295
3296 /* This turns a `type' into something suitable for use by the type map
3297    in the other parts of the compiler.  In particular, reference types
3298    are mapped to Object, primitive types are unchanged, and other
3299    types are mapped using special functions declared in verify.h.  */
3300 static vfy_jclass
3301 collapse_type (type *t)
3302 {
3303   switch (t->key)
3304     {
3305     case void_type:
3306     case boolean_type:
3307     case char_type:
3308     case float_type:
3309     case double_type:
3310     case byte_type:
3311     case short_type:
3312     case int_type:
3313     case long_type:
3314       return vfy_get_primitive_type (t->key);
3315
3316     case unsuitable_type:
3317     case continuation_type:
3318       return vfy_unsuitable_type ();
3319
3320     case return_address_type:
3321       return vfy_return_address_type ();
3322
3323     case null_type:
3324       return vfy_null_type ();
3325
3326     case reference_type:
3327     case uninitialized_reference_type:
3328       return vfy_object_type ();
3329     }
3330
3331   abort ();
3332 }
3333
3334 static void
3335 verify_instructions (void)
3336 {
3337   int i;
3338
3339   branch_prepass ();
3340   verify_instructions_0 ();
3341
3342   /* Now tell the rest of the compiler about the types we've found.  */
3343   for (i = 0; i < vfr->current_method->code_length; ++i)
3344     {
3345       int j, slot;
3346       struct state *curr;
3347
3348       if ((vfr->flags[i] & FLAG_INSN_SEEN) != 0)
3349         vfy_note_instruction_seen (i);
3350
3351       if (! vfr->states[i])
3352         continue;
3353
3354       curr = vfr->states[i]->val;
3355       vfy_note_stack_depth (vfr->current_method, i, curr->stackdepth);
3356
3357       /* Tell the compiler about each local variable.  */
3358       for (j = 0; j < vfr->current_method->max_locals; ++j)
3359         vfy_note_local_type (vfr->current_method, i, j,
3360                              collapse_type (&curr->locals[j]));
3361       /* Tell the compiler about each stack slot.  */
3362       for (slot = j = 0; j < curr->stacktop; ++j, ++slot)
3363         {
3364           vfy_note_stack_type (vfr->current_method, i, slot,
3365                                collapse_type (&curr->stack[j]));
3366           if (type_iswide (&curr->stack[j]))
3367             {
3368               ++slot;
3369               vfy_note_stack_type (vfr->current_method, i, slot,
3370                                    vfy_unsuitable_type ());
3371             }
3372         }
3373       if (slot != curr->stackdepth)
3374         abort ();
3375     }
3376 }
3377
3378 #if 0
3379 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3380 {
3381   /* We just print the text as utf-8.  This is just for debugging
3382      anyway.  */
3383   debug_print ("--------------------------------\n");
3384   debug_print ("-- Verifying method `%s'\n", m->self->name->chars());
3385
3386 }
3387 #endif
3388
3389 static void
3390 make_verifier_context (vfy_method *m)
3391 {
3392   vfr = (verifier_context *) vfy_alloc (sizeof (struct verifier_context));
3393
3394   vfr->current_method = m;
3395   vfr->bytecode = vfy_get_bytecode (m);
3396   vfr->exception = vfy_get_exceptions (m);
3397   vfr->current_class = m->defining_class;
3398
3399   vfr->states = NULL;
3400   vfr->flags = NULL;
3401   vfr->utf8_list = NULL;
3402   vfr->isect_list = NULL;
3403 }
3404
3405 static void
3406 free_verifier_context (void)
3407 {
3408   vfy_string_list *utf8_list;
3409   ref_intersection *isect_list;
3410
3411   if (vfr->flags)
3412     vfy_free (vfr->flags);
3413
3414   utf8_list = vfr->utf8_list;
3415   while (utf8_list != NULL)
3416     {
3417       vfy_string_list *n = utf8_list->next;
3418       vfy_free (utf8_list);
3419       utf8_list = n;
3420     }
3421
3422   isect_list = vfr->isect_list;
3423   while (isect_list != NULL)
3424     {
3425       ref_intersection *next = isect_list->alloc_next;
3426       vfy_free (isect_list);
3427       isect_list = next;
3428     }
3429
3430   if (vfr->states != NULL)
3431     {
3432       int i;
3433       for (i = 0; i < vfr->current_method->code_length; ++i)
3434         {
3435           state_list *iter = vfr->states[i];
3436           while (iter != NULL)
3437             {
3438               state_list *next = iter->next;
3439               vfy_free (iter->val);
3440               vfy_free (iter);
3441               iter = next;
3442             }
3443         }
3444       vfy_free (vfr->states);
3445     }
3446   
3447   vfy_free (vfr);
3448 }
3449
3450 int
3451 verify_method (vfy_method *meth)
3452 {
3453   debug_print ("verify_method (%s) %i\n", vfy_string_bytes (meth->name),
3454                meth->code_length);
3455   
3456   if (vfr != NULL)
3457     verify_fail ("verifier re-entered");
3458
3459   make_verifier_context (meth);
3460   verify_instructions ();
3461   free_verifier_context ();
3462   vfr = NULL;
3463
3464   return 1;
3465 }