forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.cc
3236 lines (2949 loc) · 84.7 KB
/
verify.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// verify.cc - verify bytecode
/* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
// Written by Tom Tromey <tromey@redhat.com>
// Define VERIFY_DEBUG to enable debugging output.
#include <config.h>
#include <string.h>
#include <jvm.h>
#include <gcj/cni.h>
#include <java-insns.h>
#include <java-interp.h>
// On Solaris 10/x86, <signal.h> indirectly includes <ia32/sys/reg.h>, which
// defines PC since g++ predefines __EXTENSIONS__. Undef here to avoid clash
// with PC member of class _Jv_BytecodeVerifier below.
#undef PC
#ifdef INTERPRETER
#include <java/lang/Class.h>
#include <java/lang/VerifyError.h>
#include <java/lang/Throwable.h>
#include <java/lang/reflect/Modifier.h>
#include <java/lang/StringBuffer.h>
#include <java/lang/NoClassDefFoundError.h>
#ifdef VERIFY_DEBUG
#include <stdio.h>
#endif /* VERIFY_DEBUG */
// This is used to mark states which are not scheduled for
// verification.
#define INVALID_STATE ((state *) -1)
static void debug_print (const char *fmt, ...)
__attribute__ ((format (printf, 1, 2)));
static inline void
debug_print (MAYBE_UNUSED const char *fmt, ...)
{
#ifdef VERIFY_DEBUG
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
#endif /* VERIFY_DEBUG */
}
// This started as a fairly ordinary verifier, and for the most part
// it remains so. It works in the obvious way, by modeling the effect
// of each opcode as it is encountered. For most opcodes, this is a
// straightforward operation.
//
// This verifier does not do type merging. It used to, but this
// results in difficulty verifying some relatively simple code
// involving interfaces, and it pushed some verification work into the
// interpreter.
//
// Instead of merging reference types, when we reach a point where two
// flows of control merge, we simply keep the union of reference types
// from each branch. Then, when we need to verify a fact about a
// reference on the stack (e.g., that it is compatible with the
// argument type of a method), we check to ensure that all possible
// types satisfy the requirement.
//
// Another area this verifier differs from the norm is in its handling
// of subroutines. The JVM specification has some confusing things to
// say about subroutines. For instance, it makes claims about not
// allowing subroutines to merge and it rejects recursive subroutines.
// For the most part these are red herrings; we used to try to follow
// these things but they lead to problems. For example, the notion of
// "being in a subroutine" is not well-defined: is an exception
// handler in a subroutine? If you never execute the `ret' but
// instead `goto 1' do you remain in the subroutine?
//
// For clarity on what is really required for type safety, read
// "Simple Verification Technique for Complex Java Bytecode
// Subroutines" by Alessandro Coglio. Among other things this paper
// shows that recursive subroutines are not harmful to type safety.
// We implement something similar to what he proposes. Note that this
// means that this verifier will accept code that is rejected by some
// other verifiers.
//
// For those not wanting to read the paper, the basic observation is
// that we can maintain split states in subroutines. We maintain one
// state for each calling `jsr'. In other words, we re-verify a
// subroutine once for each caller, using the exact types held by the
// callers (as opposed to the old approach of merging types and
// keeping a bitmap registering what did or did not change). This
// approach lets us continue to verify correctly even when a
// subroutine is exited via `goto' or `athrow' and not `ret'.
//
// In some other areas the JVM specification is (mildly) incorrect,
// so we diverge. For instance, you cannot
// violate type safety by allocating an object with `new' and then
// failing to initialize it, no matter how one branches or where one
// stores the uninitialized reference. See "Improving the official
// specification of Java bytecode verification" by Alessandro Coglio.
//
// Note that there's no real point in enforcing that padding bytes or
// the mystery byte of invokeinterface must be 0, but we do that
// regardless.
//
// The verifier is currently neither completely lazy nor eager when it
// comes to loading classes. It tries to represent types by name when
// possible, and then loads them when it needs to verify a fact about
// the type. Checking types by name is valid because we only use
// names which come from the current class' constant pool. Since all
// such names are looked up using the same class loader, there is no
// danger that we might be fooled into comparing different types with
// the same name.
//
// In the future we plan to allow for a completely lazy mode of
// operation, where the verifier will construct a list of type
// assertions to be checked later.
//
// Some test cases for the verifier live in the "verify" module of the
// Mauve test suite. However, some of these are presently
// (2004-01-20) believed to be incorrect. (More precisely the notion
// of "correct" is not well-defined, and this verifier differs from
// others while remaining type-safe.) Some other tests live in the
// libgcj test suite.
class _Jv_BytecodeVerifier
{
private:
static const int FLAG_INSN_START = 1;
static const int FLAG_BRANCH_TARGET = 2;
struct state;
struct type;
struct linked_utf8;
struct ref_intersection;
template<typename T>
struct linked
{
T *val;
linked<T> *next;
};
// The current PC.
int PC;
// The PC corresponding to the start of the current instruction.
int start_PC;
// The current state of the stack, locals, etc.
state *current_state;
// At each branch target we keep a linked list of all the states we
// can process at that point. We'll only have multiple states at a
// given PC if they both have different return-address types in the
// same stack or local slot. This array is indexed by PC and holds
// the list of all such states.
linked<state> **states;
// We keep a linked list of all the states which we must reverify.
// This is the head of the list.
state *next_verify_state;
// We keep some flags for each instruction. The values are the
// FLAG_* constants defined above. This is an array indexed by PC.
char *flags;
// The bytecode itself.
unsigned char *bytecode;
// The exceptions.
_Jv_InterpException *exception;
// Defining class.
jclass current_class;
// This method.
_Jv_InterpMethod *current_method;
// A linked list of utf8 objects we allocate.
linked<_Jv_Utf8Const> *utf8_list;
// A linked list of all ref_intersection objects we allocate.
ref_intersection *isect_list;
// Create a new Utf-8 constant and return it. We do this to avoid
// having our Utf-8 constants prematurely collected.
_Jv_Utf8Const *make_utf8_const (char *s, int len)
{
linked<_Jv_Utf8Const> *lu = (linked<_Jv_Utf8Const> *)
_Jv_Malloc (sizeof (linked<_Jv_Utf8Const>)
+ _Jv_Utf8Const::space_needed(s, len));
_Jv_Utf8Const *r = (_Jv_Utf8Const *) (lu + 1);
r->init(s, len);
lu->val = r;
lu->next = utf8_list;
utf8_list = lu;
return r;
}
__attribute__ ((__noreturn__)) void verify_fail (const char *s, jint pc = -1)
{
using namespace java::lang;
StringBuffer *buf = new StringBuffer ();
buf->append (JvNewStringLatin1 ("verification failed"));
if (pc == -1)
pc = start_PC;
if (pc != -1)
{
buf->append (JvNewStringLatin1 (" at PC "));
buf->append (pc);
}
_Jv_InterpMethod *method = current_method;
buf->append (JvNewStringLatin1 (" in "));
buf->append (current_class->getName());
buf->append ((jchar) ':');
buf->append (method->get_method()->name->toString());
buf->append ((jchar) '(');
buf->append (method->get_method()->signature->toString());
buf->append ((jchar) ')');
buf->append (JvNewStringLatin1 (": "));
buf->append (JvNewStringLatin1 (s));
throw new java::lang::VerifyError (buf->toString ());
}
// This enum holds a list of tags for all the different types we
// need to handle. Reference types are treated specially by the
// type class.
enum type_val
{
void_type,
// The values for primitive types are chosen to correspond to values
// specified to newarray.
boolean_type = 4,
char_type = 5,
float_type = 6,
double_type = 7,
byte_type = 8,
short_type = 9,
int_type = 10,
long_type = 11,
// Used when overwriting second word of a double or long in the
// local variables. Also used after merging local variable states
// to indicate an unusable value.
unsuitable_type,
return_address_type,
// This is the second word of a two-word value, i.e., a double or
// a long.
continuation_type,
// Everything after `reference_type' must be a reference type.
reference_type,
null_type,
uninitialized_reference_type
};
// This represents a merged class type. Some verifiers (including
// earlier versions of this one) will compute the intersection of
// two class types when merging states. However, this loses
// critical information about interfaces implemented by the various
// classes. So instead we keep track of all the actual classes that
// have been merged.
struct ref_intersection
{
// Whether or not this type has been resolved.
bool is_resolved;
// Actual type data.
union
{
// For a resolved reference type, this is a pointer to the class.
jclass klass;
// For other reference types, this it the name of the class.
_Jv_Utf8Const *name;
} data;
// Link to the next reference in the intersection.
ref_intersection *ref_next;
// This is used to keep track of all the allocated
// ref_intersection objects, so we can free them.
// FIXME: we should allocate these in chunks.
ref_intersection *alloc_next;
ref_intersection (jclass klass, _Jv_BytecodeVerifier *verifier)
: ref_next (NULL)
{
is_resolved = true;
data.klass = klass;
alloc_next = verifier->isect_list;
verifier->isect_list = this;
}
ref_intersection (_Jv_Utf8Const *name, _Jv_BytecodeVerifier *verifier)
: ref_next (NULL)
{
is_resolved = false;
data.name = name;
alloc_next = verifier->isect_list;
verifier->isect_list = this;
}
ref_intersection (ref_intersection *dup, ref_intersection *tail,
_Jv_BytecodeVerifier *verifier)
: ref_next (tail)
{
is_resolved = dup->is_resolved;
data = dup->data;
alloc_next = verifier->isect_list;
verifier->isect_list = this;
}
bool equals (ref_intersection *other, _Jv_BytecodeVerifier *verifier)
{
if (! is_resolved && ! other->is_resolved
&& _Jv_equalUtf8Classnames (data.name, other->data.name))
return true;
if (! is_resolved)
resolve (verifier);
if (! other->is_resolved)
other->resolve (verifier);
return data.klass == other->data.klass;
}
// Merge THIS type into OTHER, returning the result. This will
// return OTHER if all the classes in THIS already appear in
// OTHER.
ref_intersection *merge (ref_intersection *other,
_Jv_BytecodeVerifier *verifier)
{
ref_intersection *tail = other;
for (ref_intersection *self = this; self != NULL; self = self->ref_next)
{
bool add = true;
for (ref_intersection *iter = other; iter != NULL;
iter = iter->ref_next)
{
if (iter->equals (self, verifier))
{
add = false;
break;
}
}
if (add)
tail = new ref_intersection (self, tail, verifier);
}
return tail;
}
void resolve (_Jv_BytecodeVerifier *verifier)
{
if (is_resolved)
return;
// This is useful if you want to see which classes have to be resolved
// while doing the class verification.
debug_print("resolving class: %s\n", data.name->chars());
using namespace java::lang;
java::lang::ClassLoader *loader
= verifier->current_class->getClassLoaderInternal();
// Due to special handling in to_array() array classes will always
// be of the "L ... ;" kind. The separator char ('.' or '/' may vary
// however.
if (data.name->limit()[-1] == ';')
{
data.klass = _Jv_FindClassFromSignature (data.name->chars(), loader);
if (data.klass == NULL)
throw new java::lang::NoClassDefFoundError(data.name->toString());
}
else
data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
false, loader);
is_resolved = true;
}
// See if an object of type OTHER can be assigned to an object of
// type *THIS. This might resolve classes in one chain or the
// other.
bool compatible (ref_intersection *other,
_Jv_BytecodeVerifier *verifier)
{
ref_intersection *self = this;
for (; self != NULL; self = self->ref_next)
{
ref_intersection *other_iter = other;
for (; other_iter != NULL; other_iter = other_iter->ref_next)
{
// Avoid resolving if possible.
if (! self->is_resolved
&& ! other_iter->is_resolved
&& _Jv_equalUtf8Classnames (self->data.name,
other_iter->data.name))
continue;
if (! self->is_resolved)
self->resolve(verifier);
// If the LHS of the expression is of type
// java.lang.Object, assignment will succeed, no matter
// what the type of the RHS is. Using this short-cut we
// don't need to resolve the class of the RHS at
// verification time.
if (self->data.klass == &java::lang::Object::class$)
continue;
if (! other_iter->is_resolved)
other_iter->resolve(verifier);
if (! is_assignable_from_slow (self->data.klass,
other_iter->data.klass))
return false;
}
}
return true;
}
bool isarray ()
{
// assert (ref_next == NULL);
if (is_resolved)
return data.klass->isArray ();
else
return data.name->first() == '[';
}
bool isinterface (_Jv_BytecodeVerifier *verifier)
{
// assert (ref_next == NULL);
if (! is_resolved)
resolve (verifier);
return data.klass->isInterface ();
}
bool isabstract (_Jv_BytecodeVerifier *verifier)
{
// assert (ref_next == NULL);
if (! is_resolved)
resolve (verifier);
using namespace java::lang::reflect;
return Modifier::isAbstract (data.klass->getModifiers ());
}
jclass getclass (_Jv_BytecodeVerifier *verifier)
{
if (! is_resolved)
resolve (verifier);
return data.klass;
}
int count_dimensions ()
{
int ndims = 0;
if (is_resolved)
{
jclass k = data.klass;
while (k->isArray ())
{
k = k->getComponentType ();
++ndims;
}
}
else
{
char *p = data.name->chars();
while (*p++ == '[')
++ndims;
}
return ndims;
}
void *operator new (size_t bytes)
{
return _Jv_Malloc (bytes);
}
void operator delete (void *mem)
{
_Jv_Free (mem);
}
};
// Return the type_val corresponding to a primitive signature
// character. For instance `I' returns `int.class'.
type_val get_type_val_for_signature (jchar sig)
{
type_val rt;
switch (sig)
{
case 'Z':
rt = boolean_type;
break;
case 'B':
rt = byte_type;
break;
case 'C':
rt = char_type;
break;
case 'S':
rt = short_type;
break;
case 'I':
rt = int_type;
break;
case 'J':
rt = long_type;
break;
case 'F':
rt = float_type;
break;
case 'D':
rt = double_type;
break;
case 'V':
rt = void_type;
break;
default:
verify_fail ("invalid signature");
}
return rt;
}
// Return the type_val corresponding to a primitive class.
type_val get_type_val_for_signature (jclass k)
{
return get_type_val_for_signature ((jchar) k->method_count);
}
// This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
// TARGET haven't been prepared.
static bool is_assignable_from_slow (jclass target, jclass source)
{
// First, strip arrays.
while (target->isArray ())
{
// If target is array, source must be as well.
if (! source->isArray ())
return false;
target = target->getComponentType ();
source = source->getComponentType ();
}
// Quick success.
if (target == &java::lang::Object::class$)
return true;
do
{
if (source == target)
return true;
if (target->isPrimitive () || source->isPrimitive ())
return false;
if (target->isInterface ())
{
for (int i = 0; i < source->interface_count; ++i)
{
// We use a recursive call because we also need to
// check superinterfaces.
if (is_assignable_from_slow (target, source->getInterface (i)))
return true;
}
}
source = source->getSuperclass ();
}
while (source != NULL);
return false;
}
// The `type' class is used to represent a single type in the
// verifier.
struct type
{
// The type key.
type_val key;
// For reference types, the representation of the type.
ref_intersection *klass;
// This is used in two situations.
//
// First, when constructing a new object, it is the PC of the
// `new' instruction which created the object. We use the special
// value UNINIT to mean that this is uninitialized. The special
// value SELF is used for the case where the current method is
// itself the <init> method. the special value EITHER is used
// when we may optionally allow either an uninitialized or
// initialized reference to match.
//
// Second, when the key is return_address_type, this holds the PC
// of the instruction following the `jsr'.
int pc;
static const int UNINIT = -2;
static const int SELF = -1;
static const int EITHER = -3;
// Basic constructor.
type ()
{
key = unsuitable_type;
klass = NULL;
pc = UNINIT;
}
// Make a new instance given the type tag. We assume a generic
// `reference_type' means Object.
type (type_val k)
{
key = k;
// For reference_type, if KLASS==NULL then that means we are
// looking for a generic object of any kind, including an
// uninitialized reference.
klass = NULL;
pc = UNINIT;
}
// Make a new instance given a class.
type (jclass k, _Jv_BytecodeVerifier *verifier)
{
key = reference_type;
klass = new ref_intersection (k, verifier);
pc = UNINIT;
}
// Make a new instance given the name of a class.
type (_Jv_Utf8Const *n, _Jv_BytecodeVerifier *verifier)
{
key = reference_type;
klass = new ref_intersection (n, verifier);
pc = UNINIT;
}
// Copy constructor.
type (const type &t)
{
key = t.key;
klass = t.klass;
pc = t.pc;
}
// These operators are required because libgcj can't link in
// -lstdc++.
void *operator new[] (size_t bytes)
{
return _Jv_Malloc (bytes);
}
void operator delete[] (void *mem)
{
_Jv_Free (mem);
}
type& operator= (type_val k)
{
key = k;
klass = NULL;
pc = UNINIT;
return *this;
}
type& operator= (const type& t)
{
key = t.key;
klass = t.klass;
pc = t.pc;
return *this;
}
// Promote a numeric type.
type &promote ()
{
if (key == boolean_type || key == char_type
|| key == byte_type || key == short_type)
key = int_type;
return *this;
}
// Mark this type as the uninitialized result of `new'.
void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
{
if (key == reference_type)
key = uninitialized_reference_type;
else
verifier->verify_fail ("internal error in type::uninitialized");
pc = npc;
}
// Mark this type as now initialized.
void set_initialized (int npc)
{
if (npc != UNINIT && pc == npc && key == uninitialized_reference_type)
{
key = reference_type;
pc = UNINIT;
}
}
// Mark this type as a particular return address.
void set_return_address (int npc)
{
pc = npc;
}
// Return true if this type and type OTHER are considered
// mergeable for the purposes of state merging. This is related
// to subroutine handling. For this purpose two types are
// considered unmergeable if they are both return-addresses but
// have different PCs.
bool state_mergeable_p (const type &other) const
{
return (key != return_address_type
|| other.key != return_address_type
|| pc == other.pc);
}
// Return true if an object of type K can be assigned to a variable
// of type *THIS. Handle various special cases too. Might modify
// *THIS or K. Note however that this does not perform numeric
// promotion.
bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
{
// Any type is compatible with the unsuitable type.
if (key == unsuitable_type)
return true;
if (key < reference_type || k.key < reference_type)
return key == k.key;
// The `null' type is convertible to any initialized reference
// type.
if (key == null_type)
return k.key != uninitialized_reference_type;
if (k.key == null_type)
return key != uninitialized_reference_type;
// A special case for a generic reference.
if (klass == NULL)
return true;
if (k.klass == NULL)
verifier->verify_fail ("programmer error in type::compatible");
// Handle the special 'EITHER' case, which is only used in a
// special case of 'putfield'. Note that we only need to handle
// this on the LHS of a check.
if (! isinitialized () && pc == EITHER)
{
// If the RHS is uninitialized, it must be an uninitialized
// 'this'.
if (! k.isinitialized () && k.pc != SELF)
return false;
}
else if (isinitialized () != k.isinitialized ())
{
// An initialized type and an uninitialized type are not
// otherwise compatible.
return false;
}
else
{
// Two uninitialized objects are compatible if either:
// * The PCs are identical, or
// * One PC is UNINIT.
if (! isinitialized ())
{
if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
return false;
}
}
return klass->compatible(k.klass, verifier);
}
bool equals (const type &other, _Jv_BytecodeVerifier *vfy)
{
// Only works for reference types.
if ((key != reference_type
&& key != uninitialized_reference_type)
|| (other.key != reference_type
&& other.key != uninitialized_reference_type))
return false;
// Only for single-valued types.
if (klass->ref_next || other.klass->ref_next)
return false;
return klass->equals (other.klass, vfy);
}
bool isvoid () const
{
return key == void_type;
}
bool iswide () const
{
return key == long_type || key == double_type;
}
// Return number of stack or local variable slots taken by this
// type.
int depth () const
{
return iswide () ? 2 : 1;
}
bool isarray () const
{
// We treat null_type as not an array. This is ok based on the
// current uses of this method.
if (key == reference_type)
return klass->isarray ();
return false;
}
bool isnull () const
{
return key == null_type;
}
bool isinterface (_Jv_BytecodeVerifier *verifier)
{
if (key != reference_type)
return false;
return klass->isinterface (verifier);
}
bool isabstract (_Jv_BytecodeVerifier *verifier)
{
if (key != reference_type)
return false;
return klass->isabstract (verifier);
}
// Return the element type of an array.
type element_type (_Jv_BytecodeVerifier *verifier)
{
if (key != reference_type)
verifier->verify_fail ("programmer error in type::element_type()", -1);
jclass k = klass->getclass (verifier)->getComponentType ();
if (k->isPrimitive ())
return type (verifier->get_type_val_for_signature (k));
return type (k, verifier);
}
// Return the array type corresponding to an initialized
// reference. We could expand this to work for other kinds of
// types, but currently we don't need to.
type to_array (_Jv_BytecodeVerifier *verifier)
{
if (key != reference_type)
verifier->verify_fail ("internal error in type::to_array()");
// In case the class is already resolved we can simply ask the runtime
// to give us the array version.
// If it is not resolved we prepend "[" to the classname to make the
// array usage verification more lazy. In other words: makes new Foo[300]
// pass the verifier if Foo.class is missing.
if (klass->is_resolved)
{
jclass k = klass->getclass (verifier);
return type (_Jv_GetArrayClass (k, k->getClassLoaderInternal()),
verifier);
}
else
{
int len = klass->data.name->len();
// If the classname is given in the Lp1/p2/cn; format we only need
// to add a leading '['. The same procedure has to be done for
// primitive arrays (ie. provided "[I", the result should be "[[I".
// If the classname is given as p1.p2.cn we have to embed it into
// "[L" and ';'.
if (klass->data.name->limit()[-1] == ';' ||
_Jv_isPrimitiveOrDerived(klass->data.name))
{
// Reserves space for leading '[' and trailing '\0' .
char arrayName[len + 2];
arrayName[0] = '[';
strcpy(&arrayName[1], klass->data.name->chars());
#ifdef VERIFY_DEBUG
// This is only needed when we want to print the string to the
// screen while debugging.
arrayName[len + 1] = '\0';
debug_print("len: %d - old: '%s' - new: '%s'\n", len, klass->data.name->chars(), arrayName);
#endif
return type (verifier->make_utf8_const( arrayName, len + 1 ),
verifier);
}
else
{
// Reserves space for leading "[L" and trailing ';' and '\0' .
char arrayName[len + 4];
arrayName[0] = '[';
arrayName[1] = 'L';
strcpy(&arrayName[2], klass->data.name->chars());
arrayName[len + 2] = ';';
#ifdef VERIFY_DEBUG
// This is only needed when we want to print the string to the
// screen while debugging.
arrayName[len + 3] = '\0';
debug_print("len: %d - old: '%s' - new: '%s'\n", len, klass->data.name->chars(), arrayName);
#endif
return type (verifier->make_utf8_const( arrayName, len + 3 ),
verifier);
}
}
}
bool isreference () const
{
return key >= reference_type;
}
int get_pc () const
{
return pc;
}
bool isinitialized () const
{
return key == reference_type || key == null_type;
}
bool isresolved () const
{
return (key == reference_type
|| key == null_type
|| key == uninitialized_reference_type);
}
void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
{
// The way this is written, we don't need to check isarray().
if (key != reference_type)
verifier->verify_fail ("internal error in verify_dimensions:"
" not a reference type");
if (klass->count_dimensions () < ndims)
verifier->verify_fail ("array type has fewer dimensions"
" than required");
}
// Merge OLD_TYPE into this. On error throw exception. Return
// true if the merge caused a type change.
bool merge (type& old_type, bool local_semantics,
_Jv_BytecodeVerifier *verifier)
{
bool changed = false;
bool refo = old_type.isreference ();
bool refn = isreference ();
if (refo && refn)
{
if (old_type.key == null_type)
;
else if (key == null_type)
{
*this = old_type;
changed = true;
}
else if (isinitialized () != old_type.isinitialized ())
verifier->verify_fail ("merging initialized and uninitialized types");
else
{
if (! isinitialized ())
{
if (pc == UNINIT)
pc = old_type.pc;
else if (old_type.pc == UNINIT)
;
else if (pc != old_type.pc)