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