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