]> gcc.gnu.org Git - gcc.git/blame - libjava/verify.cc
2002-01-30 Paolo Carlini <pcarlini@unitus.it>
[gcc.git] / libjava / verify.cc
CommitLineData
a12fe13d
TT
1// defineclass.cc - defining a class from .class format.
2
17af0664 3/* Copyright (C) 2001, 2002 Free Software Foundation
a12fe13d
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
ef9f3bc4 11// Written by Tom Tromey <tromey@redhat.com>
a12fe13d 12
c1bf99a2
TT
13// Define VERIFY_DEBUG to enable debugging output.
14
a12fe13d
TT
15#include <config.h>
16
17#include <jvm.h>
18#include <gcj/cni.h>
19#include <java-insns.h>
20#include <java-interp.h>
21
75b17b74
JS
22#ifdef INTERPRETER
23
a12fe13d
TT
24#include <java/lang/Class.h>
25#include <java/lang/VerifyError.h>
26#include <java/lang/Throwable.h>
27#include <java/lang/reflect/Modifier.h>
60440707 28#include <java/lang/StringBuffer.h>
a12fe13d 29
c1bf99a2
TT
30#ifdef VERIFY_DEBUG
31#include <stdio.h>
32#endif /* VERIFY_DEBUG */
a12fe13d 33
a12fe13d 34
c1bf99a2
TT
35static void debug_print (const char *fmt, ...)
36 __attribute__ ((format (printf, 1, 2)));
37
38static inline void
39debug_print (const char *fmt, ...)
40{
41#ifdef VERIFY_DEBUG
42 va_list ap;
43 va_start (ap, fmt);
44 vfprintf (stderr, fmt, ap);
45 va_end (ap);
46#endif /* VERIFY_DEBUG */
47}
48
a12fe13d
TT
49class _Jv_BytecodeVerifier
50{
51private:
52
53 static const int FLAG_INSN_START = 1;
54 static const int FLAG_BRANCH_TARGET = 2;
a12fe13d
TT
55
56 struct state;
57 struct type;
58 struct subr_info;
0c88d7f8 59 struct linked_utf8;
a12fe13d
TT
60
61 // The current PC.
62 int PC;
63 // The PC corresponding to the start of the current instruction.
64 int start_PC;
65
66 // The current state of the stack, locals, etc.
67 state *current_state;
68
69 // We store the state at branch targets, for merging. This holds
70 // such states.
71 state **states;
72
73 // We keep a linked list of all the PCs which we must reverify.
74 // The link is done using the PC values. This is the head of the
75 // list.
76 int next_verify_pc;
77
78 // We keep some flags for each instruction. The values are the
79 // FLAG_* constants defined above.
80 char *flags;
81
82 // We need to keep track of which instructions can call a given
83 // subroutine. FIXME: this is inefficient. We keep a linked list
84 // of all calling `jsr's at at each jsr target.
85 subr_info **jsr_ptrs;
86
87 // The current top of the stack, in terms of slots.
88 int stacktop;
89 // The current depth of the stack. This will be larger than
90 // STACKTOP when wide types are on the stack.
91 int stackdepth;
92
93 // The bytecode itself.
94 unsigned char *bytecode;
95 // The exceptions.
96 _Jv_InterpException *exception;
97
98 // Defining class.
99 jclass current_class;
100 // This method.
101 _Jv_InterpMethod *current_method;
102
0c88d7f8
TT
103 // A linked list of utf8 objects we allocate. This is really ugly,
104 // but without this our utf8 objects would be collected.
105 linked_utf8 *utf8_list;
106
107 struct linked_utf8
108 {
109 _Jv_Utf8Const *val;
110 linked_utf8 *next;
111 };
112
113 _Jv_Utf8Const *make_utf8_const (char *s, int len)
114 {
115 _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
116 _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
117 + val->length
118 + 1);
119 r->length = val->length;
120 r->hash = val->hash;
121 memcpy (r->data, val->data, val->length + 1);
122
123 linked_utf8 *lu = (linked_utf8 *) _Jv_Malloc (sizeof (linked_utf8));
124 lu->val = r;
125 lu->next = utf8_list;
126 utf8_list = lu;
127
128 return r;
129 }
130
a12fe13d
TT
131 // This enum holds a list of tags for all the different types we
132 // need to handle. Reference types are treated specially by the
133 // type class.
134 enum type_val
135 {
136 void_type,
137
138 // The values for primitive types are chosen to correspond to values
139 // specified to newarray.
140 boolean_type = 4,
141 char_type = 5,
142 float_type = 6,
143 double_type = 7,
144 byte_type = 8,
145 short_type = 9,
146 int_type = 10,
147 long_type = 11,
148
149 // Used when overwriting second word of a double or long in the
150 // local variables. Also used after merging local variable states
151 // to indicate an unusable value.
152 unsuitable_type,
153 return_address_type,
154 continuation_type,
155
c1bf99a2
TT
156 // There is an obscure special case which requires us to note when
157 // a local variable has not been used by a subroutine. See
158 // push_jump_merge for more information.
159 unused_by_subroutine_type,
160
a12fe13d
TT
161 // Everything after `reference_type' must be a reference type.
162 reference_type,
163 null_type,
164 unresolved_reference_type,
165 uninitialized_reference_type,
166 uninitialized_unresolved_reference_type
167 };
168
169 // Return the type_val corresponding to a primitive signature
170 // character. For instance `I' returns `int.class'.
f70443f7 171 type_val get_type_val_for_signature (jchar sig)
a12fe13d
TT
172 {
173 type_val rt;
174 switch (sig)
175 {
176 case 'Z':
177 rt = boolean_type;
178 break;
4c6d901a
TT
179 case 'B':
180 rt = byte_type;
181 break;
a12fe13d
TT
182 case 'C':
183 rt = char_type;
184 break;
185 case 'S':
186 rt = short_type;
187 break;
188 case 'I':
189 rt = int_type;
190 break;
191 case 'J':
192 rt = long_type;
193 break;
194 case 'F':
195 rt = float_type;
196 break;
197 case 'D':
198 rt = double_type;
199 break;
200 case 'V':
201 rt = void_type;
202 break;
203 default:
204 verify_fail ("invalid signature");
205 }
206 return rt;
207 }
208
209 // Return the type_val corresponding to a primitive class.
f70443f7 210 type_val get_type_val_for_signature (jclass k)
a12fe13d
TT
211 {
212 return get_type_val_for_signature ((jchar) k->method_count);
213 }
214
f6b733ed
TT
215 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
216 // TARGET haven't been prepared.
217 static bool is_assignable_from_slow (jclass target, jclass source)
218 {
219 // This will terminate when SOURCE==Object.
220 while (true)
221 {
222 if (source == target)
223 return true;
224
225 if (target->isPrimitive () || source->isPrimitive ())
226 return false;
227
3297bb46
TT
228 // Check array case first because we can have an array whose
229 // component type is not prepared; _Jv_IsAssignableFrom
230 // doesn't handle this correctly.
f6b733ed
TT
231 if (target->isArray ())
232 {
233 if (! source->isArray ())
234 return false;
235 target = target->getComponentType ();
236 source = source->getComponentType ();
237 }
3297bb46
TT
238 // _Jv_IsAssignableFrom can handle a target which is an
239 // interface even if it hasn't been prepared.
240 else if ((target->state > JV_STATE_LINKED || target->isInterface ())
241 && source->state > JV_STATE_LINKED)
242 return _Jv_IsAssignableFrom (target, source);
f6b733ed
TT
243 else if (target->isInterface ())
244 {
245 for (int i = 0; i < source->interface_count; ++i)
246 {
247 // We use a recursive call because we also need to
248 // check superinterfaces.
249 if (is_assignable_from_slow (target, source->interfaces[i]))
250 return true;
251 }
7ac20fe4
PB
252 source = source->getSuperclass ();
253 if (source == NULL)
254 return false;
f6b733ed
TT
255 }
256 else if (target == &java::lang::Object::class$)
257 return true;
258 else if (source->isInterface ()
259 || source == &java::lang::Object::class$)
260 return false;
261 else
262 source = source->getSuperclass ();
263 }
264 }
265
a12fe13d
TT
266 // This is used to keep track of which `jsr's correspond to a given
267 // jsr target.
268 struct subr_info
269 {
270 // PC of the instruction just after the jsr.
271 int pc;
272 // Link.
273 subr_info *next;
274 };
275
276 // The `type' class is used to represent a single type in the
277 // verifier.
278 struct type
279 {
280 // The type.
281 type_val key;
282 // Some associated data.
283 union
284 {
285 // For a resolved reference type, this is a pointer to the class.
286 jclass klass;
287 // For other reference types, this it the name of the class.
288 _Jv_Utf8Const *name;
289 } data;
290 // This is used when constructing a new object. It is the PC of the
291 // `new' instruction which created the object. We use the special
292 // value -2 to mean that this is uninitialized, and the special
293 // value -1 for the case where the current method is itself the
294 // <init> method.
295 int pc;
296
297 static const int UNINIT = -2;
298 static const int SELF = -1;
299
300 // Basic constructor.
301 type ()
302 {
303 key = unsuitable_type;
304 data.klass = NULL;
305 pc = UNINIT;
306 }
307
308 // Make a new instance given the type tag. We assume a generic
309 // `reference_type' means Object.
310 type (type_val k)
311 {
312 key = k;
313 data.klass = NULL;
314 if (key == reference_type)
315 data.klass = &java::lang::Object::class$;
316 pc = UNINIT;
317 }
318
319 // Make a new instance given a class.
320 type (jclass klass)
321 {
322 key = reference_type;
323 data.klass = klass;
324 pc = UNINIT;
325 }
326
327 // Make a new instance given the name of a class.
328 type (_Jv_Utf8Const *n)
329 {
330 key = unresolved_reference_type;
331 data.name = n;
332 pc = UNINIT;
333 }
334
335 // Copy constructor.
336 type (const type &t)
337 {
338 key = t.key;
339 data = t.data;
340 pc = t.pc;
341 }
342
343 // These operators are required because libgcj can't link in
344 // -lstdc++.
345 void *operator new[] (size_t bytes)
346 {
347 return _Jv_Malloc (bytes);
348 }
349
350 void operator delete[] (void *mem)
351 {
352 _Jv_Free (mem);
353 }
354
355 type& operator= (type_val k)
356 {
357 key = k;
358 data.klass = NULL;
359 pc = UNINIT;
360 return *this;
361 }
362
363 type& operator= (const type& t)
364 {
365 key = t.key;
366 data = t.data;
367 pc = t.pc;
368 return *this;
369 }
370
371 // Promote a numeric type.
f6b733ed 372 type &promote ()
a12fe13d
TT
373 {
374 if (key == boolean_type || key == char_type
375 || key == byte_type || key == short_type)
376 key = int_type;
f6b733ed 377 return *this;
a12fe13d
TT
378 }
379
380 // If *THIS is an unresolved reference type, resolve it.
f70443f7 381 void resolve (_Jv_BytecodeVerifier *verifier)
a12fe13d
TT
382 {
383 if (key != unresolved_reference_type
384 && key != uninitialized_unresolved_reference_type)
385 return;
386
a12fe13d 387 using namespace java::lang;
f70443f7
PB
388 java::lang::ClassLoader *loader
389 = verifier->current_class->getClassLoader();
a12fe13d
TT
390 // We might see either kind of name. Sigh.
391 if (data.name->data[0] == 'L'
392 && data.name->data[data.name->length - 1] == ';')
f70443f7 393 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
a12fe13d
TT
394 else
395 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
f70443f7 396 false, loader);
a12fe13d
TT
397 key = (key == unresolved_reference_type
398 ? reference_type
399 : uninitialized_reference_type);
400 }
401
402 // Mark this type as the uninitialized result of `new'.
f70443f7 403 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
a12fe13d 404 {
e7b35eec
TT
405 if (key == reference_type)
406 key = uninitialized_reference_type;
407 else if (key == unresolved_reference_type)
408 key = uninitialized_unresolved_reference_type;
409 else
f70443f7 410 verifier->verify_fail ("internal error in type::uninitialized");
e7b35eec 411 pc = npc;
a12fe13d
TT
412 }
413
414 // Mark this type as now initialized.
415 void set_initialized (int npc)
416 {
e7b35eec
TT
417 if (npc != UNINIT && pc == npc
418 && (key == uninitialized_reference_type
419 || key == uninitialized_unresolved_reference_type))
a12fe13d
TT
420 {
421 key = (key == uninitialized_reference_type
422 ? reference_type
423 : unresolved_reference_type);
424 pc = UNINIT;
425 }
426 }
427
428
429 // Return true if an object of type K can be assigned to a variable
430 // of type *THIS. Handle various special cases too. Might modify
431 // *THIS or K. Note however that this does not perform numeric
432 // promotion.
f70443f7 433 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
a12fe13d
TT
434 {
435 // Any type is compatible with the unsuitable type.
436 if (key == unsuitable_type)
437 return true;
438
439 if (key < reference_type || k.key < reference_type)
440 return key == k.key;
441
442 // The `null' type is convertible to any reference type.
443 // FIXME: is this correct for THIS?
444 if (key == null_type || k.key == null_type)
445 return true;
446
447 // Any reference type is convertible to Object. This is a special
448 // case so we don't need to unnecessarily resolve a class.
449 if (key == reference_type
450 && data.klass == &java::lang::Object::class$)
451 return true;
452
453 // An initialized type and an uninitialized type are not
454 // compatible.
455 if (isinitialized () != k.isinitialized ())
456 return false;
457
458 // Two uninitialized objects are compatible if either:
459 // * The PCs are identical, or
460 // * One PC is UNINIT.
461 if (! isinitialized ())
462 {
463 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
464 return false;
465 }
466
467 // Two unresolved types are equal if their names are the same.
468 if (! isresolved ()
469 && ! k.isresolved ()
470 && _Jv_equalUtf8Consts (data.name, k.data.name))
471 return true;
472
473 // We must resolve both types and check assignability.
f70443f7
PB
474 resolve (verifier);
475 k.resolve (verifier);
f6b733ed 476 return is_assignable_from_slow (data.klass, k.data.klass);
a12fe13d
TT
477 }
478
479 bool isvoid () const
480 {
481 return key == void_type;
482 }
483
484 bool iswide () const
485 {
486 return key == long_type || key == double_type;
487 }
488
489 // Return number of stack or local variable slots taken by this
490 // type.
491 int depth () const
492 {
493 return iswide () ? 2 : 1;
494 }
495
496 bool isarray () const
497 {
498 // We treat null_type as not an array. This is ok based on the
499 // current uses of this method.
500 if (key == reference_type)
501 return data.klass->isArray ();
502 else if (key == unresolved_reference_type)
503 return data.name->data[0] == '[';
504 return false;
505 }
506
f70443f7 507 bool isinterface (_Jv_BytecodeVerifier *verifier)
a12fe13d 508 {
f70443f7 509 resolve (verifier);
a12fe13d
TT
510 if (key != reference_type)
511 return false;
512 return data.klass->isInterface ();
513 }
514
f70443f7 515 bool isabstract (_Jv_BytecodeVerifier *verifier)
a12fe13d 516 {
f70443f7 517 resolve (verifier);
a12fe13d
TT
518 if (key != reference_type)
519 return false;
520 using namespace java::lang::reflect;
521 return Modifier::isAbstract (data.klass->getModifiers ());
522 }
523
524 // Return the element type of an array.
f70443f7 525 type element_type (_Jv_BytecodeVerifier *verifier)
a12fe13d
TT
526 {
527 // FIXME: maybe should do string manipulation here.
f70443f7 528 resolve (verifier);
a12fe13d 529 if (key != reference_type)
f70443f7 530 verifier->verify_fail ("programmer error in type::element_type()", -1);
a12fe13d
TT
531
532 jclass k = data.klass->getComponentType ();
533 if (k->isPrimitive ())
f70443f7 534 return type (verifier->get_type_val_for_signature (k));
a12fe13d
TT
535 return type (k);
536 }
537
d68e5f55
TT
538 // Return the array type corresponding to an initialized
539 // reference. We could expand this to work for other kinds of
540 // types, but currently we don't need to.
f70443f7 541 type to_array (_Jv_BytecodeVerifier *verifier)
d68e5f55
TT
542 {
543 // Resolving isn't ideal, because it might force us to load
544 // another class, but it's easy. FIXME?
545 if (key == unresolved_reference_type)
f70443f7 546 resolve (verifier);
d68e5f55
TT
547
548 if (key == reference_type)
549 return type (_Jv_GetArrayClass (data.klass,
550 data.klass->getClassLoader ()));
551 else
f70443f7 552 verifier->verify_fail ("internal error in type::to_array()");
d68e5f55
TT
553 }
554
a12fe13d
TT
555 bool isreference () const
556 {
557 return key >= reference_type;
558 }
559
560 int get_pc () const
561 {
562 return pc;
563 }
564
565 bool isinitialized () const
566 {
567 return (key == reference_type
568 || key == null_type
569 || key == unresolved_reference_type);
570 }
571
572 bool isresolved () const
573 {
574 return (key == reference_type
575 || key == null_type
576 || key == uninitialized_reference_type);
577 }
578
f70443f7 579 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
a12fe13d
TT
580 {
581 // The way this is written, we don't need to check isarray().
582 if (key == reference_type)
583 {
584 jclass k = data.klass;
585 while (k->isArray () && ndims > 0)
586 {
587 k = k->getComponentType ();
588 --ndims;
589 }
590 }
591 else
592 {
593 // We know KEY == unresolved_reference_type.
594 char *p = data.name->data;
595 while (*p++ == '[' && ndims-- > 0)
596 ;
597 }
598
599 if (ndims > 0)
f70443f7 600 verifier->verify_fail ("array type has fewer dimensions than required");
a12fe13d
TT
601 }
602
603 // Merge OLD_TYPE into this. On error throw exception.
1919a4e7
TT
604 bool merge (type& old_type, bool local_semantics,
605 _Jv_BytecodeVerifier *verifier)
a12fe13d
TT
606 {
607 bool changed = false;
608 bool refo = old_type.isreference ();
609 bool refn = isreference ();
610 if (refo && refn)
611 {
612 if (old_type.key == null_type)
613 ;
614 else if (key == null_type)
615 {
616 *this = old_type;
617 changed = true;
618 }
619 else if (isinitialized () != old_type.isinitialized ())
f70443f7 620 verifier->verify_fail ("merging initialized and uninitialized types");
a12fe13d
TT
621 else
622 {
623 if (! isinitialized ())
624 {
625 if (pc == UNINIT)
626 pc = old_type.pc;
627 else if (old_type.pc == UNINIT)
628 ;
629 else if (pc != old_type.pc)
f70443f7 630 verifier->verify_fail ("merging different uninitialized types");
a12fe13d
TT
631 }
632
633 if (! isresolved ()
634 && ! old_type.isresolved ()
635 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
636 {
637 // Types are identical.
638 }
639 else
640 {
f70443f7
PB
641 resolve (verifier);
642 old_type.resolve (verifier);
a12fe13d
TT
643
644 jclass k = data.klass;
645 jclass oldk = old_type.data.klass;
646
647 int arraycount = 0;
648 while (k->isArray () && oldk->isArray ())
649 {
650 ++arraycount;
651 k = k->getComponentType ();
652 oldk = oldk->getComponentType ();
653 }
654
655 // This loop will end when we hit Object.
656 while (true)
657 {
f6b733ed 658 if (is_assignable_from_slow (k, oldk))
a12fe13d
TT
659 break;
660 k = k->getSuperclass ();
661 changed = true;
662 }
663
664 if (changed)
665 {
666 while (arraycount > 0)
667 {
1919a4e7
TT
668 java::lang::ClassLoader *loader
669 = verifier->current_class->getClassLoader();
670 k = _Jv_GetArrayClass (k, loader);
a12fe13d
TT
671 --arraycount;
672 }
673 data.klass = k;
674 }
675 }
676 }
677 }
678 else if (refo || refn || key != old_type.key)
679 {
680 if (local_semantics)
681 {
c1bf99a2
TT
682 // If we're merging into an "unused" slot, then we
683 // simply accept whatever we're merging from.
684 if (key == unused_by_subroutine_type)
685 {
686 *this = old_type;
687 changed = true;
688 }
689 else if (old_type.key == unused_by_subroutine_type)
690 {
691 // Do nothing.
692 }
0c88d7f8
TT
693 // If we already have an `unsuitable' type, then we
694 // don't need to change again.
c1bf99a2 695 else if (key != unsuitable_type)
0c88d7f8
TT
696 {
697 key = unsuitable_type;
698 changed = true;
699 }
a12fe13d
TT
700 }
701 else
f70443f7 702 verifier->verify_fail ("unmergeable type");
a12fe13d
TT
703 }
704 return changed;
705 }
c1bf99a2
TT
706
707#ifdef VERIFY_DEBUG
708 void print (void) const
709 {
710 char c = '?';
711 switch (key)
712 {
713 case boolean_type: c = 'Z'; break;
714 case byte_type: c = 'B'; break;
715 case char_type: c = 'C'; break;
716 case short_type: c = 'S'; break;
717 case int_type: c = 'I'; break;
718 case long_type: c = 'J'; break;
719 case float_type: c = 'F'; break;
720 case double_type: c = 'D'; break;
721 case void_type: c = 'V'; break;
722 case unsuitable_type: c = '-'; break;
723 case return_address_type: c = 'r'; break;
724 case continuation_type: c = '+'; break;
725 case unused_by_subroutine_type: c = '_'; break;
726 case reference_type: c = 'L'; break;
727 case null_type: c = '@'; break;
728 case unresolved_reference_type: c = 'l'; break;
729 case uninitialized_reference_type: c = 'U'; break;
730 case uninitialized_unresolved_reference_type: c = 'u'; break;
731 }
732 debug_print ("%c", c);
733 }
734#endif /* VERIFY_DEBUG */
a12fe13d
TT
735 };
736
737 // This class holds all the state information we need for a given
738 // location.
739 struct state
740 {
741 // Current top of stack.
742 int stacktop;
743 // Current stack depth. This is like the top of stack but it
744 // includes wide variable information.
745 int stackdepth;
746 // The stack.
747 type *stack;
748 // The local variables.
749 type *locals;
750 // This is used in subroutines to keep track of which local
751 // variables have been accessed.
752 bool *local_changed;
753 // If not 0, then we are in a subroutine. The value is the PC of
754 // the subroutine's entry point. We can use 0 as an exceptional
755 // value because PC=0 can never be a subroutine.
756 int subroutine;
757 // This is used to keep a linked list of all the states which
758 // require re-verification. We use the PC to keep track.
759 int next;
6d8b1244
TT
760 // We keep track of the type of `this' specially. This is used to
761 // ensure that an instance initializer invokes another initializer
762 // on `this' before returning. We must keep track of this
763 // specially because otherwise we might be confused by code which
764 // assigns to locals[0] (overwriting `this') and then returns
765 // without really initializing.
766 type this_type;
a12fe13d
TT
767
768 // INVALID marks a state which is not on the linked list of states
769 // requiring reverification.
770 static const int INVALID = -1;
771 // NO_NEXT marks the state at the end of the reverification list.
772 static const int NO_NEXT = -2;
773
774 state ()
6d8b1244 775 : this_type ()
a12fe13d
TT
776 {
777 stack = NULL;
778 locals = NULL;
779 local_changed = NULL;
780 }
781
782 state (int max_stack, int max_locals)
6d8b1244 783 : this_type ()
a12fe13d
TT
784 {
785 stacktop = 0;
786 stackdepth = 0;
787 stack = new type[max_stack];
788 for (int i = 0; i < max_stack; ++i)
789 stack[i] = unsuitable_type;
790 locals = new type[max_locals];
791 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
792 for (int i = 0; i < max_locals; ++i)
793 {
794 locals[i] = unsuitable_type;
795 local_changed[i] = false;
796 }
797 next = INVALID;
798 subroutine = 0;
799 }
800
c1bf99a2
TT
801 state (const state *orig, int max_stack, int max_locals,
802 bool ret_semantics = false)
a12fe13d
TT
803 {
804 stack = new type[max_stack];
805 locals = new type[max_locals];
806 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
c1bf99a2 807 copy (orig, max_stack, max_locals, ret_semantics);
a12fe13d
TT
808 next = INVALID;
809 }
810
811 ~state ()
812 {
813 if (stack)
814 delete[] stack;
815 if (locals)
816 delete[] locals;
817 if (local_changed)
818 _Jv_Free (local_changed);
819 }
820
821 void *operator new[] (size_t bytes)
822 {
823 return _Jv_Malloc (bytes);
824 }
825
826 void operator delete[] (void *mem)
827 {
828 _Jv_Free (mem);
829 }
830
831 void *operator new (size_t bytes)
832 {
833 return _Jv_Malloc (bytes);
834 }
835
836 void operator delete (void *mem)
837 {
838 _Jv_Free (mem);
839 }
840
c1bf99a2
TT
841 void copy (const state *copy, int max_stack, int max_locals,
842 bool ret_semantics = false)
a12fe13d
TT
843 {
844 stacktop = copy->stacktop;
845 stackdepth = copy->stackdepth;
846 subroutine = copy->subroutine;
847 for (int i = 0; i < max_stack; ++i)
848 stack[i] = copy->stack[i];
849 for (int i = 0; i < max_locals; ++i)
850 {
c1bf99a2
TT
851 // See push_jump_merge to understand this case.
852 if (ret_semantics)
853 locals[i] = type (copy->local_changed[i]
854 ? unsuitable_type
855 : unused_by_subroutine_type);
856 else
857 locals[i] = copy->locals[i];
a12fe13d
TT
858 local_changed[i] = copy->local_changed[i];
859 }
6d8b1244 860 this_type = copy->this_type;
a12fe13d
TT
861 // Don't modify `next'.
862 }
863
864 // Modify this state to reflect entry to an exception handler.
865 void set_exception (type t, int max_stack)
866 {
867 stackdepth = 1;
868 stacktop = 1;
869 stack[0] = t;
870 for (int i = stacktop; i < max_stack; ++i)
871 stack[i] = unsuitable_type;
872
873 // FIXME: subroutine handling?
874 }
875
6d8b1244
TT
876 // Merge STATE_OLD into this state. Destructively modifies this
877 // state. Returns true if the new state was in fact changed.
878 // Will throw an exception if the states are not mergeable.
a12fe13d 879 bool merge (state *state_old, bool ret_semantics,
f70443f7 880 int max_locals, _Jv_BytecodeVerifier *verifier)
a12fe13d
TT
881 {
882 bool changed = false;
883
6d8b1244
TT
884 // Special handling for `this'. If one or the other is
885 // uninitialized, then the merge is uninitialized.
886 if (this_type.isinitialized ())
887 this_type = state_old->this_type;
888
a12fe13d
TT
889 // Merge subroutine states. *THIS and *STATE_OLD must be in the
890 // same subroutine. Also, recursive subroutine calls must be
891 // avoided.
892 if (subroutine == state_old->subroutine)
893 {
894 // Nothing.
895 }
896 else if (subroutine == 0)
897 {
898 subroutine = state_old->subroutine;
899 changed = true;
900 }
1919a4e7
TT
901 // If we're handling the result of an unmerged `ret', then we
902 // can't trust that it has the correct PC setting. So in this
903 // case we ignore what might otherwise look like a merge error.
904 else if (! state_old->is_unmerged_ret_state (max_locals))
f70443f7 905 verifier->verify_fail ("subroutines merged");
a12fe13d
TT
906
907 // Merge stacks.
908 if (state_old->stacktop != stacktop)
f70443f7 909 verifier->verify_fail ("stack sizes differ");
a12fe13d
TT
910 for (int i = 0; i < state_old->stacktop; ++i)
911 {
f70443f7 912 if (stack[i].merge (state_old->stack[i], false, verifier))
a12fe13d
TT
913 changed = true;
914 }
915
916 // Merge local variables.
917 for (int i = 0; i < max_locals; ++i)
918 {
919 if (! ret_semantics || local_changed[i])
920 {
f70443f7 921 if (locals[i].merge (state_old->locals[i], true, verifier))
a12fe13d
TT
922 {
923 changed = true;
924 note_variable (i);
925 }
926 }
927
928 // If we're in a subroutine, we must compute the union of
929 // all the changed local variables.
930 if (state_old->local_changed[i])
931 note_variable (i);
932 }
933
934 return changed;
935 }
936
937 // Throw an exception if there is an uninitialized object on the
938 // stack or in a local variable. EXCEPTION_SEMANTICS controls
939 // whether we're using backwards-branch or exception-handing
940 // semantics.
941 void check_no_uninitialized_objects (int max_locals,
f70443f7 942 _Jv_BytecodeVerifier *verifier,
a12fe13d
TT
943 bool exception_semantics = false)
944 {
945 if (! exception_semantics)
946 {
947 for (int i = 0; i < stacktop; ++i)
948 if (stack[i].isreference () && ! stack[i].isinitialized ())
f70443f7 949 verifier->verify_fail ("uninitialized object on stack");
a12fe13d
TT
950 }
951
952 for (int i = 0; i < max_locals; ++i)
953 if (locals[i].isreference () && ! locals[i].isinitialized ())
f70443f7 954 verifier->verify_fail ("uninitialized object in local variable");
6d8b1244 955
f70443f7 956 check_this_initialized (verifier);
6d8b1244
TT
957 }
958
959 // Ensure that `this' has been initialized.
f70443f7 960 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
6d8b1244
TT
961 {
962 if (this_type.isreference () && ! this_type.isinitialized ())
f70443f7 963 verifier->verify_fail ("`this' is uninitialized");
6d8b1244
TT
964 }
965
966 // Set type of `this'.
967 void set_this_type (const type &k)
968 {
969 this_type = k;
a12fe13d
TT
970 }
971
c1bf99a2 972 // Note that a local variable was modified.
a12fe13d
TT
973 void note_variable (int index)
974 {
975 if (subroutine > 0)
976 local_changed[index] = true;
977 }
978
979 // Mark each `new'd object we know of that was allocated at PC as
980 // initialized.
981 void set_initialized (int pc, int max_locals)
982 {
983 for (int i = 0; i < stacktop; ++i)
984 stack[i].set_initialized (pc);
985 for (int i = 0; i < max_locals; ++i)
986 locals[i].set_initialized (pc);
6d8b1244 987 this_type.set_initialized (pc);
a12fe13d 988 }
c1bf99a2
TT
989
990 // Return true if this state is the unmerged result of a `ret'.
991 bool is_unmerged_ret_state (int max_locals) const
992 {
993 for (int i = 0; i < max_locals; ++i)
994 {
995 if (locals[i].key == unused_by_subroutine_type)
996 return true;
997 }
998 return false;
999 }
1000
1001#ifdef VERIFY_DEBUG
1002 void print (const char *leader, int pc,
1003 int max_stack, int max_locals) const
1004 {
1005 debug_print ("%s [%4d]: [stack] ", leader, pc);
1006 int i;
1007 for (i = 0; i < stacktop; ++i)
1008 stack[i].print ();
1009 for (; i < max_stack; ++i)
1010 debug_print (".");
1011 debug_print (" [local] ");
1012 for (i = 0; i < max_locals; ++i)
1013 locals[i].print ();
1919a4e7
TT
1014 if (subroutine == 0)
1015 debug_print (" | None");
1016 else
1017 debug_print (" | %4d", subroutine);
1018 debug_print (" | %p\n", this);
c1bf99a2
TT
1019 }
1020#else
1021 inline void print (const char *, int, int, int) const
1022 {
1023 }
1024#endif /* VERIFY_DEBUG */
a12fe13d
TT
1025 };
1026
1027 type pop_raw ()
1028 {
1029 if (current_state->stacktop <= 0)
f70443f7 1030 verify_fail ("stack empty");
a12fe13d
TT
1031 type r = current_state->stack[--current_state->stacktop];
1032 current_state->stackdepth -= r.depth ();
1033 if (current_state->stackdepth < 0)
e7b35eec 1034 verify_fail ("stack empty", start_PC);
a12fe13d
TT
1035 return r;
1036 }
1037
1038 type pop32 ()
1039 {
1040 type r = pop_raw ();
1041 if (r.iswide ())
f70443f7 1042 verify_fail ("narrow pop of wide type");
a12fe13d
TT
1043 return r;
1044 }
1045
1046 type pop64 ()
1047 {
1048 type r = pop_raw ();
1049 if (! r.iswide ())
f70443f7 1050 verify_fail ("wide pop of narrow type");
a12fe13d
TT
1051 return r;
1052 }
1053
1054 type pop_type (type match)
1055 {
e7b35eec 1056 match.promote ();
a12fe13d 1057 type t = pop_raw ();
f70443f7
PB
1058 if (! match.compatible (t, this))
1059 verify_fail ("incompatible type on stack");
a12fe13d
TT
1060 return t;
1061 }
1062
ef9f3bc4
TT
1063 // Pop a reference type or a return address.
1064 type pop_ref_or_return ()
1065 {
1066 type t = pop_raw ();
1067 if (! t.isreference () && t.key != return_address_type)
f70443f7 1068 verify_fail ("expected reference or return address on stack");
ef9f3bc4
TT
1069 return t;
1070 }
1071
a12fe13d
TT
1072 void push_type (type t)
1073 {
1074 // If T is a numeric type like short, promote it to int.
1075 t.promote ();
1076
1077 int depth = t.depth ();
1078 if (current_state->stackdepth + depth > current_method->max_stack)
1079 verify_fail ("stack overflow");
1080 current_state->stack[current_state->stacktop++] = t;
1081 current_state->stackdepth += depth;
1082 }
1083
1084 void set_variable (int index, type t)
1085 {
1086 // If T is a numeric type like short, promote it to int.
1087 t.promote ();
1088
1089 int depth = t.depth ();
1090 if (index > current_method->max_locals - depth)
1091 verify_fail ("invalid local variable");
1092 current_state->locals[index] = t;
1093 current_state->note_variable (index);
1094
1095 if (depth == 2)
1096 {
1097 current_state->locals[index + 1] = continuation_type;
1098 current_state->note_variable (index + 1);
1099 }
1100 if (index > 0 && current_state->locals[index - 1].iswide ())
1101 {
1102 current_state->locals[index - 1] = unsuitable_type;
1103 // There's no need to call note_variable here.
1104 }
1105 }
1106
1107 type get_variable (int index, type t)
1108 {
1109 int depth = t.depth ();
1110 if (index > current_method->max_locals - depth)
f70443f7
PB
1111 verify_fail ("invalid local variable");
1112 if (! t.compatible (current_state->locals[index], this))
1113 verify_fail ("incompatible type in local variable");
a12fe13d
TT
1114 if (depth == 2)
1115 {
1116 type t (continuation_type);
f70443f7
PB
1117 if (! current_state->locals[index + 1].compatible (t, this))
1118 verify_fail ("invalid local variable");
a12fe13d 1119 }
a12fe13d
TT
1120 return current_state->locals[index];
1121 }
1122
1123 // Make sure ARRAY is an array type and that its elements are
1124 // compatible with type ELEMENT. Returns the actual element type.
1125 type require_array_type (type array, type element)
1126 {
1127 if (! array.isarray ())
1128 verify_fail ("array required");
1129
f70443f7
PB
1130 type t = array.element_type (this);
1131 if (! element.compatible (t, this))
1578fa95
TT
1132 {
1133 // Special case for byte arrays, which must also be boolean
1134 // arrays.
1135 bool ok = true;
1136 if (element.key == byte_type)
1137 {
1138 type e2 (boolean_type);
f70443f7 1139 ok = e2.compatible (t, this);
1578fa95
TT
1140 }
1141 if (! ok)
1142 verify_fail ("incompatible array element type");
1143 }
a12fe13d
TT
1144
1145 // Return T and not ELEMENT, because T might be specialized.
1146 return t;
1147 }
1148
1149 jint get_byte ()
1150 {
1151 if (PC >= current_method->code_length)
1152 verify_fail ("premature end of bytecode");
1153 return (jint) bytecode[PC++] & 0xff;
1154 }
1155
1156 jint get_ushort ()
1157 {
590077b0
TT
1158 jint b1 = get_byte ();
1159 jint b2 = get_byte ();
a12fe13d
TT
1160 return (jint) ((b1 << 8) | b2) & 0xffff;
1161 }
1162
1163 jint get_short ()
1164 {
590077b0
TT
1165 jint b1 = get_byte ();
1166 jint b2 = get_byte ();
a12fe13d
TT
1167 jshort s = (b1 << 8) | b2;
1168 return (jint) s;
1169 }
1170
1171 jint get_int ()
1172 {
590077b0
TT
1173 jint b1 = get_byte ();
1174 jint b2 = get_byte ();
1175 jint b3 = get_byte ();
1176 jint b4 = get_byte ();
a12fe13d
TT
1177 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1178 }
1179
1180 int compute_jump (int offset)
1181 {
1182 int npc = start_PC + offset;
1183 if (npc < 0 || npc >= current_method->code_length)
1578fa95 1184 verify_fail ("branch out of range", start_PC);
a12fe13d
TT
1185 return npc;
1186 }
1187
ef9f3bc4
TT
1188 // Merge the indicated state into the state at the branch target and
1189 // schedule a new PC if there is a change. If RET_SEMANTICS is
1190 // true, then we are merging from a `ret' instruction into the
1191 // instruction after a `jsr'. This is a special case with its own
1192 // modified semantics.
a12fe13d
TT
1193 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1194 {
1195 bool changed = true;
1196 if (states[npc] == NULL)
1197 {
c1bf99a2
TT
1198 // There's a weird situation here. If are examining the
1199 // branch that results from a `ret', and there is not yet a
1200 // state available at the branch target (the instruction just
1201 // after the `jsr'), then we have to construct a special kind
1202 // of state at that point for future merging. This special
1203 // state has the type `unused_by_subroutine_type' in each slot
1204 // which was not modified by the subroutine.
a12fe13d 1205 states[npc] = new state (nstate, current_method->max_stack,
c1bf99a2
TT
1206 current_method->max_locals, ret_semantics);
1207 debug_print ("== New state in push_jump_merge\n");
1208 states[npc]->print ("New", npc, current_method->max_stack,
1209 current_method->max_locals);
a12fe13d
TT
1210 }
1211 else
c1bf99a2
TT
1212 {
1213 debug_print ("== Merge states in push_jump_merge\n");
1214 nstate->print ("Frm", start_PC, current_method->max_stack,
1215 current_method->max_locals);
1216 states[npc]->print (" To", npc, current_method->max_stack,
1217 current_method->max_locals);
1218 changed = states[npc]->merge (nstate, ret_semantics,
f70443f7 1219 current_method->max_locals, this);
c1bf99a2
TT
1220 states[npc]->print ("New", npc, current_method->max_stack,
1221 current_method->max_locals);
1222 }
a12fe13d
TT
1223
1224 if (changed && states[npc]->next == state::INVALID)
1225 {
1226 // The merge changed the state, and the new PC isn't yet on our
1227 // list of PCs to re-verify.
1228 states[npc]->next = next_verify_pc;
1229 next_verify_pc = npc;
1230 }
1231 }
1232
1233 void push_jump (int offset)
1234 {
1235 int npc = compute_jump (offset);
1236 if (npc < PC)
f70443f7 1237 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
a12fe13d
TT
1238 push_jump_merge (npc, current_state);
1239 }
1240
1241 void push_exception_jump (type t, int pc)
1242 {
8051c3d6 1243 current_state->check_no_uninitialized_objects (current_method->max_locals,
f70443f7 1244 this, true);
a12fe13d
TT
1245 state s (current_state, current_method->max_stack,
1246 current_method->max_locals);
1247 s.set_exception (t, current_method->max_stack);
1248 push_jump_merge (pc, &s);
1249 }
1250
1251 int pop_jump ()
1252 {
c1bf99a2 1253 int *prev_loc = &next_verify_pc;
a12fe13d 1254 int npc = next_verify_pc;
c1bf99a2
TT
1255 bool skipped = false;
1256
1257 while (npc != state::NO_NEXT)
a12fe13d 1258 {
c1bf99a2
TT
1259 // If the next available PC is an unmerged `ret' state, then
1260 // we aren't yet ready to handle it. That's because we would
1261 // need all kind of special cases to do so. So instead we
1262 // defer this jump until after we've processed it via a
1263 // fall-through. This has to happen because the instruction
1264 // before this one must be a `jsr'.
1265 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1266 {
1267 *prev_loc = states[npc]->next;
1268 states[npc]->next = state::INVALID;
1269 return npc;
1270 }
1271
1272 skipped = true;
1273 prev_loc = &states[npc]->next;
1274 npc = states[npc]->next;
a12fe13d 1275 }
c1bf99a2
TT
1276
1277 // If we've skipped states and there is nothing else, that's a
1278 // bug.
1279 if (skipped)
1280 verify_fail ("pop_jump: can't happen");
1281 return state::NO_NEXT;
a12fe13d
TT
1282 }
1283
1284 void invalidate_pc ()
1285 {
1286 PC = state::NO_NEXT;
1287 }
1288
1289 void note_branch_target (int pc, bool is_jsr_target = false)
1290 {
286f759e
TT
1291 // Don't check `pc <= PC', because we've advanced PC after
1292 // fetching the target and we haven't yet checked the next
1293 // instruction.
1294 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1295 verify_fail ("branch not to instruction start", start_PC);
a12fe13d
TT
1296 flags[pc] |= FLAG_BRANCH_TARGET;
1297 if (is_jsr_target)
1298 {
1299 // Record the jsr which called this instruction.
1300 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1301 info->pc = PC;
1302 info->next = jsr_ptrs[pc];
1303 jsr_ptrs[pc] = info;
a12fe13d
TT
1304 }
1305 }
1306
1307 void skip_padding ()
1308 {
1309 while ((PC % 4) > 0)
b1194618
TT
1310 if (get_byte () != 0)
1311 verify_fail ("found nonzero padding byte");
a12fe13d
TT
1312 }
1313
1314 // Return the subroutine to which the instruction at PC belongs.
1315 int get_subroutine (int pc)
1316 {
1317 if (states[pc] == NULL)
1318 return 0;
1319 return states[pc]->subroutine;
1320 }
1321
1322 // Do the work for a `ret' instruction. INDEX is the index into the
1323 // local variables.
1324 void handle_ret_insn (int index)
1325 {
1326 get_variable (index, return_address_type);
1327
1328 int csub = current_state->subroutine;
1329 if (csub == 0)
1330 verify_fail ("no subroutine");
1331
1332 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1333 {
1334 // Temporarily modify the current state so it looks like we're
1335 // in the enclosing context.
1336 current_state->subroutine = get_subroutine (subr->pc);
1337 if (subr->pc < PC)
f70443f7 1338 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
a12fe13d
TT
1339 push_jump_merge (subr->pc, current_state, true);
1340 }
1341
1342 current_state->subroutine = csub;
1343 invalidate_pc ();
1344 }
1345
1346 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1347 // sure this subroutine isn't already on the stack.
1348 void check_nonrecursive_call (int sub, int dest)
1349 {
1350 if (sub == 0)
1351 return;
1352 if (sub == dest)
1353 verify_fail ("recursive subroutine call");
1354 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1355 check_nonrecursive_call (get_subroutine (info->pc), dest);
1356 }
1357
1358 void handle_jsr_insn (int offset)
1359 {
1360 int npc = compute_jump (offset);
1361
1362 if (npc < PC)
f70443f7 1363 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
a12fe13d
TT
1364 check_nonrecursive_call (current_state->subroutine, npc);
1365
1366 // Temporarily modify the current state so that it looks like we are
1367 // in the subroutine.
1368 push_type (return_address_type);
1369 int save = current_state->subroutine;
1370 current_state->subroutine = npc;
1371
1372 // Merge into the subroutine.
1373 push_jump_merge (npc, current_state);
1374
1375 // Undo our modifications.
1376 current_state->subroutine = save;
1377 pop_type (return_address_type);
1378 }
1379
1380 jclass construct_primitive_array_type (type_val prim)
1381 {
1382 jclass k = NULL;
1383 switch (prim)
1384 {
1385 case boolean_type:
1386 k = JvPrimClass (boolean);
1387 break;
1388 case char_type:
1389 k = JvPrimClass (char);
1390 break;
1391 case float_type:
1392 k = JvPrimClass (float);
1393 break;
1394 case double_type:
1395 k = JvPrimClass (double);
1396 break;
1397 case byte_type:
1398 k = JvPrimClass (byte);
1399 break;
1400 case short_type:
1401 k = JvPrimClass (short);
1402 break;
1403 case int_type:
1404 k = JvPrimClass (int);
1405 break;
1406 case long_type:
1407 k = JvPrimClass (long);
1408 break;
1409 default:
1410 verify_fail ("unknown type in construct_primitive_array_type");
1411 }
1412 k = _Jv_GetArrayClass (k, NULL);
1413 return k;
1414 }
1415
1416 // This pass computes the location of branch targets and also
1417 // instruction starts.
1418 void branch_prepass ()
1419 {
1420 flags = (char *) _Jv_Malloc (current_method->code_length);
1421 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1422 * current_method->code_length);
1423
1424 for (int i = 0; i < current_method->code_length; ++i)
1425 {
1426 flags[i] = 0;
1427 jsr_ptrs[i] = NULL;
1428 }
1429
1430 bool last_was_jsr = false;
1431
1432 PC = 0;
1433 while (PC < current_method->code_length)
1434 {
286f759e
TT
1435 // Set `start_PC' early so that error checking can have the
1436 // correct value.
1437 start_PC = PC;
a12fe13d
TT
1438 flags[PC] |= FLAG_INSN_START;
1439
1440 // If the previous instruction was a jsr, then the next
1441 // instruction is a branch target -- the branch being the
1442 // corresponding `ret'.
1443 if (last_was_jsr)
1444 note_branch_target (PC);
1445 last_was_jsr = false;
1446
fa88ce26 1447 java_opcode opcode = (java_opcode) bytecode[PC++];
a12fe13d
TT
1448 switch (opcode)
1449 {
1450 case op_nop:
1451 case op_aconst_null:
1452 case op_iconst_m1:
1453 case op_iconst_0:
1454 case op_iconst_1:
1455 case op_iconst_2:
1456 case op_iconst_3:
1457 case op_iconst_4:
1458 case op_iconst_5:
1459 case op_lconst_0:
1460 case op_lconst_1:
1461 case op_fconst_0:
1462 case op_fconst_1:
1463 case op_fconst_2:
1464 case op_dconst_0:
1465 case op_dconst_1:
1466 case op_iload_0:
1467 case op_iload_1:
1468 case op_iload_2:
1469 case op_iload_3:
1470 case op_lload_0:
1471 case op_lload_1:
1472 case op_lload_2:
1473 case op_lload_3:
1474 case op_fload_0:
1475 case op_fload_1:
1476 case op_fload_2:
1477 case op_fload_3:
1478 case op_dload_0:
1479 case op_dload_1:
1480 case op_dload_2:
1481 case op_dload_3:
1482 case op_aload_0:
1483 case op_aload_1:
1484 case op_aload_2:
1485 case op_aload_3:
1486 case op_iaload:
1487 case op_laload:
1488 case op_faload:
1489 case op_daload:
1490 case op_aaload:
1491 case op_baload:
1492 case op_caload:
1493 case op_saload:
1494 case op_istore_0:
1495 case op_istore_1:
1496 case op_istore_2:
1497 case op_istore_3:
1498 case op_lstore_0:
1499 case op_lstore_1:
1500 case op_lstore_2:
1501 case op_lstore_3:
1502 case op_fstore_0:
1503 case op_fstore_1:
1504 case op_fstore_2:
1505 case op_fstore_3:
1506 case op_dstore_0:
1507 case op_dstore_1:
1508 case op_dstore_2:
1509 case op_dstore_3:
1510 case op_astore_0:
1511 case op_astore_1:
1512 case op_astore_2:
1513 case op_astore_3:
1514 case op_iastore:
1515 case op_lastore:
1516 case op_fastore:
1517 case op_dastore:
1518 case op_aastore:
1519 case op_bastore:
1520 case op_castore:
1521 case op_sastore:
1522 case op_pop:
1523 case op_pop2:
1524 case op_dup:
1525 case op_dup_x1:
1526 case op_dup_x2:
1527 case op_dup2:
1528 case op_dup2_x1:
1529 case op_dup2_x2:
1530 case op_swap:
1531 case op_iadd:
1532 case op_isub:
1533 case op_imul:
1534 case op_idiv:
1535 case op_irem:
1536 case op_ishl:
1537 case op_ishr:
1538 case op_iushr:
1539 case op_iand:
1540 case op_ior:
1541 case op_ixor:
1542 case op_ladd:
1543 case op_lsub:
1544 case op_lmul:
1545 case op_ldiv:
1546 case op_lrem:
1547 case op_lshl:
1548 case op_lshr:
1549 case op_lushr:
1550 case op_land:
1551 case op_lor:
1552 case op_lxor:
1553 case op_fadd:
1554 case op_fsub:
1555 case op_fmul:
1556 case op_fdiv:
1557 case op_frem:
1558 case op_dadd:
1559 case op_dsub:
1560 case op_dmul:
1561 case op_ddiv:
1562 case op_drem:
1563 case op_ineg:
1564 case op_i2b:
1565 case op_i2c:
1566 case op_i2s:
1567 case op_lneg:
1568 case op_fneg:
1569 case op_dneg:
a12fe13d
TT
1570 case op_i2l:
1571 case op_i2f:
1572 case op_i2d:
1573 case op_l2i:
1574 case op_l2f:
1575 case op_l2d:
1576 case op_f2i:
1577 case op_f2l:
1578 case op_f2d:
1579 case op_d2i:
1580 case op_d2l:
1581 case op_d2f:
1582 case op_lcmp:
1583 case op_fcmpl:
1584 case op_fcmpg:
1585 case op_dcmpl:
1586 case op_dcmpg:
1587 case op_monitorenter:
1588 case op_monitorexit:
1589 case op_ireturn:
1590 case op_lreturn:
1591 case op_freturn:
1592 case op_dreturn:
1593 case op_areturn:
1594 case op_return:
1595 case op_athrow:
8051c3d6 1596 case op_arraylength:
a12fe13d
TT
1597 break;
1598
1599 case op_bipush:
a12fe13d
TT
1600 case op_ldc:
1601 case op_iload:
1602 case op_lload:
1603 case op_fload:
1604 case op_dload:
1605 case op_aload:
1606 case op_istore:
1607 case op_lstore:
1608 case op_fstore:
1609 case op_dstore:
1610 case op_astore:
a12fe13d 1611 case op_ret:
7db43d37 1612 case op_newarray:
a12fe13d
TT
1613 get_byte ();
1614 break;
1615
8051c3d6 1616 case op_iinc:
7db43d37 1617 case op_sipush:
a12fe13d
TT
1618 case op_ldc_w:
1619 case op_ldc2_w:
1620 case op_getstatic:
1621 case op_getfield:
1622 case op_putfield:
1623 case op_putstatic:
1624 case op_new:
1625 case op_anewarray:
1626 case op_instanceof:
1627 case op_checkcast:
1628 case op_invokespecial:
1629 case op_invokestatic:
1630 case op_invokevirtual:
1631 get_short ();
1632 break;
1633
1634 case op_multianewarray:
1635 get_short ();
1636 get_byte ();
1637 break;
1638
1639 case op_jsr:
1640 last_was_jsr = true;
1641 // Fall through.
1642 case op_ifeq:
1643 case op_ifne:
1644 case op_iflt:
1645 case op_ifge:
1646 case op_ifgt:
1647 case op_ifle:
1648 case op_if_icmpeq:
1649 case op_if_icmpne:
1650 case op_if_icmplt:
1651 case op_if_icmpge:
1652 case op_if_icmpgt:
1653 case op_if_icmple:
1654 case op_if_acmpeq:
1655 case op_if_acmpne:
1656 case op_ifnull:
1657 case op_ifnonnull:
1658 case op_goto:
1659 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1660 break;
1661
1662 case op_tableswitch:
1663 {
1664 skip_padding ();
1665 note_branch_target (compute_jump (get_int ()));
1666 jint low = get_int ();
1667 jint hi = get_int ();
1668 if (low > hi)
60440707 1669 verify_fail ("invalid tableswitch", start_PC);
a12fe13d
TT
1670 for (int i = low; i <= hi; ++i)
1671 note_branch_target (compute_jump (get_int ()));
1672 }
1673 break;
1674
1675 case op_lookupswitch:
1676 {
1677 skip_padding ();
1678 note_branch_target (compute_jump (get_int ()));
1679 int npairs = get_int ();
1680 if (npairs < 0)
60440707 1681 verify_fail ("too few pairs in lookupswitch", start_PC);
a12fe13d
TT
1682 while (npairs-- > 0)
1683 {
1684 get_int ();
1685 note_branch_target (compute_jump (get_int ()));
1686 }
1687 }
1688 break;
1689
1690 case op_invokeinterface:
1691 get_short ();
1692 get_byte ();
1693 get_byte ();
1694 break;
1695
1696 case op_wide:
1697 {
fa88ce26 1698 opcode = (java_opcode) get_byte ();
a12fe13d 1699 get_short ();
fa88ce26 1700 if (opcode == op_iinc)
a12fe13d
TT
1701 get_short ();
1702 }
1703 break;
1704
1705 case op_jsr_w:
1706 last_was_jsr = true;
1707 // Fall through.
1708 case op_goto_w:
1709 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1710 break;
1711
1712 default:
60440707
TT
1713 verify_fail ("unrecognized instruction in branch_prepass",
1714 start_PC);
a12fe13d
TT
1715 }
1716
1717 // See if any previous branch tried to branch to the middle of
1718 // this instruction.
1719 for (int pc = start_PC + 1; pc < PC; ++pc)
1720 {
1721 if ((flags[pc] & FLAG_BRANCH_TARGET))
60440707 1722 verify_fail ("branch to middle of instruction", pc);
a12fe13d
TT
1723 }
1724 }
1725
1726 // Verify exception handlers.
1727 for (int i = 0; i < current_method->exc_count; ++i)
1728 {
1729 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
60440707
TT
1730 verify_fail ("exception handler not at instruction start",
1731 exception[i].handler_pc);
60440707
TT
1732 if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
1733 verify_fail ("exception start not at instruction start",
1734 exception[i].start_pc);
ef9f3bc4
TT
1735 if (exception[i].end_pc != current_method->code_length
1736 && ! (flags[exception[i].end_pc] & FLAG_INSN_START))
60440707
TT
1737 verify_fail ("exception end not at instruction start",
1738 exception[i].end_pc);
a12fe13d
TT
1739
1740 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1741 }
1742 }
1743
1744 void check_pool_index (int index)
1745 {
1746 if (index < 0 || index >= current_class->constants.size)
60440707 1747 verify_fail ("constant pool index out of range", start_PC);
a12fe13d
TT
1748 }
1749
1750 type check_class_constant (int index)
1751 {
1752 check_pool_index (index);
1753 _Jv_Constants *pool = &current_class->constants;
1754 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1755 return type (pool->data[index].clazz);
1756 else if (pool->tags[index] == JV_CONSTANT_Class)
1757 return type (pool->data[index].utf8);
60440707 1758 verify_fail ("expected class constant", start_PC);
a12fe13d
TT
1759 }
1760
1761 type check_constant (int index)
1762 {
1763 check_pool_index (index);
1764 _Jv_Constants *pool = &current_class->constants;
1765 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1766 || pool->tags[index] == JV_CONSTANT_String)
1767 return type (&java::lang::String::class$);
1768 else if (pool->tags[index] == JV_CONSTANT_Integer)
1769 return type (int_type);
1770 else if (pool->tags[index] == JV_CONSTANT_Float)
1771 return type (float_type);
60440707
TT
1772 verify_fail ("String, int, or float constant expected", start_PC);
1773 }
1774
1775 type check_wide_constant (int index)
1776 {
1777 check_pool_index (index);
1778 _Jv_Constants *pool = &current_class->constants;
1779 if (pool->tags[index] == JV_CONSTANT_Long)
1780 return type (long_type);
1781 else if (pool->tags[index] == JV_CONSTANT_Double)
1782 return type (double_type);
1783 verify_fail ("long or double constant expected", start_PC);
a12fe13d
TT
1784 }
1785
1786 // Helper for both field and method. These are laid out the same in
1787 // the constant pool.
1788 type handle_field_or_method (int index, int expected,
1789 _Jv_Utf8Const **name,
1790 _Jv_Utf8Const **fmtype)
1791 {
1792 check_pool_index (index);
1793 _Jv_Constants *pool = &current_class->constants;
1794 if (pool->tags[index] != expected)
60440707 1795 verify_fail ("didn't see expected constant", start_PC);
a12fe13d
TT
1796 // Once we know we have a Fieldref or Methodref we assume that it
1797 // is correctly laid out in the constant pool. I think the code
1798 // in defineclass.cc guarantees this.
1799 _Jv_ushort class_index, name_and_type_index;
1800 _Jv_loadIndexes (&pool->data[index],
1801 class_index,
1802 name_and_type_index);
1803 _Jv_ushort name_index, desc_index;
1804 _Jv_loadIndexes (&pool->data[name_and_type_index],
1805 name_index, desc_index);
1806
1807 *name = pool->data[name_index].utf8;
1808 *fmtype = pool->data[desc_index].utf8;
1809
1810 return check_class_constant (class_index);
1811 }
1812
1813 // Return field's type, compute class' type if requested.
1814 type check_field_constant (int index, type *class_type = NULL)
1815 {
1816 _Jv_Utf8Const *name, *field_type;
1817 type ct = handle_field_or_method (index,
1818 JV_CONSTANT_Fieldref,
1819 &name, &field_type);
1820 if (class_type)
1821 *class_type = ct;
b34e9a5b
TT
1822 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1823 return type (field_type);
1824 return get_type_val_for_signature (field_type->data[0]);
a12fe13d
TT
1825 }
1826
1827 type check_method_constant (int index, bool is_interface,
1828 _Jv_Utf8Const **method_name,
1829 _Jv_Utf8Const **method_signature)
1830 {
1831 return handle_field_or_method (index,
1832 (is_interface
1833 ? JV_CONSTANT_InterfaceMethodref
1834 : JV_CONSTANT_Methodref),
1835 method_name, method_signature);
1836 }
1837
1838 type get_one_type (char *&p)
1839 {
1840 char *start = p;
1841
1842 int arraycount = 0;
1843 while (*p == '[')
1844 {
1845 ++arraycount;
1846 ++p;
1847 }
1848
1849 char v = *p++;
1850
1851 if (v == 'L')
1852 {
1853 while (*p != ';')
1854 ++p;
1855 ++p;
0c88d7f8 1856 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
a12fe13d
TT
1857 return type (name);
1858 }
1859
1860 // Casting to jchar here is ok since we are looking at an ASCII
1861 // character.
1862 type_val rt = get_type_val_for_signature (jchar (v));
1863
1864 if (arraycount == 0)
f6b733ed
TT
1865 {
1866 // Callers of this function eventually push their arguments on
1867 // the stack. So, promote them here.
1868 return type (rt).promote ();
1869 }
a12fe13d
TT
1870
1871 jclass k = construct_primitive_array_type (rt);
1872 while (--arraycount > 0)
1873 k = _Jv_GetArrayClass (k, NULL);
1874 return type (k);
1875 }
1876
1877 void compute_argument_types (_Jv_Utf8Const *signature,
1878 type *types)
1879 {
1880 char *p = signature->data;
1881 // Skip `('.
1882 ++p;
1883
1884 int i = 0;
1885 while (*p != ')')
1886 types[i++] = get_one_type (p);
1887 }
1888
1889 type compute_return_type (_Jv_Utf8Const *signature)
1890 {
1891 char *p = signature->data;
1892 while (*p != ')')
1893 ++p;
1894 ++p;
1895 return get_one_type (p);
1896 }
1897
590077b0 1898 void check_return_type (type onstack)
a12fe13d
TT
1899 {
1900 type rt = compute_return_type (current_method->self->signature);
f70443f7
PB
1901 if (! rt.compatible (onstack, this))
1902 verify_fail ("incompatible return type");
a12fe13d
TT
1903 }
1904
6d8b1244
TT
1905 // Initialize the stack for the new method. Returns true if this
1906 // method is an instance initializer.
1907 bool initialize_stack ()
1908 {
1909 int var = 0;
1910 bool is_init = false;
1911
1912 using namespace java::lang::reflect;
1913 if (! Modifier::isStatic (current_method->self->accflags))
1914 {
1915 type kurr (current_class);
1916 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1917 {
f70443f7 1918 kurr.set_uninitialized (type::SELF, this);
6d8b1244
TT
1919 is_init = true;
1920 }
1921 set_variable (0, kurr);
1922 current_state->set_this_type (kurr);
1923 ++var;
1924 }
1925
1926 // We have to handle wide arguments specially here.
1927 int arg_count = _Jv_count_arguments (current_method->self->signature);
1928 type arg_types[arg_count];
1929 compute_argument_types (current_method->self->signature, arg_types);
1930 for (int i = 0; i < arg_count; ++i)
1931 {
1932 set_variable (var, arg_types[i]);
1933 ++var;
1934 if (arg_types[i].iswide ())
1935 ++var;
1936 }
1937
1938 return is_init;
1939 }
1940
a12fe13d
TT
1941 void verify_instructions_0 ()
1942 {
1943 current_state = new state (current_method->max_stack,
1944 current_method->max_locals);
1945
1946 PC = 0;
60440707 1947 start_PC = 0;
a12fe13d 1948
6d8b1244
TT
1949 // True if we are verifying an instance initializer.
1950 bool this_is_init = initialize_stack ();
a12fe13d
TT
1951
1952 states = (state **) _Jv_Malloc (sizeof (state *)
1953 * current_method->code_length);
1954 for (int i = 0; i < current_method->code_length; ++i)
1955 states[i] = NULL;
1956
1957 next_verify_pc = state::NO_NEXT;
1958
1959 while (true)
1960 {
1961 // If the PC was invalidated, get a new one from the work list.
1962 if (PC == state::NO_NEXT)
1963 {
1964 PC = pop_jump ();
1965 if (PC == state::INVALID)
ef9f3bc4 1966 verify_fail ("can't happen: saw state::INVALID");
a12fe13d
TT
1967 if (PC == state::NO_NEXT)
1968 break;
1969 // Set up the current state.
ef9f3bc4
TT
1970 current_state->copy (states[PC], current_method->max_stack,
1971 current_method->max_locals);
a12fe13d 1972 }
ef9f3bc4 1973 else
a12fe13d 1974 {
ef9f3bc4
TT
1975 // Control can't fall off the end of the bytecode. We
1976 // only need to check this in the fall-through case,
1977 // because branch bounds are checked when they are
1978 // pushed.
1979 if (PC >= current_method->code_length)
1980 verify_fail ("fell off end");
1981
1982 // We only have to do this checking in the situation where
1983 // control flow falls through from the previous
1984 // instruction. Otherwise merging is done at the time we
1985 // push the branch.
1986 if (states[PC] != NULL)
a12fe13d 1987 {
ef9f3bc4
TT
1988 // We've already visited this instruction. So merge
1989 // the states together. If this yields no change then
c1bf99a2
TT
1990 // we don't have to re-verify. However, if the new
1991 // state is an the result of an unmerged `ret', we
1992 // must continue through it.
1993 debug_print ("== Fall through merge\n");
1994 states[PC]->print ("Old", PC, current_method->max_stack,
1995 current_method->max_locals);
1996 current_state->print ("Cur", PC, current_method->max_stack,
1997 current_method->max_locals);
ef9f3bc4 1998 if (! current_state->merge (states[PC], false,
f70443f7 1999 current_method->max_locals, this)
c1bf99a2 2000 && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
ef9f3bc4 2001 {
c1bf99a2 2002 debug_print ("== Fall through optimization\n");
ef9f3bc4
TT
2003 invalidate_pc ();
2004 continue;
2005 }
2006 // Save a copy of it for later.
2007 states[PC]->copy (current_state, current_method->max_stack,
2008 current_method->max_locals);
c1bf99a2
TT
2009 current_state->print ("New", PC, current_method->max_stack,
2010 current_method->max_locals);
a12fe13d 2011 }
a12fe13d 2012 }
ef9f3bc4
TT
2013
2014 // We only have to keep saved state at branch targets. If
2015 // we're at a branch target and the state here hasn't been set
2016 // yet, we set it now.
2017 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
a12fe13d 2018 {
a12fe13d
TT
2019 states[PC] = new state (current_state, current_method->max_stack,
2020 current_method->max_locals);
2021 }
2022
c1bf99a2
TT
2023 // Set this before handling exceptions so that debug output is
2024 // sane.
2025 start_PC = PC;
2026
a12fe13d
TT
2027 // Update states for all active exception handlers. Ordinarily
2028 // there are not many exception handlers. So we simply run
2029 // through them all.
2030 for (int i = 0; i < current_method->exc_count; ++i)
2031 {
2032 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
2033 {
ef9f3bc4 2034 type handler (&java::lang::Throwable::class$);
a12fe13d
TT
2035 if (exception[i].handler_type != 0)
2036 handler = check_class_constant (exception[i].handler_type);
2037 push_exception_jump (handler, exception[i].handler_pc);
2038 }
2039 }
2040
c1bf99a2
TT
2041 current_state->print (" ", PC, current_method->max_stack,
2042 current_method->max_locals);
fa88ce26 2043 java_opcode opcode = (java_opcode) bytecode[PC++];
a12fe13d
TT
2044 switch (opcode)
2045 {
2046 case op_nop:
2047 break;
2048
2049 case op_aconst_null:
2050 push_type (null_type);
2051 break;
2052
2053 case op_iconst_m1:
2054 case op_iconst_0:
2055 case op_iconst_1:
2056 case op_iconst_2:
2057 case op_iconst_3:
2058 case op_iconst_4:
2059 case op_iconst_5:
2060 push_type (int_type);
2061 break;
2062
2063 case op_lconst_0:
2064 case op_lconst_1:
2065 push_type (long_type);
2066 break;
2067
2068 case op_fconst_0:
2069 case op_fconst_1:
2070 case op_fconst_2:
2071 push_type (float_type);
2072 break;
2073
2074 case op_dconst_0:
2075 case op_dconst_1:
2076 push_type (double_type);
2077 break;
2078
2079 case op_bipush:
2080 get_byte ();
2081 push_type (int_type);
2082 break;
2083
2084 case op_sipush:
2085 get_short ();
2086 push_type (int_type);
2087 break;
2088
2089 case op_ldc:
2090 push_type (check_constant (get_byte ()));
2091 break;
2092 case op_ldc_w:
2093 push_type (check_constant (get_ushort ()));
2094 break;
2095 case op_ldc2_w:
60440707 2096 push_type (check_wide_constant (get_ushort ()));
a12fe13d
TT
2097 break;
2098
2099 case op_iload:
2100 push_type (get_variable (get_byte (), int_type));
2101 break;
2102 case op_lload:
2103 push_type (get_variable (get_byte (), long_type));
2104 break;
2105 case op_fload:
2106 push_type (get_variable (get_byte (), float_type));
2107 break;
2108 case op_dload:
2109 push_type (get_variable (get_byte (), double_type));
2110 break;
2111 case op_aload:
2112 push_type (get_variable (get_byte (), reference_type));
2113 break;
2114
2115 case op_iload_0:
2116 case op_iload_1:
2117 case op_iload_2:
2118 case op_iload_3:
2119 push_type (get_variable (opcode - op_iload_0, int_type));
2120 break;
2121 case op_lload_0:
2122 case op_lload_1:
2123 case op_lload_2:
2124 case op_lload_3:
2125 push_type (get_variable (opcode - op_lload_0, long_type));
2126 break;
2127 case op_fload_0:
2128 case op_fload_1:
2129 case op_fload_2:
2130 case op_fload_3:
2131 push_type (get_variable (opcode - op_fload_0, float_type));
2132 break;
2133 case op_dload_0:
2134 case op_dload_1:
2135 case op_dload_2:
2136 case op_dload_3:
2137 push_type (get_variable (opcode - op_dload_0, double_type));
2138 break;
2139 case op_aload_0:
2140 case op_aload_1:
2141 case op_aload_2:
2142 case op_aload_3:
2143 push_type (get_variable (opcode - op_aload_0, reference_type));
2144 break;
2145 case op_iaload:
2146 pop_type (int_type);
2147 push_type (require_array_type (pop_type (reference_type),
2148 int_type));
2149 break;
2150 case op_laload:
2151 pop_type (int_type);
2152 push_type (require_array_type (pop_type (reference_type),
2153 long_type));
2154 break;
2155 case op_faload:
2156 pop_type (int_type);
2157 push_type (require_array_type (pop_type (reference_type),
2158 float_type));
2159 break;
2160 case op_daload:
2161 pop_type (int_type);
2162 push_type (require_array_type (pop_type (reference_type),
2163 double_type));
2164 break;
2165 case op_aaload:
2166 pop_type (int_type);
2167 push_type (require_array_type (pop_type (reference_type),
2168 reference_type));
2169 break;
2170 case op_baload:
2171 pop_type (int_type);
2172 require_array_type (pop_type (reference_type), byte_type);
2173 push_type (int_type);
2174 break;
2175 case op_caload:
2176 pop_type (int_type);
2177 require_array_type (pop_type (reference_type), char_type);
2178 push_type (int_type);
2179 break;
2180 case op_saload:
2181 pop_type (int_type);
2182 require_array_type (pop_type (reference_type), short_type);
2183 push_type (int_type);
2184 break;
2185 case op_istore:
2186 set_variable (get_byte (), pop_type (int_type));
2187 break;
2188 case op_lstore:
2189 set_variable (get_byte (), pop_type (long_type));
2190 break;
2191 case op_fstore:
2192 set_variable (get_byte (), pop_type (float_type));
2193 break;
2194 case op_dstore:
2195 set_variable (get_byte (), pop_type (double_type));
2196 break;
2197 case op_astore:
ef9f3bc4 2198 set_variable (get_byte (), pop_ref_or_return ());
a12fe13d
TT
2199 break;
2200 case op_istore_0:
2201 case op_istore_1:
2202 case op_istore_2:
2203 case op_istore_3:
2204 set_variable (opcode - op_istore_0, pop_type (int_type));
2205 break;
2206 case op_lstore_0:
2207 case op_lstore_1:
2208 case op_lstore_2:
2209 case op_lstore_3:
2210 set_variable (opcode - op_lstore_0, pop_type (long_type));
2211 break;
2212 case op_fstore_0:
2213 case op_fstore_1:
2214 case op_fstore_2:
2215 case op_fstore_3:
2216 set_variable (opcode - op_fstore_0, pop_type (float_type));
2217 break;
2218 case op_dstore_0:
2219 case op_dstore_1:
2220 case op_dstore_2:
2221 case op_dstore_3:
2222 set_variable (opcode - op_dstore_0, pop_type (double_type));
2223 break;
2224 case op_astore_0:
2225 case op_astore_1:
2226 case op_astore_2:
2227 case op_astore_3:
ef9f3bc4 2228 set_variable (opcode - op_astore_0, pop_ref_or_return ());
a12fe13d
TT
2229 break;
2230 case op_iastore:
2231 pop_type (int_type);
2232 pop_type (int_type);
2233 require_array_type (pop_type (reference_type), int_type);
2234 break;
2235 case op_lastore:
2236 pop_type (long_type);
2237 pop_type (int_type);
2238 require_array_type (pop_type (reference_type), long_type);
2239 break;
2240 case op_fastore:
2241 pop_type (float_type);
2242 pop_type (int_type);
2243 require_array_type (pop_type (reference_type), float_type);
2244 break;
2245 case op_dastore:
2246 pop_type (double_type);
2247 pop_type (int_type);
2248 require_array_type (pop_type (reference_type), double_type);
2249 break;
2250 case op_aastore:
2251 pop_type (reference_type);
2252 pop_type (int_type);
2253 require_array_type (pop_type (reference_type), reference_type);
2254 break;
2255 case op_bastore:
2256 pop_type (int_type);
2257 pop_type (int_type);
2258 require_array_type (pop_type (reference_type), byte_type);
2259 break;
2260 case op_castore:
2261 pop_type (int_type);
2262 pop_type (int_type);
2263 require_array_type (pop_type (reference_type), char_type);
2264 break;
2265 case op_sastore:
2266 pop_type (int_type);
2267 pop_type (int_type);
2268 require_array_type (pop_type (reference_type), short_type);
2269 break;
2270 case op_pop:
2271 pop32 ();
2272 break;
2273 case op_pop2:
2274 pop64 ();
2275 break;
2276 case op_dup:
2277 {
2278 type t = pop32 ();
2279 push_type (t);
2280 push_type (t);
2281 }
2282 break;
2283 case op_dup_x1:
2284 {
2285 type t1 = pop32 ();
2286 type t2 = pop32 ();
2287 push_type (t1);
2288 push_type (t2);
2289 push_type (t1);
2290 }
2291 break;
2292 case op_dup_x2:
2293 {
2294 type t1 = pop32 ();
6c5a8271
TT
2295 type t2 = pop_raw ();
2296 if (! t2.iswide ())
2297 {
2298 type t3 = pop32 ();
2299 push_type (t1);
2300 push_type (t3);
2301 }
2302 else
2303 push_type (t1);
a12fe13d
TT
2304 push_type (t2);
2305 push_type (t1);
2306 }
2307 break;
2308 case op_dup2:
2309 {
6c5a8271
TT
2310 type t = pop_raw ();
2311 if (! t.iswide ())
2312 {
2313 type t2 = pop32 ();
2314 push_type (t2);
2315 push_type (t);
2316 push_type (t2);
2317 }
a12fe13d
TT
2318 push_type (t);
2319 }
2320 break;
2321 case op_dup2_x1:
2322 {
6c5a8271
TT
2323 type t1 = pop_raw ();
2324 type t2 = pop32 ();
2325 if (! t1.iswide ())
2326 {
2327 type t3 = pop32 ();
2328 push_type (t2);
2329 push_type (t1);
2330 push_type (t3);
2331 }
2332 else
2333 push_type (t1);
a12fe13d
TT
2334 push_type (t2);
2335 push_type (t1);
2336 }
2337 break;
2338 case op_dup2_x2:
2339 {
6c5a8271
TT
2340 type t1 = pop_raw ();
2341 if (t1.iswide ())
2342 {
2343 type t2 = pop_raw ();
2344 if (t2.iswide ())
2345 {
2346 push_type (t1);
2347 push_type (t2);
2348 }
2349 else
2350 {
2351 type t3 = pop32 ();
2352 push_type (t1);
2353 push_type (t3);
2354 push_type (t2);
2355 }
2356 push_type (t1);
2357 }
2358 else
2359 {
2360 type t2 = pop32 ();
2361 type t3 = pop_raw ();
2362 if (t3.iswide ())
2363 {
2364 push_type (t2);
2365 push_type (t1);
2366 }
2367 else
2368 {
2369 type t4 = pop32 ();
2370 push_type (t2);
2371 push_type (t1);
2372 push_type (t4);
2373 }
2374 push_type (t3);
2375 push_type (t2);
2376 push_type (t1);
2377 }
a12fe13d
TT
2378 }
2379 break;
2380 case op_swap:
2381 {
2382 type t1 = pop32 ();
2383 type t2 = pop32 ();
2384 push_type (t1);
2385 push_type (t2);
2386 }
2387 break;
2388 case op_iadd:
2389 case op_isub:
2390 case op_imul:
2391 case op_idiv:
2392 case op_irem:
2393 case op_ishl:
2394 case op_ishr:
2395 case op_iushr:
2396 case op_iand:
2397 case op_ior:
2398 case op_ixor:
2399 pop_type (int_type);
2400 push_type (pop_type (int_type));
2401 break;
2402 case op_ladd:
2403 case op_lsub:
2404 case op_lmul:
2405 case op_ldiv:
2406 case op_lrem:
a12fe13d
TT
2407 case op_land:
2408 case op_lor:
2409 case op_lxor:
2410 pop_type (long_type);
2411 push_type (pop_type (long_type));
2412 break;
94e1e142
TT
2413 case op_lshl:
2414 case op_lshr:
2415 case op_lushr:
2416 pop_type (int_type);
2417 push_type (pop_type (long_type));
2418 break;
a12fe13d
TT
2419 case op_fadd:
2420 case op_fsub:
2421 case op_fmul:
2422 case op_fdiv:
2423 case op_frem:
2424 pop_type (float_type);
2425 push_type (pop_type (float_type));
2426 break;
2427 case op_dadd:
2428 case op_dsub:
2429 case op_dmul:
2430 case op_ddiv:
2431 case op_drem:
2432 pop_type (double_type);
2433 push_type (pop_type (double_type));
2434 break;
2435 case op_ineg:
2436 case op_i2b:
2437 case op_i2c:
2438 case op_i2s:
2439 push_type (pop_type (int_type));
2440 break;
2441 case op_lneg:
2442 push_type (pop_type (long_type));
2443 break;
2444 case op_fneg:
2445 push_type (pop_type (float_type));
2446 break;
2447 case op_dneg:
2448 push_type (pop_type (double_type));
2449 break;
2450 case op_iinc:
2451 get_variable (get_byte (), int_type);
2452 get_byte ();
2453 break;
2454 case op_i2l:
2455 pop_type (int_type);
2456 push_type (long_type);
2457 break;
2458 case op_i2f:
2459 pop_type (int_type);
2460 push_type (float_type);
2461 break;
2462 case op_i2d:
2463 pop_type (int_type);
2464 push_type (double_type);
2465 break;
2466 case op_l2i:
2467 pop_type (long_type);
2468 push_type (int_type);
2469 break;
2470 case op_l2f:
2471 pop_type (long_type);
2472 push_type (float_type);
2473 break;
2474 case op_l2d:
2475 pop_type (long_type);
2476 push_type (double_type);
2477 break;
2478 case op_f2i:
2479 pop_type (float_type);
2480 push_type (int_type);
2481 break;
2482 case op_f2l:
2483 pop_type (float_type);
2484 push_type (long_type);
2485 break;
2486 case op_f2d:
2487 pop_type (float_type);
2488 push_type (double_type);
2489 break;
2490 case op_d2i:
2491 pop_type (double_type);
2492 push_type (int_type);
2493 break;
2494 case op_d2l:
2495 pop_type (double_type);
2496 push_type (long_type);
2497 break;
2498 case op_d2f:
2499 pop_type (double_type);
2500 push_type (float_type);
2501 break;
2502 case op_lcmp:
2503 pop_type (long_type);
2504 pop_type (long_type);
2505 push_type (int_type);
2506 break;
2507 case op_fcmpl:
2508 case op_fcmpg:
2509 pop_type (float_type);
2510 pop_type (float_type);
2511 push_type (int_type);
2512 break;
2513 case op_dcmpl:
2514 case op_dcmpg:
2515 pop_type (double_type);
2516 pop_type (double_type);
2517 push_type (int_type);
2518 break;
2519 case op_ifeq:
2520 case op_ifne:
2521 case op_iflt:
2522 case op_ifge:
2523 case op_ifgt:
2524 case op_ifle:
2525 pop_type (int_type);
2526 push_jump (get_short ());
2527 break;
2528 case op_if_icmpeq:
2529 case op_if_icmpne:
2530 case op_if_icmplt:
2531 case op_if_icmpge:
2532 case op_if_icmpgt:
2533 case op_if_icmple:
2534 pop_type (int_type);
2535 pop_type (int_type);
2536 push_jump (get_short ());
2537 break;
2538 case op_if_acmpeq:
2539 case op_if_acmpne:
2540 pop_type (reference_type);
2541 pop_type (reference_type);
2542 push_jump (get_short ());
2543 break;
2544 case op_goto:
2545 push_jump (get_short ());
2546 invalidate_pc ();
2547 break;
2548 case op_jsr:
2549 handle_jsr_insn (get_short ());
2550 break;
2551 case op_ret:
2552 handle_ret_insn (get_byte ());
2553 break;
2554 case op_tableswitch:
2555 {
2556 pop_type (int_type);
2557 skip_padding ();
2558 push_jump (get_int ());
2559 jint low = get_int ();
2560 jint high = get_int ();
2561 // Already checked LOW -vs- HIGH.
2562 for (int i = low; i <= high; ++i)
2563 push_jump (get_int ());
2564 invalidate_pc ();
2565 }
2566 break;
2567
2568 case op_lookupswitch:
2569 {
2570 pop_type (int_type);
2571 skip_padding ();
2572 push_jump (get_int ());
2573 jint npairs = get_int ();
2574 // Already checked NPAIRS >= 0.
2575 jint lastkey = 0;
2576 for (int i = 0; i < npairs; ++i)
2577 {
2578 jint key = get_int ();
2579 if (i > 0 && key <= lastkey)
60440707 2580 verify_fail ("lookupswitch pairs unsorted", start_PC);
a12fe13d
TT
2581 lastkey = key;
2582 push_jump (get_int ());
2583 }
2584 invalidate_pc ();
2585 }
2586 break;
2587 case op_ireturn:
2588 check_return_type (pop_type (int_type));
2589 invalidate_pc ();
2590 break;
2591 case op_lreturn:
2592 check_return_type (pop_type (long_type));
2593 invalidate_pc ();
2594 break;
2595 case op_freturn:
2596 check_return_type (pop_type (float_type));
2597 invalidate_pc ();
2598 break;
2599 case op_dreturn:
2600 check_return_type (pop_type (double_type));
2601 invalidate_pc ();
2602 break;
2603 case op_areturn:
2604 check_return_type (pop_type (reference_type));
2605 invalidate_pc ();
2606 break;
2607 case op_return:
6d8b1244
TT
2608 // We only need to check this when the return type is
2609 // void, because all instance initializers return void.
2610 if (this_is_init)
f70443f7 2611 current_state->check_this_initialized (this);
a12fe13d
TT
2612 check_return_type (void_type);
2613 invalidate_pc ();
2614 break;
2615 case op_getstatic:
2616 push_type (check_field_constant (get_ushort ()));
2617 break;
2618 case op_putstatic:
2619 pop_type (check_field_constant (get_ushort ()));
2620 break;
2621 case op_getfield:
2622 {
2623 type klass;
2624 type field = check_field_constant (get_ushort (), &klass);
2625 pop_type (klass);
2626 push_type (field);
2627 }
2628 break;
2629 case op_putfield:
2630 {
2631 type klass;
2632 type field = check_field_constant (get_ushort (), &klass);
2633 pop_type (field);
6d8b1244
TT
2634
2635 // We have an obscure special case here: we can use
2636 // `putfield' on a field declared in this class, even if
2637 // `this' has not yet been initialized.
2638 if (! current_state->this_type.isinitialized ()
2639 && current_state->this_type.pc == type::SELF)
f70443f7 2640 klass.set_uninitialized (type::SELF, this);
a12fe13d
TT
2641 pop_type (klass);
2642 }
2643 break;
2644
2645 case op_invokevirtual:
2646 case op_invokespecial:
2647 case op_invokestatic:
2648 case op_invokeinterface:
2649 {
2650 _Jv_Utf8Const *method_name, *method_signature;
2651 type class_type
2652 = check_method_constant (get_ushort (),
fa88ce26 2653 opcode == op_invokeinterface,
a12fe13d
TT
2654 &method_name,
2655 &method_signature);
8987cc88
TT
2656 // NARGS is only used when we're processing
2657 // invokeinterface. It is simplest for us to compute it
2658 // here and then verify it later.
2659 int nargs = 0;
fa88ce26 2660 if (opcode == op_invokeinterface)
a12fe13d 2661 {
8987cc88 2662 nargs = get_byte ();
a12fe13d 2663 if (get_byte () != 0)
f70443f7 2664 verify_fail ("invokeinterface dummy byte is wrong");
a12fe13d
TT
2665 }
2666
2667 bool is_init = false;
2668 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2669 {
2670 is_init = true;
fa88ce26 2671 if (opcode != op_invokespecial)
f70443f7 2672 verify_fail ("can't invoke <init>");
a12fe13d
TT
2673 }
2674 else if (method_name->data[0] == '<')
f70443f7 2675 verify_fail ("can't invoke method starting with `<'");
a12fe13d
TT
2676
2677 // Pop arguments and check types.
8987cc88 2678 int arg_count = _Jv_count_arguments (method_signature);
a12fe13d
TT
2679 type arg_types[arg_count];
2680 compute_argument_types (method_signature, arg_types);
2681 for (int i = arg_count - 1; i >= 0; --i)
8987cc88
TT
2682 {
2683 // This is only used for verifying the byte for
2684 // invokeinterface.
2685 nargs -= arg_types[i].depth ();
2686 pop_type (arg_types[i]);
2687 }
2688
2689 if (opcode == op_invokeinterface
2690 && nargs != 1)
2691 verify_fail ("wrong argument count for invokeinterface");
a12fe13d 2692
fa88ce26 2693 if (opcode != op_invokestatic)
a12fe13d
TT
2694 {
2695 type t = class_type;
2696 if (is_init)
2697 {
2698 // In this case the PC doesn't matter.
f70443f7 2699 t.set_uninitialized (type::UNINIT, this);
a12fe13d
TT
2700 }
2701 t = pop_type (t);
2702 if (is_init)
2703 current_state->set_initialized (t.get_pc (),
2704 current_method->max_locals);
2705 }
2706
2707 type rt = compute_return_type (method_signature);
2708 if (! rt.isvoid ())
2709 push_type (rt);
2710 }
2711 break;
2712
2713 case op_new:
2714 {
2715 type t = check_class_constant (get_ushort ());
f70443f7
PB
2716 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2717 verify_fail ("type is array, interface, or abstract");
2718 t.set_uninitialized (start_PC, this);
a12fe13d
TT
2719 push_type (t);
2720 }
2721 break;
2722
2723 case op_newarray:
2724 {
2725 int atype = get_byte ();
2726 // We intentionally have chosen constants to make this
2727 // valid.
2728 if (atype < boolean_type || atype > long_type)
60440707 2729 verify_fail ("type not primitive", start_PC);
a12fe13d
TT
2730 pop_type (int_type);
2731 push_type (construct_primitive_array_type (type_val (atype)));
2732 }
2733 break;
2734 case op_anewarray:
2735 pop_type (int_type);
f70443f7 2736 push_type (check_class_constant (get_ushort ()).to_array (this));
a12fe13d
TT
2737 break;
2738 case op_arraylength:
2739 {
2740 type t = pop_type (reference_type);
2741 if (! t.isarray ())
f70443f7 2742 verify_fail ("array type expected");
a12fe13d
TT
2743 push_type (int_type);
2744 }
2745 break;
2746 case op_athrow:
2747 pop_type (type (&java::lang::Throwable::class$));
2748 invalidate_pc ();
2749 break;
2750 case op_checkcast:
2751 pop_type (reference_type);
2752 push_type (check_class_constant (get_ushort ()));
2753 break;
2754 case op_instanceof:
2755 pop_type (reference_type);
2756 check_class_constant (get_ushort ());
2757 push_type (int_type);
2758 break;
2759 case op_monitorenter:
2760 pop_type (reference_type);
2761 break;
2762 case op_monitorexit:
2763 pop_type (reference_type);
2764 break;
2765 case op_wide:
2766 {
2767 switch (get_byte ())
2768 {
2769 case op_iload:
2770 push_type (get_variable (get_ushort (), int_type));
2771 break;
2772 case op_lload:
2773 push_type (get_variable (get_ushort (), long_type));
2774 break;
2775 case op_fload:
2776 push_type (get_variable (get_ushort (), float_type));
2777 break;
2778 case op_dload:
2779 push_type (get_variable (get_ushort (), double_type));
2780 break;
2781 case op_aload:
2782 push_type (get_variable (get_ushort (), reference_type));
2783 break;
2784 case op_istore:
2785 set_variable (get_ushort (), pop_type (int_type));
2786 break;
2787 case op_lstore:
2788 set_variable (get_ushort (), pop_type (long_type));
2789 break;
2790 case op_fstore:
2791 set_variable (get_ushort (), pop_type (float_type));
2792 break;
2793 case op_dstore:
2794 set_variable (get_ushort (), pop_type (double_type));
2795 break;
2796 case op_astore:
2797 set_variable (get_ushort (), pop_type (reference_type));
2798 break;
2799 case op_ret:
2800 handle_ret_insn (get_short ());
2801 break;
2802 case op_iinc:
2803 get_variable (get_ushort (), int_type);
2804 get_short ();
2805 break;
2806 default:
60440707 2807 verify_fail ("unrecognized wide instruction", start_PC);
a12fe13d
TT
2808 }
2809 }
2810 break;
2811 case op_multianewarray:
2812 {
2813 type atype = check_class_constant (get_ushort ());
2814 int dim = get_byte ();
2815 if (dim < 1)
60440707 2816 verify_fail ("too few dimensions to multianewarray", start_PC);
f70443f7 2817 atype.verify_dimensions (dim, this);
a12fe13d
TT
2818 for (int i = 0; i < dim; ++i)
2819 pop_type (int_type);
2820 push_type (atype);
2821 }
2822 break;
2823 case op_ifnull:
2824 case op_ifnonnull:
2825 pop_type (reference_type);
2826 push_jump (get_short ());
2827 break;
2828 case op_goto_w:
2829 push_jump (get_int ());
2830 invalidate_pc ();
2831 break;
2832 case op_jsr_w:
2833 handle_jsr_insn (get_int ());
2834 break;
2835
2836 default:
2837 // Unrecognized opcode.
60440707
TT
2838 verify_fail ("unrecognized instruction in verify_instructions_0",
2839 start_PC);
a12fe13d
TT
2840 }
2841 }
2842 }
2843
f70443f7
PB
2844 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
2845 {
2846 using namespace java::lang;
2847 StringBuffer *buf = new StringBuffer ();
2848
2849 buf->append (JvNewStringLatin1 ("verification failed"));
2850 if (pc == -1)
2851 pc = start_PC;
2852 if (pc != -1)
2853 {
2854 buf->append (JvNewStringLatin1 (" at PC "));
2855 buf->append (pc);
2856 }
2857
2858 _Jv_InterpMethod *method = current_method;
2859 buf->append (JvNewStringLatin1 (" in "));
2860 buf->append (current_class->getName());
2861 buf->append ((jchar) ':');
2862 buf->append (JvNewStringUTF (method->get_method()->name->data));
2863 buf->append ((jchar) '(');
2864 buf->append (JvNewStringUTF (method->get_method()->signature->data));
2865 buf->append ((jchar) ')');
2866
2867 buf->append (JvNewStringLatin1 (": "));
2868 buf->append (JvNewStringLatin1 (s));
2869 throw new java::lang::VerifyError (buf->toString ());
2870 }
2871
a12fe13d
TT
2872public:
2873
2874 void verify_instructions ()
2875 {
2876 branch_prepass ();
2877 verify_instructions_0 ();
2878 }
2879
2880 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2881 {
c1bf99a2
TT
2882 // We just print the text as utf-8. This is just for debugging
2883 // anyway.
2884 debug_print ("--------------------------------\n");
2885 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
2886
a12fe13d
TT
2887 current_method = m;
2888 bytecode = m->bytecode ();
2889 exception = m->exceptions ();
2890 current_class = m->defining_class;
2891
2892 states = NULL;
2893 flags = NULL;
2894 jsr_ptrs = NULL;
0c88d7f8 2895 utf8_list = NULL;
a12fe13d
TT
2896 }
2897
2898 ~_Jv_BytecodeVerifier ()
2899 {
2900 if (states)
2901 _Jv_Free (states);
2902 if (flags)
2903 _Jv_Free (flags);
2904 if (jsr_ptrs)
2905 _Jv_Free (jsr_ptrs);
0c88d7f8
TT
2906 while (utf8_list != NULL)
2907 {
2908 linked_utf8 *n = utf8_list->next;
2909 _Jv_Free (utf8_list->val);
2910 _Jv_Free (utf8_list);
2911 utf8_list = n;
2912 }
a12fe13d
TT
2913 }
2914};
2915
2916void
2917_Jv_VerifyMethod (_Jv_InterpMethod *meth)
2918{
2919 _Jv_BytecodeVerifier v (meth);
2920 v.verify_instructions ();
2921}
75b17b74 2922#endif /* INTERPRETER */
This page took 0.343669 seconds and 5 git commands to generate.