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