Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/check_code.c
38825 views
1
/*
2
* Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*-
27
* Verify that the code within a method block doesn't exploit any
28
* security holes.
29
*/
30
/*
31
Exported function:
32
33
jboolean
34
VerifyClass(JNIEnv *env, jclass cb, char *message_buffer,
35
jint buffer_length)
36
jboolean
37
VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *message_buffer,
38
jint buffer_length, jint major_version)
39
40
This file now only uses the standard JNI and the following VM functions
41
exported in jvm.h:
42
43
JVM_FindClassFromClass
44
JVM_IsInterface
45
JVM_GetClassNameUTF
46
JVM_GetClassCPEntriesCount
47
JVM_GetClassCPTypes
48
JVM_GetClassFieldsCount
49
JVM_GetClassMethodsCount
50
51
JVM_GetFieldIxModifiers
52
53
JVM_GetMethodIxModifiers
54
JVM_GetMethodIxExceptionTableLength
55
JVM_GetMethodIxLocalsCount
56
JVM_GetMethodIxArgsSize
57
JVM_GetMethodIxMaxStack
58
JVM_GetMethodIxNameUTF
59
JVM_GetMethodIxSignatureUTF
60
JVM_GetMethodIxExceptionsCount
61
JVM_GetMethodIxExceptionIndexes
62
JVM_GetMethodIxByteCodeLength
63
JVM_GetMethodIxByteCode
64
JVM_GetMethodIxExceptionTableEntry
65
JVM_IsConstructorIx
66
67
JVM_GetCPClassNameUTF
68
JVM_GetCPFieldNameUTF
69
JVM_GetCPMethodNameUTF
70
JVM_GetCPFieldSignatureUTF
71
JVM_GetCPMethodSignatureUTF
72
JVM_GetCPFieldClassNameUTF
73
JVM_GetCPMethodClassNameUTF
74
JVM_GetCPFieldModifiers
75
JVM_GetCPMethodModifiers
76
77
JVM_ReleaseUTF
78
JVM_IsSameClassPackage
79
80
*/
81
82
#include <string.h>
83
#include <setjmp.h>
84
#include <assert.h>
85
#include <limits.h>
86
#include <stdlib.h>
87
88
#include "jni.h"
89
#include "jvm.h"
90
#include "classfile_constants.h"
91
#include "opcodes.in_out"
92
93
/* On AIX malloc(0) and calloc(0, ...) return a NULL pointer, which is legal,
94
* but the code here does not handles it. So we wrap the methods and return non-NULL
95
* pointers even if we allocate 0 bytes.
96
*/
97
#ifdef _AIX
98
static int aix_dummy;
99
static void* aix_malloc(size_t len) {
100
if (len == 0) {
101
return &aix_dummy;
102
}
103
return malloc(len);
104
}
105
106
static void* aix_calloc(size_t n, size_t size) {
107
if (n == 0) {
108
return &aix_dummy;
109
}
110
return calloc(n, size);
111
}
112
113
static void aix_free(void* p) {
114
if (p == &aix_dummy) {
115
return;
116
}
117
free(p);
118
}
119
120
#undef malloc
121
#undef calloc
122
#undef free
123
#define malloc aix_malloc
124
#define calloc aix_calloc
125
#define free aix_free
126
#endif
127
128
#ifdef __APPLE__
129
/* use setjmp/longjmp versions that do not save/restore the signal mask */
130
#define setjmp _setjmp
131
#define longjmp _longjmp
132
#endif
133
134
#define MAX_ARRAY_DIMENSIONS 255
135
/* align byte code */
136
#ifndef ALIGN_UP
137
#define ALIGN_UP(n,align_grain) (((n) + ((align_grain) - 1)) & ~((align_grain)-1))
138
#endif /* ALIGN_UP */
139
#define UCALIGN(n) ((unsigned char *)ALIGN_UP((uintptr_t)(n),sizeof(int)))
140
141
#ifdef DEBUG
142
143
int verify_verbose = 0;
144
static struct context_type *GlobalContext;
145
#endif
146
147
enum {
148
ITEM_Bogus,
149
ITEM_Void, /* only as a function return value */
150
ITEM_Integer,
151
ITEM_Float,
152
ITEM_Double,
153
ITEM_Double_2, /* 2nd word of double in register */
154
ITEM_Long,
155
ITEM_Long_2, /* 2nd word of long in register */
156
ITEM_Array,
157
ITEM_Object, /* Extra info field gives name. */
158
ITEM_NewObject, /* Like object, but uninitialized. */
159
ITEM_InitObject, /* "this" is init method, before call
160
to super() */
161
ITEM_ReturnAddress, /* Extra info gives instr # of start pc */
162
/* The following three are only used within array types.
163
* Normally, we use ITEM_Integer, instead. */
164
ITEM_Byte,
165
ITEM_Short,
166
ITEM_Char
167
};
168
169
170
#define UNKNOWN_STACK_SIZE -1
171
#define UNKNOWN_REGISTER_COUNT -1
172
#define UNKNOWN_RET_INSTRUCTION -1
173
174
#undef MAX
175
#undef MIN
176
#define MAX(a, b) ((a) > (b) ? (a) : (b))
177
#define MIN(a, b) ((a) < (b) ? (a) : (b))
178
179
#define BITS_PER_INT (CHAR_BIT * sizeof(int)/sizeof(char))
180
#define SET_BIT(flags, i) (flags[(i)/BITS_PER_INT] |= \
181
((unsigned)1 << ((i) % BITS_PER_INT)))
182
#define IS_BIT_SET(flags, i) (flags[(i)/BITS_PER_INT] & \
183
((unsigned)1 << ((i) % BITS_PER_INT)))
184
185
typedef unsigned int fullinfo_type;
186
typedef unsigned int *bitvector;
187
188
#define GET_ITEM_TYPE(thing) ((thing) & 0x1F)
189
#define GET_INDIRECTION(thing) (((thing) & 0xFFFF) >> 5)
190
#define GET_EXTRA_INFO(thing) ((thing) >> 16)
191
#define WITH_ZERO_INDIRECTION(thing) ((thing) & ~(0xFFE0))
192
#define WITH_ZERO_EXTRA_INFO(thing) ((thing) & 0xFFFF)
193
194
#define MAKE_FULLINFO(type, indirect, extra) \
195
((type) + ((indirect) << 5) + ((extra) << 16))
196
197
#define MAKE_Object_ARRAY(indirect) \
198
(context->object_info + ((indirect) << 5))
199
200
#define NULL_FULLINFO MAKE_FULLINFO(ITEM_Object, 0, 0)
201
202
/* JVM_OPC_invokespecial calls to <init> need to be treated special */
203
#define JVM_OPC_invokeinit 0x100
204
205
/* A hash mechanism used by the verifier.
206
* Maps class names to unique 16 bit integers.
207
*/
208
209
#define HASH_TABLE_SIZE 503
210
211
/* The buckets are managed as a 256 by 256 matrix. We allocate an entire
212
* row (256 buckets) at a time to minimize fragmentation. Rows are
213
* allocated on demand so that we don't waste too much space.
214
*/
215
216
#define MAX_HASH_ENTRIES 65536
217
#define HASH_ROW_SIZE 256
218
219
typedef struct hash_bucket_type {
220
char *name;
221
unsigned int hash;
222
jclass class;
223
unsigned short ID;
224
unsigned short next;
225
unsigned loadable:1; /* from context->class loader */
226
} hash_bucket_type;
227
228
typedef struct {
229
hash_bucket_type **buckets;
230
unsigned short *table;
231
int entries_used;
232
} hash_table_type;
233
234
#define GET_BUCKET(class_hash, ID)\
235
(class_hash->buckets[ID / HASH_ROW_SIZE] + ID % HASH_ROW_SIZE)
236
237
/*
238
* There are currently two types of resources that we need to keep
239
* track of (in addition to the CCalloc pool).
240
*/
241
enum {
242
VM_STRING_UTF, /* VM-allocated UTF strings */
243
VM_MALLOC_BLK /* malloc'ed blocks */
244
};
245
246
#define LDC_CLASS_MAJOR_VERSION 49
247
248
#define LDC_METHOD_HANDLE_MAJOR_VERSION 51
249
250
#define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION 51
251
252
#define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION 52
253
254
#define ALLOC_STACK_SIZE 16 /* big enough */
255
256
typedef struct alloc_stack_type {
257
void *ptr;
258
int kind;
259
struct alloc_stack_type *next;
260
} alloc_stack_type;
261
262
/* The context type encapsulates the current invocation of the byte
263
* code verifier.
264
*/
265
struct context_type {
266
267
JNIEnv *env; /* current JNIEnv */
268
269
/* buffers etc. */
270
char *message;
271
jint message_buf_len;
272
jboolean err_code;
273
274
alloc_stack_type *allocated_memory; /* all memory blocks that we have not
275
had a chance to free */
276
/* Store up to ALLOC_STACK_SIZE number of handles to allocated memory
277
blocks here, to save mallocs. */
278
alloc_stack_type alloc_stack[ALLOC_STACK_SIZE];
279
int alloc_stack_top;
280
281
/* these fields are per class */
282
jclass class; /* current class */
283
jint major_version;
284
jint nconstants;
285
unsigned char *constant_types;
286
hash_table_type class_hash;
287
288
fullinfo_type object_info; /* fullinfo for java/lang/Object */
289
fullinfo_type string_info; /* fullinfo for java/lang/String */
290
fullinfo_type throwable_info; /* fullinfo for java/lang/Throwable */
291
fullinfo_type cloneable_info; /* fullinfo for java/lang/Cloneable */
292
fullinfo_type serializable_info; /* fullinfo for java/io/Serializable */
293
294
fullinfo_type currentclass_info; /* fullinfo for context->class */
295
fullinfo_type superclass_info; /* fullinfo for superclass */
296
297
/* these fields are per method */
298
int method_index; /* current method */
299
unsigned short *exceptions; /* exceptions */
300
unsigned char *code; /* current code object */
301
jint code_length;
302
int *code_data; /* offset to instruction number */
303
struct instruction_data_type *instruction_data; /* info about each */
304
struct handler_info_type *handler_info;
305
fullinfo_type *superclasses; /* null terminated superclasses */
306
int instruction_count; /* number of instructions */
307
fullinfo_type return_type; /* function return type */
308
fullinfo_type swap_table[4]; /* used for passing information */
309
int bitmask_size; /* words needed to hold bitmap of arguments */
310
311
/* these fields are per field */
312
int field_index;
313
314
/* Used by the space allocator */
315
struct CCpool *CCroot, *CCcurrent;
316
char *CCfree_ptr;
317
int CCfree_size;
318
319
/* Jump here on any error. */
320
jmp_buf jump_buffer;
321
322
#ifdef DEBUG
323
/* keep track of how many global refs are allocated. */
324
int n_globalrefs;
325
#endif
326
};
327
328
struct stack_info_type {
329
struct stack_item_type *stack;
330
int stack_size;
331
};
332
333
struct register_info_type {
334
int register_count; /* number of registers used */
335
fullinfo_type *registers;
336
int mask_count; /* number of masks in the following */
337
struct mask_type *masks;
338
};
339
340
struct mask_type {
341
int entry;
342
int *modifies;
343
};
344
345
typedef unsigned short flag_type;
346
347
struct instruction_data_type {
348
int opcode; /* may turn into "canonical" opcode */
349
unsigned changed:1; /* has it changed */
350
unsigned protected:1; /* must accessor be a subclass of "this" */
351
union {
352
int i; /* operand to the opcode */
353
int *ip;
354
fullinfo_type fi;
355
} operand, operand2;
356
fullinfo_type p;
357
struct stack_info_type stack_info;
358
struct register_info_type register_info;
359
#define FLAG_REACHED 0x01 /* instruction reached */
360
#define FLAG_NEED_CONSTRUCTOR 0x02 /* must call this.<init> or super.<init> */
361
#define FLAG_NO_RETURN 0x04 /* must throw out of method */
362
flag_type or_flags; /* true for at least one path to this inst */
363
#define FLAG_CONSTRUCTED 0x01 /* this.<init> or super.<init> called */
364
flag_type and_flags; /* true for all paths to this instruction */
365
};
366
367
struct handler_info_type {
368
int start, end, handler;
369
struct stack_info_type stack_info;
370
};
371
372
struct stack_item_type {
373
fullinfo_type item;
374
struct stack_item_type *next;
375
};
376
377
typedef struct context_type context_type;
378
typedef struct instruction_data_type instruction_data_type;
379
typedef struct stack_item_type stack_item_type;
380
typedef struct register_info_type register_info_type;
381
typedef struct stack_info_type stack_info_type;
382
typedef struct mask_type mask_type;
383
384
static void read_all_code(context_type *context, jclass cb, int num_methods,
385
int** code_lengths, unsigned char*** code);
386
static void verify_method(context_type *context, jclass cb, int index,
387
int code_length, unsigned char* code);
388
static void free_all_code(context_type* context, int num_methods,
389
unsigned char** code);
390
static void verify_field(context_type *context, jclass cb, int index);
391
392
static void verify_opcode_operands (context_type *, unsigned int inumber, int offset);
393
static void set_protected(context_type *, unsigned int inumber, int key, int);
394
static jboolean is_superclass(context_type *, fullinfo_type);
395
396
static void initialize_exception_table(context_type *);
397
static int instruction_length(unsigned char *iptr, unsigned char *end);
398
static jboolean isLegalTarget(context_type *, int offset);
399
static void verify_constant_pool_type(context_type *, int, unsigned);
400
401
static void initialize_dataflow(context_type *);
402
static void run_dataflow(context_type *context);
403
static void check_register_values(context_type *context, unsigned int inumber);
404
static void check_flags(context_type *context, unsigned int inumber);
405
static void pop_stack(context_type *, unsigned int inumber, stack_info_type *);
406
static void update_registers(context_type *, unsigned int inumber, register_info_type *);
407
static void update_flags(context_type *, unsigned int inumber,
408
flag_type *new_and_flags, flag_type *new_or_flags);
409
static void push_stack(context_type *, unsigned int inumber, stack_info_type *stack);
410
411
static void merge_into_successors(context_type *, unsigned int inumber,
412
register_info_type *register_info,
413
stack_info_type *stack_info,
414
flag_type and_flags, flag_type or_flags);
415
static void merge_into_one_successor(context_type *context,
416
unsigned int from_inumber,
417
unsigned int inumber,
418
register_info_type *register_info,
419
stack_info_type *stack_info,
420
flag_type and_flags, flag_type or_flags,
421
jboolean isException);
422
static void merge_stack(context_type *, unsigned int inumber,
423
unsigned int to_inumber, stack_info_type *);
424
static void merge_registers(context_type *, unsigned int inumber,
425
unsigned int to_inumber,
426
register_info_type *);
427
static void merge_flags(context_type *context, unsigned int from_inumber,
428
unsigned int to_inumber,
429
flag_type new_and_flags, flag_type new_or_flags);
430
431
static stack_item_type *copy_stack(context_type *, stack_item_type *);
432
static mask_type *copy_masks(context_type *, mask_type *masks, int mask_count);
433
static mask_type *add_to_masks(context_type *, mask_type *, int , int);
434
435
static fullinfo_type decrement_indirection(fullinfo_type);
436
437
static fullinfo_type merge_fullinfo_types(context_type *context,
438
fullinfo_type a,
439
fullinfo_type b,
440
jboolean assignment);
441
static jboolean isAssignableTo(context_type *,
442
fullinfo_type a,
443
fullinfo_type b);
444
445
static jclass object_fullinfo_to_classclass(context_type *, fullinfo_type);
446
447
448
#define NEW(type, count) \
449
((type *)CCalloc(context, (count)*(sizeof(type)), JNI_FALSE))
450
#define ZNEW(type, count) \
451
((type *)CCalloc(context, (count)*(sizeof(type)), JNI_TRUE))
452
453
static void CCinit(context_type *context);
454
static void CCreinit(context_type *context);
455
static void CCdestroy(context_type *context);
456
static void *CCalloc(context_type *context, int size, jboolean zero);
457
458
static fullinfo_type cp_index_to_class_fullinfo(context_type *, int, int);
459
460
static const char* get_result_signature(const char* signature);
461
462
static char signature_to_fieldtype(context_type *context,
463
const char **signature_p, fullinfo_type *info);
464
465
static void CCerror (context_type *, char *format, ...);
466
static void CFerror (context_type *, char *format, ...);
467
static void CCout_of_memory (context_type *);
468
469
/* Because we can longjmp any time, we need to be very careful about
470
* remembering what needs to be freed. */
471
472
static void check_and_push(context_type *context, const void *ptr, int kind);
473
static void pop_and_free(context_type *context);
474
475
static int signature_to_args_size(const char *method_signature);
476
477
#ifdef DEBUG
478
static void print_stack (context_type *, stack_info_type *stack_info);
479
static void print_registers(context_type *, register_info_type *register_info);
480
static void print_flags(context_type *, flag_type, flag_type);
481
static void print_formatted_fieldname(context_type *context, int index);
482
static void print_formatted_methodname(context_type *context, int index);
483
#endif
484
485
void initialize_class_hash(context_type *context)
486
{
487
hash_table_type *class_hash = &(context->class_hash);
488
class_hash->buckets = (hash_bucket_type **)
489
calloc(MAX_HASH_ENTRIES / HASH_ROW_SIZE, sizeof(hash_bucket_type *));
490
class_hash->table = (unsigned short *)
491
calloc(HASH_TABLE_SIZE, sizeof(unsigned short));
492
if (class_hash->buckets == 0 ||
493
class_hash->table == 0)
494
CCout_of_memory(context);
495
class_hash->entries_used = 0;
496
}
497
498
static void finalize_class_hash(context_type *context)
499
{
500
hash_table_type *class_hash = &(context->class_hash);
501
JNIEnv *env = context->env;
502
int i;
503
/* 4296677: bucket index starts from 1. */
504
for (i=1;i<=class_hash->entries_used;i++) {
505
hash_bucket_type *bucket = GET_BUCKET(class_hash, i);
506
assert(bucket != NULL);
507
free(bucket->name);
508
if (bucket->class) {
509
(*env)->DeleteGlobalRef(env, bucket->class);
510
#ifdef DEBUG
511
context->n_globalrefs--;
512
#endif
513
}
514
}
515
if (class_hash->buckets) {
516
for (i=0;i<MAX_HASH_ENTRIES / HASH_ROW_SIZE; i++) {
517
if (class_hash->buckets[i] == 0)
518
break;
519
free(class_hash->buckets[i]);
520
}
521
}
522
free(class_hash->buckets);
523
free(class_hash->table);
524
}
525
526
static hash_bucket_type *
527
new_bucket(context_type *context, unsigned short *pID)
528
{
529
hash_table_type *class_hash = &(context->class_hash);
530
int i = *pID = class_hash->entries_used + 1;
531
int row = i / HASH_ROW_SIZE;
532
if (i >= MAX_HASH_ENTRIES)
533
CCerror(context, "Exceeded verifier's limit of 65535 referred classes");
534
if (class_hash->buckets[row] == 0) {
535
class_hash->buckets[row] = (hash_bucket_type*)
536
calloc(HASH_ROW_SIZE, sizeof(hash_bucket_type));
537
if (class_hash->buckets[row] == 0)
538
CCout_of_memory(context);
539
}
540
class_hash->entries_used++; /* only increment when we are sure there
541
is no overflow. */
542
return GET_BUCKET(class_hash, i);
543
}
544
545
static unsigned int
546
class_hash_fun(const char *s)
547
{
548
int i;
549
unsigned raw_hash;
550
for (raw_hash = 0; (i = *s) != '\0'; ++s)
551
raw_hash = raw_hash * 37 + i;
552
return raw_hash;
553
}
554
555
/*
556
* Find a class using the defining loader of the current class
557
* and return a local reference to it.
558
*/
559
static jclass load_class_local(context_type *context,const char *classname)
560
{
561
jclass cb = JVM_FindClassFromClass(context->env, classname,
562
JNI_FALSE, context->class);
563
if (cb == 0)
564
CCerror(context, "Cannot find class %s", classname);
565
return cb;
566
}
567
568
/*
569
* Find a class using the defining loader of the current class
570
* and return a global reference to it.
571
*/
572
static jclass load_class_global(context_type *context, const char *classname)
573
{
574
JNIEnv *env = context->env;
575
jclass local, global;
576
577
local = load_class_local(context, classname);
578
global = (*env)->NewGlobalRef(env, local);
579
if (global == 0)
580
CCout_of_memory(context);
581
#ifdef DEBUG
582
context->n_globalrefs++;
583
#endif
584
(*env)->DeleteLocalRef(env, local);
585
return global;
586
}
587
588
/*
589
* Return a unique ID given a local class reference. The loadable
590
* flag is true if the defining class loader of context->class
591
* is known to be capable of loading the class.
592
*/
593
static unsigned short
594
class_to_ID(context_type *context, jclass cb, jboolean loadable)
595
{
596
JNIEnv *env = context->env;
597
hash_table_type *class_hash = &(context->class_hash);
598
unsigned int hash;
599
hash_bucket_type *bucket;
600
unsigned short *pID;
601
const char *name = JVM_GetClassNameUTF(env, cb);
602
603
check_and_push(context, name, VM_STRING_UTF);
604
hash = class_hash_fun(name);
605
pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
606
while (*pID) {
607
bucket = GET_BUCKET(class_hash, *pID);
608
if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
609
/*
610
* There is an unresolved entry with our name
611
* so we're forced to load it in case it matches us.
612
*/
613
if (bucket->class == 0) {
614
assert(bucket->loadable == JNI_TRUE);
615
bucket->class = load_class_global(context, name);
616
}
617
618
/*
619
* It's already in the table. Update the loadable
620
* state if it's known and then we're done.
621
*/
622
if ((*env)->IsSameObject(env, cb, bucket->class)) {
623
if (loadable && !bucket->loadable)
624
bucket->loadable = JNI_TRUE;
625
goto done;
626
}
627
}
628
pID = &bucket->next;
629
}
630
bucket = new_bucket(context, pID);
631
bucket->next = 0;
632
bucket->hash = hash;
633
bucket->name = malloc(strlen(name) + 1);
634
if (bucket->name == 0)
635
CCout_of_memory(context);
636
strcpy(bucket->name, name);
637
bucket->loadable = loadable;
638
bucket->class = (*env)->NewGlobalRef(env, cb);
639
if (bucket->class == 0)
640
CCout_of_memory(context);
641
#ifdef DEBUG
642
context->n_globalrefs++;
643
#endif
644
645
done:
646
pop_and_free(context);
647
return *pID;
648
}
649
650
/*
651
* Return a unique ID given a class name from the constant pool.
652
* All classes are lazily loaded from the defining loader of
653
* context->class.
654
*/
655
static unsigned short
656
class_name_to_ID(context_type *context, const char *name)
657
{
658
hash_table_type *class_hash = &(context->class_hash);
659
unsigned int hash = class_hash_fun(name);
660
hash_bucket_type *bucket;
661
unsigned short *pID;
662
jboolean force_load = JNI_FALSE;
663
664
pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
665
while (*pID) {
666
bucket = GET_BUCKET(class_hash, *pID);
667
if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
668
if (bucket->loadable)
669
goto done;
670
force_load = JNI_TRUE;
671
}
672
pID = &bucket->next;
673
}
674
675
if (force_load) {
676
/*
677
* We found at least one matching named entry for a class that
678
* was not known to be loadable through the defining class loader
679
* of context->class. We must load our named class and update
680
* the hash table in case one these entries matches our class.
681
*/
682
JNIEnv *env = context->env;
683
jclass cb = load_class_local(context, name);
684
unsigned short id = class_to_ID(context, cb, JNI_TRUE);
685
(*env)->DeleteLocalRef(env, cb);
686
return id;
687
}
688
689
bucket = new_bucket(context, pID);
690
bucket->next = 0;
691
bucket->class = 0;
692
bucket->loadable = JNI_TRUE; /* name-only IDs are implicitly loadable */
693
bucket->hash = hash;
694
bucket->name = malloc(strlen(name) + 1);
695
if (bucket->name == 0)
696
CCout_of_memory(context);
697
strcpy(bucket->name, name);
698
699
done:
700
return *pID;
701
}
702
703
static const char *
704
ID_to_class_name(context_type *context, unsigned short ID)
705
{
706
hash_table_type *class_hash = &(context->class_hash);
707
hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
708
return bucket->name;
709
}
710
711
static jclass
712
ID_to_class(context_type *context, unsigned short ID)
713
{
714
hash_table_type *class_hash = &(context->class_hash);
715
hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
716
if (bucket->class == 0) {
717
assert(bucket->loadable == JNI_TRUE);
718
bucket->class = load_class_global(context, bucket->name);
719
}
720
return bucket->class;
721
}
722
723
static fullinfo_type
724
make_loadable_class_info(context_type *context, jclass cb)
725
{
726
return MAKE_FULLINFO(ITEM_Object, 0,
727
class_to_ID(context, cb, JNI_TRUE));
728
}
729
730
static fullinfo_type
731
make_class_info(context_type *context, jclass cb)
732
{
733
return MAKE_FULLINFO(ITEM_Object, 0,
734
class_to_ID(context, cb, JNI_FALSE));
735
}
736
737
static fullinfo_type
738
make_class_info_from_name(context_type *context, const char *name)
739
{
740
return MAKE_FULLINFO(ITEM_Object, 0,
741
class_name_to_ID(context, name));
742
}
743
744
/* RETURNS
745
* 1: on success chosen to be consistent with previous VerifyClass
746
* 0: verify error
747
* 2: out of memory
748
* 3: class format error
749
*
750
* Called by verify_class. Verify the code of each of the methods
751
* in a class. Note that this function apparently can't be JNICALL,
752
* because if it is the dynamic linker doesn't appear to be able to
753
* find it on Win32.
754
*/
755
756
#define CC_OK 1
757
#define CC_VerifyError 0
758
#define CC_OutOfMemory 2
759
#define CC_ClassFormatError 3
760
761
JNIEXPORT jboolean
762
VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *buffer, jint len,
763
jint major_version)
764
{
765
context_type context_structure;
766
context_type *context = &context_structure;
767
jboolean result = CC_OK;
768
int i;
769
int num_methods;
770
int* code_lengths;
771
unsigned char** code;
772
773
#ifdef DEBUG
774
GlobalContext = context;
775
#endif
776
777
memset(context, 0, sizeof(context_type));
778
context->message = buffer;
779
context->message_buf_len = len;
780
781
context->env = env;
782
context->class = cb;
783
784
/* Set invalid method/field index of the context, in case anyone
785
calls CCerror */
786
context->method_index = -1;
787
context->field_index = -1;
788
789
/* Don't call CCerror or anything that can call it above the setjmp! */
790
if (!setjmp(context->jump_buffer)) {
791
jclass super;
792
793
CCinit(context); /* initialize heap; may throw */
794
795
initialize_class_hash(context);
796
797
context->major_version = major_version;
798
context->nconstants = JVM_GetClassCPEntriesCount(env, cb);
799
context->constant_types = (unsigned char *)
800
malloc(sizeof(unsigned char) * context->nconstants + 1);
801
802
if (context->constant_types == 0)
803
CCout_of_memory(context);
804
805
JVM_GetClassCPTypes(env, cb, context->constant_types);
806
807
if (context->constant_types == 0)
808
CCout_of_memory(context);
809
810
context->object_info =
811
make_class_info_from_name(context, "java/lang/Object");
812
context->string_info =
813
make_class_info_from_name(context, "java/lang/String");
814
context->throwable_info =
815
make_class_info_from_name(context, "java/lang/Throwable");
816
context->cloneable_info =
817
make_class_info_from_name(context, "java/lang/Cloneable");
818
context->serializable_info =
819
make_class_info_from_name(context, "java/io/Serializable");
820
821
context->currentclass_info = make_loadable_class_info(context, cb);
822
823
super = (*env)->GetSuperclass(env, cb);
824
825
if (super != 0) {
826
fullinfo_type *gptr;
827
int i = 0;
828
829
context->superclass_info = make_loadable_class_info(context, super);
830
831
while(super != 0) {
832
jclass tmp_cb = (*env)->GetSuperclass(env, super);
833
(*env)->DeleteLocalRef(env, super);
834
super = tmp_cb;
835
i++;
836
}
837
(*env)->DeleteLocalRef(env, super);
838
super = 0;
839
840
/* Can't go on context heap since it survives more than
841
one method */
842
context->superclasses = gptr =
843
malloc(sizeof(fullinfo_type)*(i + 1));
844
if (gptr == 0) {
845
CCout_of_memory(context);
846
}
847
848
super = (*env)->GetSuperclass(env, context->class);
849
while(super != 0) {
850
jclass tmp_cb;
851
*gptr++ = make_class_info(context, super);
852
tmp_cb = (*env)->GetSuperclass(env, super);
853
(*env)->DeleteLocalRef(env, super);
854
super = tmp_cb;
855
}
856
*gptr = 0;
857
} else {
858
context->superclass_info = 0;
859
}
860
861
(*env)->DeleteLocalRef(env, super);
862
863
/* Look at each method */
864
for (i = JVM_GetClassFieldsCount(env, cb); --i >= 0;)
865
verify_field(context, cb, i);
866
num_methods = JVM_GetClassMethodsCount(env, cb);
867
read_all_code(context, cb, num_methods, &code_lengths, &code);
868
for (i = num_methods - 1; i >= 0; --i)
869
verify_method(context, cb, i, code_lengths[i], code[i]);
870
free_all_code(context, num_methods, code);
871
result = CC_OK;
872
} else {
873
result = context->err_code;
874
}
875
876
/* Cleanup */
877
finalize_class_hash(context);
878
879
while(context->allocated_memory)
880
pop_and_free(context);
881
882
#ifdef DEBUG
883
GlobalContext = 0;
884
#endif
885
886
if (context->exceptions)
887
free(context->exceptions);
888
889
if (context->constant_types)
890
free(context->constant_types);
891
892
if (context->superclasses)
893
free(context->superclasses);
894
895
#ifdef DEBUG
896
/* Make sure all global refs created in the verifier are freed */
897
assert(context->n_globalrefs == 0);
898
#endif
899
900
CCdestroy(context); /* destroy heap */
901
return result;
902
}
903
904
#define OLD_FORMAT_MAX_MAJOR_VERSION 48
905
906
JNIEXPORT jboolean
907
VerifyClass(JNIEnv *env, jclass cb, char *buffer, jint len)
908
{
909
static int warned = 0;
910
if (!warned) {
911
jio_fprintf(stdout, "Warning! An old version of jvm is used. This is not supported.\n");
912
warned = 1;
913
}
914
return VerifyClassForMajorVersion(env, cb, buffer, len,
915
OLD_FORMAT_MAX_MAJOR_VERSION);
916
}
917
918
static void
919
verify_field(context_type *context, jclass cb, int field_index)
920
{
921
JNIEnv *env = context->env;
922
int access_bits = JVM_GetFieldIxModifiers(env, cb, field_index);
923
context->field_index = field_index;
924
925
if ( ((access_bits & JVM_ACC_PUBLIC) != 0) &&
926
((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
927
CCerror(context, "Inconsistent access bits.");
928
}
929
context->field_index = -1;
930
}
931
932
933
/**
934
* We read all of the class's methods' code because it is possible that
935
* the verification of one method could resulting in linking further
936
* down the stack (due to class loading), which could end up rewriting
937
* some of the bytecode of methods we haven't verified yet. Since we
938
* don't want to see the rewritten bytecode, cache all the code and
939
* operate only on that.
940
*/
941
static void
942
read_all_code(context_type* context, jclass cb, int num_methods,
943
int** lengths_addr, unsigned char*** code_addr)
944
{
945
int* lengths;
946
unsigned char** code;
947
int i;
948
949
lengths = malloc(sizeof(int) * num_methods);
950
check_and_push(context, lengths, VM_MALLOC_BLK);
951
952
code = malloc(sizeof(unsigned char*) * num_methods);
953
check_and_push(context, code, VM_MALLOC_BLK);
954
955
*(lengths_addr) = lengths;
956
*(code_addr) = code;
957
958
for (i = 0; i < num_methods; ++i) {
959
lengths[i] = JVM_GetMethodIxByteCodeLength(context->env, cb, i);
960
if (lengths[i] > 0) {
961
code[i] = malloc(sizeof(unsigned char) * (lengths[i] + 1));
962
check_and_push(context, code[i], VM_MALLOC_BLK);
963
JVM_GetMethodIxByteCode(context->env, cb, i, code[i]);
964
} else {
965
code[i] = NULL;
966
}
967
}
968
}
969
970
static void
971
free_all_code(context_type* context, int num_methods, unsigned char** code)
972
{
973
int i;
974
for (i = 0; i < num_methods; ++i) {
975
if (code[i] != NULL) {
976
pop_and_free(context);
977
}
978
}
979
pop_and_free(context); /* code */
980
pop_and_free(context); /* lengths */
981
}
982
983
/* Verify the code of one method */
984
static void
985
verify_method(context_type *context, jclass cb, int method_index,
986
int code_length, unsigned char* code)
987
{
988
JNIEnv *env = context->env;
989
int access_bits = JVM_GetMethodIxModifiers(env, cb, method_index);
990
int *code_data;
991
instruction_data_type *idata = 0;
992
int instruction_count;
993
int i, offset;
994
unsigned int inumber;
995
jint nexceptions;
996
997
if ((access_bits & (JVM_ACC_NATIVE | JVM_ACC_ABSTRACT)) != 0) {
998
/* not much to do for abstract and native methods */
999
return;
1000
}
1001
1002
context->code_length = code_length;
1003
context->code = code;
1004
1005
/* CCerror can give method-specific info once this is set */
1006
context->method_index = method_index;
1007
1008
CCreinit(context); /* initial heap */
1009
code_data = NEW(int, code_length);
1010
1011
#ifdef DEBUG
1012
if (verify_verbose) {
1013
const char *classname = JVM_GetClassNameUTF(env, cb);
1014
const char *methodname =
1015
JVM_GetMethodIxNameUTF(env, cb, method_index);
1016
const char *signature =
1017
JVM_GetMethodIxSignatureUTF(env, cb, method_index);
1018
jio_fprintf(stdout, "Looking at %s.%s%s\n",
1019
(classname ? classname : ""),
1020
(methodname ? methodname : ""),
1021
(signature ? signature : ""));
1022
JVM_ReleaseUTF(classname);
1023
JVM_ReleaseUTF(methodname);
1024
JVM_ReleaseUTF(signature);
1025
}
1026
#endif
1027
1028
if (((access_bits & JVM_ACC_PUBLIC) != 0) &&
1029
((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
1030
CCerror(context, "Inconsistent access bits.");
1031
}
1032
1033
// If this method is an overpass method, which is generated by the VM,
1034
// we trust the code and no check needs to be done.
1035
if (JVM_IsVMGeneratedMethodIx(env, cb, method_index)) {
1036
return;
1037
}
1038
1039
/* Run through the code. Mark the start of each instruction, and give
1040
* the instruction a number */
1041
for (i = 0, offset = 0; offset < code_length; i++) {
1042
int length = instruction_length(&code[offset], code + code_length);
1043
int next_offset = offset + length;
1044
if (length <= 0)
1045
CCerror(context, "Illegal instruction found at offset %d", offset);
1046
if (next_offset > code_length)
1047
CCerror(context, "Code stops in the middle of instruction "
1048
" starting at offset %d", offset);
1049
code_data[offset] = i;
1050
while (++offset < next_offset)
1051
code_data[offset] = -1; /* illegal location */
1052
}
1053
instruction_count = i; /* number of instructions in code */
1054
1055
/* Allocate a structure to hold info about each instruction. */
1056
idata = NEW(instruction_data_type, instruction_count);
1057
1058
/* Initialize the heap, and other info in the context structure. */
1059
context->code = code;
1060
context->instruction_data = idata;
1061
context->code_data = code_data;
1062
context->instruction_count = instruction_count;
1063
context->handler_info =
1064
NEW(struct handler_info_type,
1065
JVM_GetMethodIxExceptionTableLength(env, cb, method_index));
1066
context->bitmask_size =
1067
(JVM_GetMethodIxLocalsCount(env, cb, method_index)
1068
+ (BITS_PER_INT - 1))/BITS_PER_INT;
1069
1070
if (instruction_count == 0)
1071
CCerror(context, "Empty code");
1072
1073
for (inumber = 0, offset = 0; offset < code_length; inumber++) {
1074
int length = instruction_length(&code[offset], code + code_length);
1075
instruction_data_type *this_idata = &idata[inumber];
1076
this_idata->opcode = code[offset];
1077
this_idata->stack_info.stack = NULL;
1078
this_idata->stack_info.stack_size = UNKNOWN_STACK_SIZE;
1079
this_idata->register_info.register_count = UNKNOWN_REGISTER_COUNT;
1080
this_idata->changed = JNI_FALSE; /* no need to look at it yet. */
1081
this_idata->protected = JNI_FALSE; /* no need to look at it yet. */
1082
this_idata->and_flags = (flag_type) -1; /* "bottom" and value */
1083
this_idata->or_flags = 0; /* "bottom" or value*/
1084
/* This also sets up this_data->operand. It also makes the
1085
* xload_x and xstore_x instructions look like the generic form. */
1086
verify_opcode_operands(context, inumber, offset);
1087
offset += length;
1088
}
1089
1090
1091
/* make sure exception table is reasonable. */
1092
initialize_exception_table(context);
1093
/* Set up first instruction, and start of exception handlers. */
1094
initialize_dataflow(context);
1095
/* Run data flow analysis on the instructions. */
1096
run_dataflow(context);
1097
1098
/* verify checked exceptions, if any */
1099
nexceptions = JVM_GetMethodIxExceptionsCount(env, cb, method_index);
1100
context->exceptions = (unsigned short *)
1101
malloc(sizeof(unsigned short) * nexceptions + 1);
1102
if (context->exceptions == 0)
1103
CCout_of_memory(context);
1104
JVM_GetMethodIxExceptionIndexes(env, cb, method_index,
1105
context->exceptions);
1106
for (i = 0; i < nexceptions; i++) {
1107
/* Make sure the constant pool item is JVM_CONSTANT_Class */
1108
verify_constant_pool_type(context, (int)context->exceptions[i],
1109
1 << JVM_CONSTANT_Class);
1110
}
1111
free(context->exceptions);
1112
context->exceptions = 0;
1113
context->code = 0;
1114
context->method_index = -1;
1115
}
1116
1117
1118
/* Look at a single instruction, and verify its operands. Also, for
1119
* simplicity, move the operand into the ->operand field.
1120
* Make sure that branches don't go into the middle of nowhere.
1121
*/
1122
1123
static jint _ck_ntohl(jint n)
1124
{
1125
unsigned char *p = (unsigned char *)&n;
1126
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1127
}
1128
1129
static void
1130
verify_opcode_operands(context_type *context, unsigned int inumber, int offset)
1131
{
1132
JNIEnv *env = context->env;
1133
instruction_data_type *idata = context->instruction_data;
1134
instruction_data_type *this_idata = &idata[inumber];
1135
int *code_data = context->code_data;
1136
int mi = context->method_index;
1137
unsigned char *code = context->code;
1138
int opcode = this_idata->opcode;
1139
int var;
1140
1141
/*
1142
* Set the ip fields to 0 not the i fields because the ip fields
1143
* are 64 bits on 64 bit architectures, the i field is only 32
1144
*/
1145
this_idata->operand.ip = 0;
1146
this_idata->operand2.ip = 0;
1147
1148
switch (opcode) {
1149
1150
case JVM_OPC_jsr:
1151
/* instruction of ret statement */
1152
this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
1153
/* FALLTHROUGH */
1154
case JVM_OPC_ifeq: case JVM_OPC_ifne: case JVM_OPC_iflt:
1155
case JVM_OPC_ifge: case JVM_OPC_ifgt: case JVM_OPC_ifle:
1156
case JVM_OPC_ifnull: case JVM_OPC_ifnonnull:
1157
case JVM_OPC_if_icmpeq: case JVM_OPC_if_icmpne: case JVM_OPC_if_icmplt:
1158
case JVM_OPC_if_icmpge: case JVM_OPC_if_icmpgt: case JVM_OPC_if_icmple:
1159
case JVM_OPC_if_acmpeq: case JVM_OPC_if_acmpne:
1160
case JVM_OPC_goto: {
1161
/* Set the ->operand to be the instruction number of the target. */
1162
int jump = (((signed char)(code[offset+1])) << 8) + code[offset+2];
1163
int target = offset + jump;
1164
if (!isLegalTarget(context, target))
1165
CCerror(context, "Illegal target of jump or branch");
1166
this_idata->operand.i = code_data[target];
1167
break;
1168
}
1169
1170
case JVM_OPC_jsr_w:
1171
/* instruction of ret statement */
1172
this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
1173
/* FALLTHROUGH */
1174
case JVM_OPC_goto_w: {
1175
/* Set the ->operand to be the instruction number of the target. */
1176
int jump = (((signed char)(code[offset+1])) << 24) +
1177
(code[offset+2] << 16) + (code[offset+3] << 8) +
1178
(code[offset + 4]);
1179
int target = offset + jump;
1180
if (!isLegalTarget(context, target))
1181
CCerror(context, "Illegal target of jump or branch");
1182
this_idata->operand.i = code_data[target];
1183
break;
1184
}
1185
1186
case JVM_OPC_tableswitch:
1187
case JVM_OPC_lookupswitch: {
1188
/* Set the ->operand to be a table of possible instruction targets. */
1189
int *lpc = (int *) UCALIGN(code + offset + 1);
1190
int *lptr;
1191
int *saved_operand;
1192
int keys;
1193
int k, delta;
1194
1195
if (context->major_version < NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION) {
1196
/* 4639449, 4647081: Padding bytes must be zero. */
1197
unsigned char* bptr = (unsigned char*) (code + offset + 1);
1198
for (; bptr < (unsigned char*)lpc; bptr++) {
1199
if (*bptr != 0) {
1200
CCerror(context, "Non zero padding bytes in switch");
1201
}
1202
}
1203
}
1204
if (opcode == JVM_OPC_tableswitch) {
1205
keys = _ck_ntohl(lpc[2]) - _ck_ntohl(lpc[1]) + 1;
1206
delta = 1;
1207
} else {
1208
keys = _ck_ntohl(lpc[1]); /* number of pairs */
1209
delta = 2;
1210
/* Make sure that the tableswitch items are sorted */
1211
for (k = keys - 1, lptr = &lpc[2]; --k >= 0; lptr += 2) {
1212
int this_key = _ck_ntohl(lptr[0]); /* NB: ntohl may be unsigned */
1213
int next_key = _ck_ntohl(lptr[2]);
1214
if (this_key >= next_key) {
1215
CCerror(context, "Unsorted lookup switch");
1216
}
1217
}
1218
}
1219
saved_operand = NEW(int, keys + 2);
1220
if (!isLegalTarget(context, offset + _ck_ntohl(lpc[0])))
1221
CCerror(context, "Illegal default target in switch");
1222
saved_operand[keys + 1] = code_data[offset + _ck_ntohl(lpc[0])];
1223
for (k = keys, lptr = &lpc[3]; --k >= 0; lptr += delta) {
1224
int target = offset + _ck_ntohl(lptr[0]);
1225
if (!isLegalTarget(context, target))
1226
CCerror(context, "Illegal branch in tableswitch");
1227
saved_operand[k + 1] = code_data[target];
1228
}
1229
saved_operand[0] = keys + 1; /* number of successors */
1230
this_idata->operand.ip = saved_operand;
1231
break;
1232
}
1233
1234
case JVM_OPC_ldc: {
1235
/* Make sure the constant pool item is the right type. */
1236
int key = code[offset + 1];
1237
int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
1238
(1 << JVM_CONSTANT_String);
1239
if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
1240
types |= 1 << JVM_CONSTANT_Class;
1241
}
1242
if (context->major_version >= LDC_METHOD_HANDLE_MAJOR_VERSION) {
1243
types |= (1 << JVM_CONSTANT_MethodHandle) |
1244
(1 << JVM_CONSTANT_MethodType);
1245
}
1246
this_idata->operand.i = key;
1247
verify_constant_pool_type(context, key, types);
1248
break;
1249
}
1250
1251
case JVM_OPC_ldc_w: {
1252
/* Make sure the constant pool item is the right type. */
1253
int key = (code[offset + 1] << 8) + code[offset + 2];
1254
int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
1255
(1 << JVM_CONSTANT_String);
1256
if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
1257
types |= 1 << JVM_CONSTANT_Class;
1258
}
1259
if (context->major_version >= LDC_METHOD_HANDLE_MAJOR_VERSION) {
1260
types |= (1 << JVM_CONSTANT_MethodHandle) |
1261
(1 << JVM_CONSTANT_MethodType);
1262
}
1263
this_idata->operand.i = key;
1264
verify_constant_pool_type(context, key, types);
1265
break;
1266
}
1267
1268
case JVM_OPC_ldc2_w: {
1269
/* Make sure the constant pool item is the right type. */
1270
int key = (code[offset + 1] << 8) + code[offset + 2];
1271
int types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long);
1272
this_idata->operand.i = key;
1273
verify_constant_pool_type(context, key, types);
1274
break;
1275
}
1276
1277
case JVM_OPC_getfield: case JVM_OPC_putfield:
1278
case JVM_OPC_getstatic: case JVM_OPC_putstatic: {
1279
/* Make sure the constant pool item is the right type. */
1280
int key = (code[offset + 1] << 8) + code[offset + 2];
1281
this_idata->operand.i = key;
1282
verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Fieldref);
1283
if (opcode == JVM_OPC_getfield || opcode == JVM_OPC_putfield)
1284
set_protected(context, inumber, key, opcode);
1285
break;
1286
}
1287
1288
case JVM_OPC_invokevirtual:
1289
case JVM_OPC_invokespecial:
1290
case JVM_OPC_invokestatic:
1291
case JVM_OPC_invokeinterface: {
1292
/* Make sure the constant pool item is the right type. */
1293
int key = (code[offset + 1] << 8) + code[offset + 2];
1294
const char *methodname;
1295
jclass cb = context->class;
1296
fullinfo_type clazz_info;
1297
int is_constructor, is_internal;
1298
int kind;
1299
1300
switch (opcode ) {
1301
case JVM_OPC_invokestatic:
1302
kind = ((context->major_version < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION)
1303
? (1 << JVM_CONSTANT_Methodref)
1304
: ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref)));
1305
break;
1306
case JVM_OPC_invokeinterface:
1307
kind = 1 << JVM_CONSTANT_InterfaceMethodref;
1308
break;
1309
default:
1310
kind = 1 << JVM_CONSTANT_Methodref;
1311
}
1312
1313
/* Make sure the constant pool item is the right type. */
1314
verify_constant_pool_type(context, key, kind);
1315
methodname = JVM_GetCPMethodNameUTF(env, cb, key);
1316
check_and_push(context, methodname, VM_STRING_UTF);
1317
is_constructor = !strcmp(methodname, "<init>");
1318
is_internal = methodname[0] == '<';
1319
pop_and_free(context);
1320
1321
clazz_info = cp_index_to_class_fullinfo(context, key,
1322
JVM_CONSTANT_Methodref);
1323
this_idata->operand.i = key;
1324
this_idata->operand2.fi = clazz_info;
1325
if (is_constructor) {
1326
if (opcode != JVM_OPC_invokespecial) {
1327
CCerror(context,
1328
"Must call initializers using invokespecial");
1329
}
1330
this_idata->opcode = JVM_OPC_invokeinit;
1331
} else {
1332
if (is_internal) {
1333
CCerror(context, "Illegal call to internal method");
1334
}
1335
if (opcode == JVM_OPC_invokespecial
1336
&& clazz_info != context->currentclass_info
1337
&& clazz_info != context->superclass_info) {
1338
int not_found = 1;
1339
1340
jclass super = (*env)->GetSuperclass(env, context->class);
1341
while(super != 0) {
1342
jclass tmp_cb;
1343
fullinfo_type new_info = make_class_info(context, super);
1344
if (clazz_info == new_info) {
1345
not_found = 0;
1346
break;
1347
}
1348
tmp_cb = (*env)->GetSuperclass(env, super);
1349
(*env)->DeleteLocalRef(env, super);
1350
super = tmp_cb;
1351
}
1352
(*env)->DeleteLocalRef(env, super);
1353
1354
/* The optimizer may cause this to happen on local code */
1355
if (not_found) {
1356
CCerror(context, "Illegal use of nonvirtual function call");
1357
}
1358
}
1359
}
1360
if (opcode == JVM_OPC_invokeinterface) {
1361
unsigned int args1;
1362
unsigned int args2;
1363
const char *signature =
1364
JVM_GetCPMethodSignatureUTF(env, context->class, key);
1365
check_and_push(context, signature, VM_STRING_UTF);
1366
args1 = signature_to_args_size(signature) + 1;
1367
args2 = code[offset + 3];
1368
if (args1 != args2) {
1369
CCerror(context,
1370
"Inconsistent args_size for invokeinterface");
1371
}
1372
if (code[offset + 4] != 0) {
1373
CCerror(context,
1374
"Fourth operand byte of invokeinterface must be zero");
1375
}
1376
pop_and_free(context);
1377
} else if (opcode == JVM_OPC_invokevirtual
1378
|| opcode == JVM_OPC_invokespecial)
1379
set_protected(context, inumber, key, opcode);
1380
break;
1381
}
1382
1383
case JVM_OPC_invokedynamic:
1384
CCerror(context,
1385
"invokedynamic bytecode is not supported in this class file version");
1386
1387
case JVM_OPC_instanceof:
1388
case JVM_OPC_checkcast:
1389
case JVM_OPC_new:
1390
case JVM_OPC_anewarray:
1391
case JVM_OPC_multianewarray: {
1392
/* Make sure the constant pool item is a class */
1393
int key = (code[offset + 1] << 8) + code[offset + 2];
1394
fullinfo_type target;
1395
verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Class);
1396
target = cp_index_to_class_fullinfo(context, key, JVM_CONSTANT_Class);
1397
if (GET_ITEM_TYPE(target) == ITEM_Bogus)
1398
CCerror(context, "Illegal type");
1399
switch(opcode) {
1400
case JVM_OPC_anewarray:
1401
if ((GET_INDIRECTION(target)) >= MAX_ARRAY_DIMENSIONS)
1402
CCerror(context, "Array with too many dimensions");
1403
this_idata->operand.fi = MAKE_FULLINFO(GET_ITEM_TYPE(target),
1404
GET_INDIRECTION(target) + 1,
1405
GET_EXTRA_INFO(target));
1406
break;
1407
case JVM_OPC_new:
1408
if (WITH_ZERO_EXTRA_INFO(target) !=
1409
MAKE_FULLINFO(ITEM_Object, 0, 0))
1410
CCerror(context, "Illegal creation of multi-dimensional array");
1411
/* operand gets set to the "unitialized object". operand2 gets
1412
* set to what the value will be after it's initialized. */
1413
this_idata->operand.fi = MAKE_FULLINFO(ITEM_NewObject, 0, inumber);
1414
this_idata->operand2.fi = target;
1415
break;
1416
case JVM_OPC_multianewarray:
1417
this_idata->operand.fi = target;
1418
this_idata->operand2.i = code[offset + 3];
1419
if ( (this_idata->operand2.i > (int)GET_INDIRECTION(target))
1420
|| (this_idata->operand2.i == 0))
1421
CCerror(context, "Illegal dimension argument");
1422
break;
1423
default:
1424
this_idata->operand.fi = target;
1425
}
1426
break;
1427
}
1428
1429
case JVM_OPC_newarray: {
1430
/* Cache the result of the JVM_OPC_newarray into the operand slot */
1431
fullinfo_type full_info;
1432
switch (code[offset + 1]) {
1433
case JVM_T_INT:
1434
full_info = MAKE_FULLINFO(ITEM_Integer, 1, 0); break;
1435
case JVM_T_LONG:
1436
full_info = MAKE_FULLINFO(ITEM_Long, 1, 0); break;
1437
case JVM_T_FLOAT:
1438
full_info = MAKE_FULLINFO(ITEM_Float, 1, 0); break;
1439
case JVM_T_DOUBLE:
1440
full_info = MAKE_FULLINFO(ITEM_Double, 1, 0); break;
1441
case JVM_T_BYTE: case JVM_T_BOOLEAN:
1442
full_info = MAKE_FULLINFO(ITEM_Byte, 1, 0); break;
1443
case JVM_T_CHAR:
1444
full_info = MAKE_FULLINFO(ITEM_Char, 1, 0); break;
1445
case JVM_T_SHORT:
1446
full_info = MAKE_FULLINFO(ITEM_Short, 1, 0); break;
1447
default:
1448
full_info = 0; /* Keep lint happy */
1449
CCerror(context, "Bad type passed to newarray");
1450
}
1451
this_idata->operand.fi = full_info;
1452
break;
1453
}
1454
1455
/* Fudge iload_x, aload_x, etc to look like their generic cousin. */
1456
case JVM_OPC_iload_0: case JVM_OPC_iload_1: case JVM_OPC_iload_2: case JVM_OPC_iload_3:
1457
this_idata->opcode = JVM_OPC_iload;
1458
var = opcode - JVM_OPC_iload_0;
1459
goto check_local_variable;
1460
1461
case JVM_OPC_fload_0: case JVM_OPC_fload_1: case JVM_OPC_fload_2: case JVM_OPC_fload_3:
1462
this_idata->opcode = JVM_OPC_fload;
1463
var = opcode - JVM_OPC_fload_0;
1464
goto check_local_variable;
1465
1466
case JVM_OPC_aload_0: case JVM_OPC_aload_1: case JVM_OPC_aload_2: case JVM_OPC_aload_3:
1467
this_idata->opcode = JVM_OPC_aload;
1468
var = opcode - JVM_OPC_aload_0;
1469
goto check_local_variable;
1470
1471
case JVM_OPC_lload_0: case JVM_OPC_lload_1: case JVM_OPC_lload_2: case JVM_OPC_lload_3:
1472
this_idata->opcode = JVM_OPC_lload;
1473
var = opcode - JVM_OPC_lload_0;
1474
goto check_local_variable2;
1475
1476
case JVM_OPC_dload_0: case JVM_OPC_dload_1: case JVM_OPC_dload_2: case JVM_OPC_dload_3:
1477
this_idata->opcode = JVM_OPC_dload;
1478
var = opcode - JVM_OPC_dload_0;
1479
goto check_local_variable2;
1480
1481
case JVM_OPC_istore_0: case JVM_OPC_istore_1: case JVM_OPC_istore_2: case JVM_OPC_istore_3:
1482
this_idata->opcode = JVM_OPC_istore;
1483
var = opcode - JVM_OPC_istore_0;
1484
goto check_local_variable;
1485
1486
case JVM_OPC_fstore_0: case JVM_OPC_fstore_1: case JVM_OPC_fstore_2: case JVM_OPC_fstore_3:
1487
this_idata->opcode = JVM_OPC_fstore;
1488
var = opcode - JVM_OPC_fstore_0;
1489
goto check_local_variable;
1490
1491
case JVM_OPC_astore_0: case JVM_OPC_astore_1: case JVM_OPC_astore_2: case JVM_OPC_astore_3:
1492
this_idata->opcode = JVM_OPC_astore;
1493
var = opcode - JVM_OPC_astore_0;
1494
goto check_local_variable;
1495
1496
case JVM_OPC_lstore_0: case JVM_OPC_lstore_1: case JVM_OPC_lstore_2: case JVM_OPC_lstore_3:
1497
this_idata->opcode = JVM_OPC_lstore;
1498
var = opcode - JVM_OPC_lstore_0;
1499
goto check_local_variable2;
1500
1501
case JVM_OPC_dstore_0: case JVM_OPC_dstore_1: case JVM_OPC_dstore_2: case JVM_OPC_dstore_3:
1502
this_idata->opcode = JVM_OPC_dstore;
1503
var = opcode - JVM_OPC_dstore_0;
1504
goto check_local_variable2;
1505
1506
case JVM_OPC_wide:
1507
this_idata->opcode = code[offset + 1];
1508
var = (code[offset + 2] << 8) + code[offset + 3];
1509
switch(this_idata->opcode) {
1510
case JVM_OPC_lload: case JVM_OPC_dload:
1511
case JVM_OPC_lstore: case JVM_OPC_dstore:
1512
goto check_local_variable2;
1513
default:
1514
goto check_local_variable;
1515
}
1516
1517
case JVM_OPC_iinc: /* the increment amount doesn't matter */
1518
case JVM_OPC_ret:
1519
case JVM_OPC_aload: case JVM_OPC_iload: case JVM_OPC_fload:
1520
case JVM_OPC_astore: case JVM_OPC_istore: case JVM_OPC_fstore:
1521
var = code[offset + 1];
1522
check_local_variable:
1523
/* Make sure that the variable number isn't illegal. */
1524
this_idata->operand.i = var;
1525
if (var >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
1526
CCerror(context, "Illegal local variable number");
1527
break;
1528
1529
case JVM_OPC_lload: case JVM_OPC_dload: case JVM_OPC_lstore: case JVM_OPC_dstore:
1530
var = code[offset + 1];
1531
check_local_variable2:
1532
/* Make sure that the variable number isn't illegal. */
1533
this_idata->operand.i = var;
1534
if ((var + 1) >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
1535
CCerror(context, "Illegal local variable number");
1536
break;
1537
1538
default:
1539
if (opcode > JVM_OPC_MAX)
1540
CCerror(context, "Quick instructions shouldn't appear yet.");
1541
break;
1542
} /* of switch */
1543
}
1544
1545
1546
static void
1547
set_protected(context_type *context, unsigned int inumber, int key, int opcode)
1548
{
1549
JNIEnv *env = context->env;
1550
fullinfo_type clazz_info;
1551
if (opcode != JVM_OPC_invokevirtual && opcode != JVM_OPC_invokespecial) {
1552
clazz_info = cp_index_to_class_fullinfo(context, key,
1553
JVM_CONSTANT_Fieldref);
1554
} else {
1555
clazz_info = cp_index_to_class_fullinfo(context, key,
1556
JVM_CONSTANT_Methodref);
1557
}
1558
if (is_superclass(context, clazz_info)) {
1559
jclass calledClass =
1560
object_fullinfo_to_classclass(context, clazz_info);
1561
int access;
1562
/* 4734966: JVM_GetCPFieldModifiers() or JVM_GetCPMethodModifiers() only
1563
searches the referenced field or method in calledClass. The following
1564
while loop is added to search up the superclass chain to make this
1565
symbolic resolution consistent with the field/method resolution
1566
specified in VM spec 5.4.3. */
1567
calledClass = (*env)->NewLocalRef(env, calledClass);
1568
do {
1569
jclass tmp_cb;
1570
if (opcode != JVM_OPC_invokevirtual && opcode != JVM_OPC_invokespecial) {
1571
access = JVM_GetCPFieldModifiers
1572
(env, context->class, key, calledClass);
1573
} else {
1574
access = JVM_GetCPMethodModifiers
1575
(env, context->class, key, calledClass);
1576
}
1577
if (access != -1) {
1578
break;
1579
}
1580
tmp_cb = (*env)->GetSuperclass(env, calledClass);
1581
(*env)->DeleteLocalRef(env, calledClass);
1582
calledClass = tmp_cb;
1583
} while (calledClass != 0);
1584
1585
if (access == -1) {
1586
/* field/method not found, detected at runtime. */
1587
} else if (access & JVM_ACC_PROTECTED) {
1588
if (!JVM_IsSameClassPackage(env, calledClass, context->class))
1589
context->instruction_data[inumber].protected = JNI_TRUE;
1590
}
1591
(*env)->DeleteLocalRef(env, calledClass);
1592
}
1593
}
1594
1595
1596
static jboolean
1597
is_superclass(context_type *context, fullinfo_type clazz_info) {
1598
fullinfo_type *fptr = context->superclasses;
1599
1600
if (fptr == 0)
1601
return JNI_FALSE;
1602
for (; *fptr != 0; fptr++) {
1603
if (*fptr == clazz_info)
1604
return JNI_TRUE;
1605
}
1606
return JNI_FALSE;
1607
}
1608
1609
1610
/* Look through each item on the exception table. Each of the fields must
1611
* refer to a legal instruction.
1612
*/
1613
static void
1614
initialize_exception_table(context_type *context)
1615
{
1616
JNIEnv *env = context->env;
1617
int mi = context->method_index;
1618
struct handler_info_type *handler_info = context->handler_info;
1619
int *code_data = context->code_data;
1620
int code_length = context->code_length;
1621
int max_stack_size = JVM_GetMethodIxMaxStack(env, context->class, mi);
1622
int i = JVM_GetMethodIxExceptionTableLength(env, context->class, mi);
1623
if (max_stack_size < 1 && i > 0) {
1624
// If the method contains exception handlers, it must have room
1625
// on the expression stack for the exception that the VM could push
1626
CCerror(context, "Stack size too large");
1627
}
1628
for (; --i >= 0; handler_info++) {
1629
JVM_ExceptionTableEntryType einfo;
1630
stack_item_type *stack_item = NEW(stack_item_type, 1);
1631
1632
JVM_GetMethodIxExceptionTableEntry(env, context->class, mi,
1633
i, &einfo);
1634
1635
if (!(einfo.start_pc < einfo.end_pc &&
1636
einfo.start_pc >= 0 &&
1637
isLegalTarget(context, einfo.start_pc) &&
1638
(einfo.end_pc == code_length ||
1639
isLegalTarget(context, einfo.end_pc)))) {
1640
CFerror(context, "Illegal exception table range");
1641
}
1642
if (!((einfo.handler_pc > 0) &&
1643
isLegalTarget(context, einfo.handler_pc))) {
1644
CFerror(context, "Illegal exception table handler");
1645
}
1646
1647
handler_info->start = code_data[einfo.start_pc];
1648
/* einfo.end_pc may point to one byte beyond the end of bytecodes. */
1649
handler_info->end = (einfo.end_pc == context->code_length) ?
1650
context->instruction_count : code_data[einfo.end_pc];
1651
handler_info->handler = code_data[einfo.handler_pc];
1652
handler_info->stack_info.stack = stack_item;
1653
handler_info->stack_info.stack_size = 1;
1654
stack_item->next = NULL;
1655
if (einfo.catchType != 0) {
1656
const char *classname;
1657
/* Constant pool entry type has been checked in format checker */
1658
classname = JVM_GetCPClassNameUTF(env,
1659
context->class,
1660
einfo.catchType);
1661
check_and_push(context, classname, VM_STRING_UTF);
1662
stack_item->item = make_class_info_from_name(context, classname);
1663
if (!isAssignableTo(context,
1664
stack_item->item,
1665
context->throwable_info))
1666
CCerror(context, "catch_type not a subclass of Throwable");
1667
pop_and_free(context);
1668
} else {
1669
stack_item->item = context->throwable_info;
1670
}
1671
}
1672
}
1673
1674
1675
/* Given a pointer to an instruction, return its length. Use the table
1676
* opcode_length[] which is automatically built.
1677
*/
1678
static int instruction_length(unsigned char *iptr, unsigned char *end)
1679
{
1680
static unsigned char opcode_length[] = JVM_OPCODE_LENGTH_INITIALIZER;
1681
int instruction = *iptr;
1682
switch (instruction) {
1683
case JVM_OPC_tableswitch: {
1684
int *lpc = (int *)UCALIGN(iptr + 1);
1685
int index;
1686
if (lpc + 2 >= (int *)end) {
1687
return -1; /* do not read pass the end */
1688
}
1689
index = _ck_ntohl(lpc[2]) - _ck_ntohl(lpc[1]);
1690
if ((index < 0) || (index > 65535)) {
1691
return -1; /* illegal */
1692
} else {
1693
return (unsigned char *)(&lpc[index + 4]) - iptr;
1694
}
1695
}
1696
1697
case JVM_OPC_lookupswitch: {
1698
int *lpc = (int *) UCALIGN(iptr + 1);
1699
int npairs;
1700
if (lpc + 1 >= (int *)end)
1701
return -1; /* do not read pass the end */
1702
npairs = _ck_ntohl(lpc[1]);
1703
/* There can't be more than 64K labels because of the limit
1704
* on per-method byte code length.
1705
*/
1706
if (npairs < 0 || npairs >= 65536)
1707
return -1;
1708
else
1709
return (unsigned char *)(&lpc[2 * (npairs + 1)]) - iptr;
1710
}
1711
1712
case JVM_OPC_wide:
1713
if (iptr + 1 >= end)
1714
return -1; /* do not read pass the end */
1715
switch(iptr[1]) {
1716
case JVM_OPC_ret:
1717
case JVM_OPC_iload: case JVM_OPC_istore:
1718
case JVM_OPC_fload: case JVM_OPC_fstore:
1719
case JVM_OPC_aload: case JVM_OPC_astore:
1720
case JVM_OPC_lload: case JVM_OPC_lstore:
1721
case JVM_OPC_dload: case JVM_OPC_dstore:
1722
return 4;
1723
case JVM_OPC_iinc:
1724
return 6;
1725
default:
1726
return -1;
1727
}
1728
1729
default: {
1730
/* A length of 0 indicates an error. */
1731
int length = opcode_length[instruction];
1732
return (length <= 0) ? -1 : length;
1733
}
1734
}
1735
}
1736
1737
1738
/* Given the target of a branch, make sure that it's a legal target. */
1739
static jboolean
1740
isLegalTarget(context_type *context, int offset)
1741
{
1742
int code_length = context->code_length;
1743
int *code_data = context->code_data;
1744
return (offset >= 0 && offset < code_length && code_data[offset] >= 0);
1745
}
1746
1747
1748
/* Make sure that an element of the constant pool really is of the indicated
1749
* type.
1750
*/
1751
static void
1752
verify_constant_pool_type(context_type *context, int index, unsigned mask)
1753
{
1754
int nconstants = context->nconstants;
1755
unsigned char *type_table = context->constant_types;
1756
unsigned type;
1757
1758
if ((index <= 0) || (index >= nconstants))
1759
CCerror(context, "Illegal constant pool index");
1760
1761
type = type_table[index];
1762
if ((mask & (1 << type)) == 0)
1763
CCerror(context, "Illegal type in constant pool");
1764
}
1765
1766
1767
static void
1768
initialize_dataflow(context_type *context)
1769
{
1770
JNIEnv *env = context->env;
1771
instruction_data_type *idata = context->instruction_data;
1772
int mi = context->method_index;
1773
jclass cb = context->class;
1774
int args_size = JVM_GetMethodIxArgsSize(env, cb, mi);
1775
fullinfo_type *reg_ptr;
1776
fullinfo_type full_info;
1777
const char *p;
1778
const char *signature;
1779
1780
/* Initialize the function entry, since we know everything about it. */
1781
idata[0].stack_info.stack_size = 0;
1782
idata[0].stack_info.stack = NULL;
1783
idata[0].register_info.register_count = args_size;
1784
idata[0].register_info.registers = NEW(fullinfo_type, args_size);
1785
idata[0].register_info.mask_count = 0;
1786
idata[0].register_info.masks = NULL;
1787
idata[0].and_flags = 0; /* nothing needed */
1788
idata[0].or_flags = FLAG_REACHED; /* instruction reached */
1789
reg_ptr = idata[0].register_info.registers;
1790
1791
if ((JVM_GetMethodIxModifiers(env, cb, mi) & JVM_ACC_STATIC) == 0) {
1792
/* A non static method. If this is an <init> method, the first
1793
* argument is an uninitialized object. Otherwise it is an object of
1794
* the given class type. java.lang.Object.<init> is special since
1795
* we don't call its superclass <init> method.
1796
*/
1797
if (JVM_IsConstructorIx(env, cb, mi)
1798
&& context->currentclass_info != context->object_info) {
1799
*reg_ptr++ = MAKE_FULLINFO(ITEM_InitObject, 0, 0);
1800
idata[0].or_flags |= FLAG_NEED_CONSTRUCTOR;
1801
} else {
1802
*reg_ptr++ = context->currentclass_info;
1803
}
1804
}
1805
signature = JVM_GetMethodIxSignatureUTF(env, cb, mi);
1806
check_and_push(context, signature, VM_STRING_UTF);
1807
/* Fill in each of the arguments into the registers. */
1808
for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
1809
char fieldchar = signature_to_fieldtype(context, &p, &full_info);
1810
switch (fieldchar) {
1811
case 'D': case 'L':
1812
*reg_ptr++ = full_info;
1813
*reg_ptr++ = full_info + 1;
1814
break;
1815
default:
1816
*reg_ptr++ = full_info;
1817
break;
1818
}
1819
}
1820
p++; /* skip over right parenthesis */
1821
if (*p == 'V') {
1822
context->return_type = MAKE_FULLINFO(ITEM_Void, 0, 0);
1823
} else {
1824
signature_to_fieldtype(context, &p, &full_info);
1825
context->return_type = full_info;
1826
}
1827
pop_and_free(context);
1828
/* Indicate that we need to look at the first instruction. */
1829
idata[0].changed = JNI_TRUE;
1830
}
1831
1832
1833
/* Run the data flow analysis, as long as there are things to change. */
1834
static void
1835
run_dataflow(context_type *context) {
1836
JNIEnv *env = context->env;
1837
int mi = context->method_index;
1838
jclass cb = context->class;
1839
int max_stack_size = JVM_GetMethodIxMaxStack(env, cb, mi);
1840
instruction_data_type *idata = context->instruction_data;
1841
unsigned int icount = context->instruction_count;
1842
jboolean work_to_do = JNI_TRUE;
1843
unsigned int inumber;
1844
1845
/* Run through the loop, until there is nothing left to do. */
1846
while (work_to_do) {
1847
work_to_do = JNI_FALSE;
1848
for (inumber = 0; inumber < icount; inumber++) {
1849
instruction_data_type *this_idata = &idata[inumber];
1850
if (this_idata->changed) {
1851
register_info_type new_register_info;
1852
stack_info_type new_stack_info;
1853
flag_type new_and_flags, new_or_flags;
1854
1855
this_idata->changed = JNI_FALSE;
1856
work_to_do = JNI_TRUE;
1857
#ifdef DEBUG
1858
if (verify_verbose) {
1859
int opcode = this_idata->opcode;
1860
jio_fprintf(stdout, "Instruction %d: ", inumber);
1861
print_stack(context, &this_idata->stack_info);
1862
print_registers(context, &this_idata->register_info);
1863
print_flags(context,
1864
this_idata->and_flags, this_idata->or_flags);
1865
fflush(stdout);
1866
}
1867
#endif
1868
/* Make sure the registers and flags are appropriate */
1869
check_register_values(context, inumber);
1870
check_flags(context, inumber);
1871
1872
/* Make sure the stack can deal with this instruction */
1873
pop_stack(context, inumber, &new_stack_info);
1874
1875
/* Update the registers and flags */
1876
update_registers(context, inumber, &new_register_info);
1877
update_flags(context, inumber, &new_and_flags, &new_or_flags);
1878
1879
/* Update the stack. */
1880
push_stack(context, inumber, &new_stack_info);
1881
1882
if (new_stack_info.stack_size > max_stack_size)
1883
CCerror(context, "Stack size too large");
1884
#ifdef DEBUG
1885
if (verify_verbose) {
1886
jio_fprintf(stdout, " ");
1887
print_stack(context, &new_stack_info);
1888
print_registers(context, &new_register_info);
1889
print_flags(context, new_and_flags, new_or_flags);
1890
fflush(stdout);
1891
}
1892
#endif
1893
/* Add the new stack and register information to any
1894
* instructions that can follow this instruction. */
1895
merge_into_successors(context, inumber,
1896
&new_register_info, &new_stack_info,
1897
new_and_flags, new_or_flags);
1898
}
1899
}
1900
}
1901
}
1902
1903
1904
/* Make sure that the registers contain a legitimate value for the given
1905
* instruction.
1906
*/
1907
1908
static void
1909
check_register_values(context_type *context, unsigned int inumber)
1910
{
1911
instruction_data_type *idata = context->instruction_data;
1912
instruction_data_type *this_idata = &idata[inumber];
1913
int opcode = this_idata->opcode;
1914
int operand = this_idata->operand.i;
1915
int register_count = this_idata->register_info.register_count;
1916
fullinfo_type *registers = this_idata->register_info.registers;
1917
jboolean double_word = JNI_FALSE; /* default value */
1918
int type;
1919
1920
switch (opcode) {
1921
default:
1922
return;
1923
case JVM_OPC_iload: case JVM_OPC_iinc:
1924
type = ITEM_Integer; break;
1925
case JVM_OPC_fload:
1926
type = ITEM_Float; break;
1927
case JVM_OPC_aload:
1928
type = ITEM_Object; break;
1929
case JVM_OPC_ret:
1930
type = ITEM_ReturnAddress; break;
1931
case JVM_OPC_lload:
1932
type = ITEM_Long; double_word = JNI_TRUE; break;
1933
case JVM_OPC_dload:
1934
type = ITEM_Double; double_word = JNI_TRUE; break;
1935
}
1936
if (!double_word) {
1937
fullinfo_type reg;
1938
/* Make sure we don't have an illegal register or one with wrong type */
1939
if (operand >= register_count) {
1940
CCerror(context,
1941
"Accessing value from uninitialized register %d", operand);
1942
}
1943
reg = registers[operand];
1944
1945
if (WITH_ZERO_EXTRA_INFO(reg) == (unsigned)MAKE_FULLINFO(type, 0, 0)) {
1946
/* the register is obviously of the given type */
1947
return;
1948
} else if (GET_INDIRECTION(reg) > 0 && type == ITEM_Object) {
1949
/* address type stuff be used on all arrays */
1950
return;
1951
} else if (GET_ITEM_TYPE(reg) == ITEM_ReturnAddress) {
1952
CCerror(context, "Cannot load return address from register %d",
1953
operand);
1954
/* alternatively
1955
(GET_ITEM_TYPE(reg) == ITEM_ReturnAddress)
1956
&& (opcode == JVM_OPC_iload)
1957
&& (type == ITEM_Object || type == ITEM_Integer)
1958
but this never occurs
1959
*/
1960
} else if (reg == ITEM_InitObject && type == ITEM_Object) {
1961
return;
1962
} else if (WITH_ZERO_EXTRA_INFO(reg) ==
1963
MAKE_FULLINFO(ITEM_NewObject, 0, 0) &&
1964
type == ITEM_Object) {
1965
return;
1966
} else {
1967
CCerror(context, "Register %d contains wrong type", operand);
1968
}
1969
} else {
1970
/* Make sure we don't have an illegal register or one with wrong type */
1971
if ((operand + 1) >= register_count) {
1972
CCerror(context,
1973
"Accessing value from uninitialized register pair %d/%d",
1974
operand, operand+1);
1975
} else {
1976
if ((registers[operand] == (unsigned)MAKE_FULLINFO(type, 0, 0)) &&
1977
(registers[operand + 1] == (unsigned)MAKE_FULLINFO(type + 1, 0, 0))) {
1978
return;
1979
} else {
1980
CCerror(context, "Register pair %d/%d contains wrong type",
1981
operand, operand+1);
1982
}
1983
}
1984
}
1985
}
1986
1987
1988
/* Make sure the flags contain legitimate values for this instruction.
1989
*/
1990
1991
static void
1992
check_flags(context_type *context, unsigned int inumber)
1993
{
1994
instruction_data_type *idata = context->instruction_data;
1995
instruction_data_type *this_idata = &idata[inumber];
1996
int opcode = this_idata->opcode;
1997
switch (opcode) {
1998
case JVM_OPC_return:
1999
/* We need a constructor, but we aren't guaranteed it's called */
2000
if ((this_idata->or_flags & FLAG_NEED_CONSTRUCTOR) &&
2001
!(this_idata->and_flags & FLAG_CONSTRUCTED))
2002
CCerror(context, "Constructor must call super() or this()");
2003
/* fall through */
2004
case JVM_OPC_ireturn: case JVM_OPC_lreturn:
2005
case JVM_OPC_freturn: case JVM_OPC_dreturn: case JVM_OPC_areturn:
2006
if (this_idata->or_flags & FLAG_NO_RETURN)
2007
/* This method cannot exit normally */
2008
CCerror(context, "Cannot return normally");
2009
default:
2010
break; /* nothing to do. */
2011
}
2012
}
2013
2014
/* Make sure that the top of the stack contains reasonable values for the
2015
* given instruction. The post-pop values of the stack and its size are
2016
* returned in *new_stack_info.
2017
*/
2018
2019
static void
2020
pop_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
2021
{
2022
instruction_data_type *idata = context->instruction_data;
2023
instruction_data_type *this_idata = &idata[inumber];
2024
int opcode = this_idata->opcode;
2025
stack_item_type *stack = this_idata->stack_info.stack;
2026
int stack_size = this_idata->stack_info.stack_size;
2027
char *stack_operands, *p;
2028
char buffer[257]; /* for holding manufactured argument lists */
2029
fullinfo_type stack_extra_info_buffer[256]; /* save info popped off stack */
2030
fullinfo_type *stack_extra_info = &stack_extra_info_buffer[256];
2031
fullinfo_type full_info; /* only used in case of invoke instructions */
2032
fullinfo_type put_full_info; /* only used in case JVM_OPC_putstatic and JVM_OPC_putfield */
2033
2034
switch(opcode) {
2035
default:
2036
/* For most instructions, we just use a built-in table */
2037
stack_operands = opcode_in_out[opcode][0];
2038
break;
2039
2040
case JVM_OPC_putstatic: case JVM_OPC_putfield: {
2041
/* The top thing on the stack depends on the signature of
2042
* the object. */
2043
int operand = this_idata->operand.i;
2044
const char *signature =
2045
JVM_GetCPFieldSignatureUTF(context->env,
2046
context->class,
2047
operand);
2048
char *ip = buffer;
2049
check_and_push(context, signature, VM_STRING_UTF);
2050
#ifdef DEBUG
2051
if (verify_verbose) {
2052
print_formatted_fieldname(context, operand);
2053
}
2054
#endif
2055
if (opcode == JVM_OPC_putfield)
2056
*ip++ = 'A'; /* object for putfield */
2057
*ip++ = signature_to_fieldtype(context, &signature, &put_full_info);
2058
*ip = '\0';
2059
stack_operands = buffer;
2060
pop_and_free(context);
2061
break;
2062
}
2063
2064
case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
2065
case JVM_OPC_invokeinit: /* invokespecial call to <init> */
2066
case JVM_OPC_invokestatic: case JVM_OPC_invokeinterface: {
2067
/* The top stuff on the stack depends on the method signature */
2068
int operand = this_idata->operand.i;
2069
const char *signature =
2070
JVM_GetCPMethodSignatureUTF(context->env,
2071
context->class,
2072
operand);
2073
char *ip = buffer;
2074
const char *p;
2075
check_and_push(context, signature, VM_STRING_UTF);
2076
#ifdef DEBUG
2077
if (verify_verbose) {
2078
print_formatted_methodname(context, operand);
2079
}
2080
#endif
2081
if (opcode != JVM_OPC_invokestatic)
2082
/* First, push the object */
2083
*ip++ = (opcode == JVM_OPC_invokeinit ? '@' : 'A');
2084
for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
2085
*ip++ = signature_to_fieldtype(context, &p, &full_info);
2086
if (ip >= buffer + sizeof(buffer) - 1)
2087
CCerror(context, "Signature %s has too many arguments",
2088
signature);
2089
}
2090
*ip = 0;
2091
stack_operands = buffer;
2092
pop_and_free(context);
2093
break;
2094
}
2095
2096
case JVM_OPC_multianewarray: {
2097
/* Count can't be larger than 255. So can't overflow buffer */
2098
int count = this_idata->operand2.i; /* number of ints on stack */
2099
memset(buffer, 'I', count);
2100
buffer[count] = '\0';
2101
stack_operands = buffer;
2102
break;
2103
}
2104
2105
} /* of switch */
2106
2107
/* Run through the list of operands >>backwards<< */
2108
for ( p = stack_operands + strlen(stack_operands);
2109
p > stack_operands;
2110
stack = stack->next) {
2111
int type = *--p;
2112
fullinfo_type top_type = stack ? stack->item : 0;
2113
int size = (type == 'D' || type == 'L') ? 2 : 1;
2114
*--stack_extra_info = top_type;
2115
if (stack == NULL)
2116
CCerror(context, "Unable to pop operand off an empty stack");
2117
2118
switch (type) {
2119
case 'I':
2120
if (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
2121
CCerror(context, "Expecting to find integer on stack");
2122
break;
2123
2124
case 'F':
2125
if (top_type != MAKE_FULLINFO(ITEM_Float, 0, 0))
2126
CCerror(context, "Expecting to find float on stack");
2127
break;
2128
2129
case 'A': /* object or array */
2130
if ( (GET_ITEM_TYPE(top_type) != ITEM_Object)
2131
&& (GET_INDIRECTION(top_type) == 0)) {
2132
/* The thing isn't an object or an array. Let's see if it's
2133
* one of the special cases */
2134
if ( (WITH_ZERO_EXTRA_INFO(top_type) ==
2135
MAKE_FULLINFO(ITEM_ReturnAddress, 0, 0))
2136
&& (opcode == JVM_OPC_astore))
2137
break;
2138
if ( (GET_ITEM_TYPE(top_type) == ITEM_NewObject
2139
|| (GET_ITEM_TYPE(top_type) == ITEM_InitObject))
2140
&& ((opcode == JVM_OPC_astore) || (opcode == JVM_OPC_aload)
2141
|| (opcode == JVM_OPC_ifnull) || (opcode == JVM_OPC_ifnonnull)))
2142
break;
2143
/* The 2nd edition VM of the specification allows field
2144
* initializations before the superclass initializer,
2145
* if the field is defined within the current class.
2146
*/
2147
if ( (GET_ITEM_TYPE(top_type) == ITEM_InitObject)
2148
&& (opcode == JVM_OPC_putfield)) {
2149
int operand = this_idata->operand.i;
2150
int access_bits = JVM_GetCPFieldModifiers(context->env,
2151
context->class,
2152
operand,
2153
context->class);
2154
/* Note: This relies on the fact that
2155
* JVM_GetCPFieldModifiers retrieves only local fields,
2156
* and does not respect inheritance.
2157
*/
2158
if (access_bits != -1) {
2159
if ( cp_index_to_class_fullinfo(context, operand, JVM_CONSTANT_Fieldref) ==
2160
context->currentclass_info ) {
2161
top_type = context->currentclass_info;
2162
*stack_extra_info = top_type;
2163
break;
2164
}
2165
}
2166
}
2167
CCerror(context, "Expecting to find object/array on stack");
2168
}
2169
break;
2170
2171
case '@': { /* unitialized object, for call to <init> */
2172
int item_type = GET_ITEM_TYPE(top_type);
2173
if (item_type != ITEM_NewObject && item_type != ITEM_InitObject)
2174
CCerror(context,
2175
"Expecting to find unitialized object on stack");
2176
break;
2177
}
2178
2179
case 'O': /* object, not array */
2180
if (WITH_ZERO_EXTRA_INFO(top_type) !=
2181
MAKE_FULLINFO(ITEM_Object, 0, 0))
2182
CCerror(context, "Expecting to find object on stack");
2183
break;
2184
2185
case 'a': /* integer, object, or array */
2186
if ( (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
2187
&& (GET_ITEM_TYPE(top_type) != ITEM_Object)
2188
&& (GET_INDIRECTION(top_type) == 0))
2189
CCerror(context,
2190
"Expecting to find object, array, or int on stack");
2191
break;
2192
2193
case 'D': /* double */
2194
if (top_type != MAKE_FULLINFO(ITEM_Double, 0, 0))
2195
CCerror(context, "Expecting to find double on stack");
2196
break;
2197
2198
case 'L': /* long */
2199
if (top_type != MAKE_FULLINFO(ITEM_Long, 0, 0))
2200
CCerror(context, "Expecting to find long on stack");
2201
break;
2202
2203
case ']': /* array of some type */
2204
if (top_type == NULL_FULLINFO) {
2205
/* do nothing */
2206
} else switch(p[-1]) {
2207
case 'I': /* array of integers */
2208
if (top_type != MAKE_FULLINFO(ITEM_Integer, 1, 0) &&
2209
top_type != NULL_FULLINFO)
2210
CCerror(context,
2211
"Expecting to find array of ints on stack");
2212
break;
2213
2214
case 'L': /* array of longs */
2215
if (top_type != MAKE_FULLINFO(ITEM_Long, 1, 0))
2216
CCerror(context,
2217
"Expecting to find array of longs on stack");
2218
break;
2219
2220
case 'F': /* array of floats */
2221
if (top_type != MAKE_FULLINFO(ITEM_Float, 1, 0))
2222
CCerror(context,
2223
"Expecting to find array of floats on stack");
2224
break;
2225
2226
case 'D': /* array of doubles */
2227
if (top_type != MAKE_FULLINFO(ITEM_Double, 1, 0))
2228
CCerror(context,
2229
"Expecting to find array of doubles on stack");
2230
break;
2231
2232
case 'A': { /* array of addresses (arrays or objects) */
2233
int indirection = GET_INDIRECTION(top_type);
2234
if ((indirection == 0) ||
2235
((indirection == 1) &&
2236
(GET_ITEM_TYPE(top_type) != ITEM_Object)))
2237
CCerror(context,
2238
"Expecting to find array of objects or arrays "
2239
"on stack");
2240
break;
2241
}
2242
2243
case 'B': /* array of bytes */
2244
if (top_type != MAKE_FULLINFO(ITEM_Byte, 1, 0))
2245
CCerror(context,
2246
"Expecting to find array of bytes on stack");
2247
break;
2248
2249
case 'C': /* array of characters */
2250
if (top_type != MAKE_FULLINFO(ITEM_Char, 1, 0))
2251
CCerror(context,
2252
"Expecting to find array of chars on stack");
2253
break;
2254
2255
case 'S': /* array of shorts */
2256
if (top_type != MAKE_FULLINFO(ITEM_Short, 1, 0))
2257
CCerror(context,
2258
"Expecting to find array of shorts on stack");
2259
break;
2260
2261
case '?': /* any type of array is okay */
2262
if (GET_INDIRECTION(top_type) == 0)
2263
CCerror(context,
2264
"Expecting to find array on stack");
2265
break;
2266
2267
default:
2268
CCerror(context, "Internal error #1");
2269
break;
2270
}
2271
p -= 2; /* skip over [ <char> */
2272
break;
2273
2274
case '1': case '2': case '3': case '4': /* stack swapping */
2275
if (top_type == MAKE_FULLINFO(ITEM_Double, 0, 0)
2276
|| top_type == MAKE_FULLINFO(ITEM_Long, 0, 0)) {
2277
if ((p > stack_operands) && (p[-1] == '+')) {
2278
context->swap_table[type - '1'] = top_type + 1;
2279
context->swap_table[p[-2] - '1'] = top_type;
2280
size = 2;
2281
p -= 2;
2282
} else {
2283
CCerror(context,
2284
"Attempt to split long or double on the stack");
2285
}
2286
} else {
2287
context->swap_table[type - '1'] = stack->item;
2288
if ((p > stack_operands) && (p[-1] == '+'))
2289
p--; /* ignore */
2290
}
2291
break;
2292
case '+': /* these should have been caught. */
2293
default:
2294
CCerror(context, "Internal error #2");
2295
}
2296
stack_size -= size;
2297
}
2298
2299
/* For many of the opcodes that had an "A" in their field, we really
2300
* need to go back and do a little bit more accurate testing. We can, of
2301
* course, assume that the minimal type checking has already been done.
2302
*/
2303
switch (opcode) {
2304
default: break;
2305
case JVM_OPC_aastore: { /* array index object */
2306
fullinfo_type array_type = stack_extra_info[0];
2307
fullinfo_type object_type = stack_extra_info[2];
2308
fullinfo_type target_type = decrement_indirection(array_type);
2309
if ((GET_ITEM_TYPE(object_type) != ITEM_Object)
2310
&& (GET_INDIRECTION(object_type) == 0)) {
2311
CCerror(context, "Expecting reference type on operand stack in aastore");
2312
}
2313
if ((GET_ITEM_TYPE(target_type) != ITEM_Object)
2314
&& (GET_INDIRECTION(target_type) == 0)) {
2315
CCerror(context, "Component type of the array must be reference type in aastore");
2316
}
2317
break;
2318
}
2319
2320
case JVM_OPC_putfield:
2321
case JVM_OPC_getfield:
2322
case JVM_OPC_putstatic: {
2323
int operand = this_idata->operand.i;
2324
fullinfo_type stack_object = stack_extra_info[0];
2325
if (opcode == JVM_OPC_putfield || opcode == JVM_OPC_getfield) {
2326
if (!isAssignableTo
2327
(context,
2328
stack_object,
2329
cp_index_to_class_fullinfo
2330
(context, operand, JVM_CONSTANT_Fieldref))) {
2331
CCerror(context,
2332
"Incompatible type for getting or setting field");
2333
}
2334
if (this_idata->protected &&
2335
!isAssignableTo(context, stack_object,
2336
context->currentclass_info)) {
2337
CCerror(context, "Bad access to protected data");
2338
}
2339
}
2340
if (opcode == JVM_OPC_putfield || opcode == JVM_OPC_putstatic) {
2341
int item = (opcode == JVM_OPC_putfield ? 1 : 0);
2342
if (!isAssignableTo(context,
2343
stack_extra_info[item], put_full_info)) {
2344
CCerror(context, "Bad type in putfield/putstatic");
2345
}
2346
}
2347
break;
2348
}
2349
2350
case JVM_OPC_athrow:
2351
if (!isAssignableTo(context, stack_extra_info[0],
2352
context->throwable_info)) {
2353
CCerror(context, "Can only throw Throwable objects");
2354
}
2355
break;
2356
2357
case JVM_OPC_aaload: { /* array index */
2358
/* We need to pass the information to the stack updater */
2359
fullinfo_type array_type = stack_extra_info[0];
2360
context->swap_table[0] = decrement_indirection(array_type);
2361
break;
2362
}
2363
2364
case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
2365
case JVM_OPC_invokeinit:
2366
case JVM_OPC_invokeinterface: case JVM_OPC_invokestatic: {
2367
int operand = this_idata->operand.i;
2368
const char *signature =
2369
JVM_GetCPMethodSignatureUTF(context->env,
2370
context->class,
2371
operand);
2372
int item;
2373
const char *p;
2374
check_and_push(context, signature, VM_STRING_UTF);
2375
if (opcode == JVM_OPC_invokestatic) {
2376
item = 0;
2377
} else if (opcode == JVM_OPC_invokeinit) {
2378
fullinfo_type init_type = this_idata->operand2.fi;
2379
fullinfo_type object_type = stack_extra_info[0];
2380
context->swap_table[0] = object_type; /* save value */
2381
if (GET_ITEM_TYPE(stack_extra_info[0]) == ITEM_NewObject) {
2382
/* We better be calling the appropriate init. Find the
2383
* inumber of the "JVM_OPC_new" instruction", and figure
2384
* out what the type really is.
2385
*/
2386
unsigned int new_inumber = GET_EXTRA_INFO(stack_extra_info[0]);
2387
fullinfo_type target_type = idata[new_inumber].operand2.fi;
2388
context->swap_table[1] = target_type;
2389
2390
if (target_type != init_type) {
2391
CCerror(context, "Call to wrong initialization method");
2392
}
2393
if (this_idata->protected
2394
&& context->major_version > LDC_CLASS_MAJOR_VERSION
2395
&& !isAssignableTo(context, object_type,
2396
context->currentclass_info)) {
2397
CCerror(context, "Bad access to protected data");
2398
}
2399
} else {
2400
/* We better be calling super() or this(). */
2401
if (init_type != context->superclass_info &&
2402
init_type != context->currentclass_info) {
2403
CCerror(context, "Call to wrong initialization method");
2404
}
2405
context->swap_table[1] = context->currentclass_info;
2406
}
2407
item = 1;
2408
} else {
2409
fullinfo_type target_type = this_idata->operand2.fi;
2410
fullinfo_type object_type = stack_extra_info[0];
2411
if (!isAssignableTo(context, object_type, target_type)){
2412
CCerror(context,
2413
"Incompatible object argument for function call");
2414
}
2415
if (opcode == JVM_OPC_invokespecial
2416
&& !isAssignableTo(context, object_type,
2417
context->currentclass_info)) {
2418
/* Make sure object argument is assignment compatible to current class */
2419
CCerror(context,
2420
"Incompatible object argument for invokespecial");
2421
}
2422
if (this_idata->protected
2423
&& !isAssignableTo(context, object_type,
2424
context->currentclass_info)) {
2425
/* This is ugly. Special dispensation. Arrays pretend to
2426
implement public Object clone() even though they don't */
2427
const char *utfName =
2428
JVM_GetCPMethodNameUTF(context->env,
2429
context->class,
2430
this_idata->operand.i);
2431
int is_clone = utfName && (strcmp(utfName, "clone") == 0);
2432
JVM_ReleaseUTF(utfName);
2433
2434
if ((target_type == context->object_info) &&
2435
(GET_INDIRECTION(object_type) > 0) &&
2436
is_clone) {
2437
} else {
2438
CCerror(context, "Bad access to protected data");
2439
}
2440
}
2441
item = 1;
2442
}
2443
for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; item++)
2444
if (signature_to_fieldtype(context, &p, &full_info) == 'A') {
2445
if (!isAssignableTo(context,
2446
stack_extra_info[item], full_info)) {
2447
CCerror(context, "Incompatible argument to function");
2448
}
2449
}
2450
2451
pop_and_free(context);
2452
break;
2453
}
2454
2455
case JVM_OPC_return:
2456
if (context->return_type != MAKE_FULLINFO(ITEM_Void, 0, 0))
2457
CCerror(context, "Wrong return type in function");
2458
break;
2459
2460
case JVM_OPC_ireturn: case JVM_OPC_lreturn: case JVM_OPC_freturn:
2461
case JVM_OPC_dreturn: case JVM_OPC_areturn: {
2462
fullinfo_type target_type = context->return_type;
2463
fullinfo_type object_type = stack_extra_info[0];
2464
if (!isAssignableTo(context, object_type, target_type)) {
2465
CCerror(context, "Wrong return type in function");
2466
}
2467
break;
2468
}
2469
2470
case JVM_OPC_new: {
2471
/* Make sure that nothing on the stack already looks like what
2472
* we want to create. I can't image how this could possibly happen
2473
* but we should test for it anyway, since if it could happen, the
2474
* result would be an unitialized object being able to masquerade
2475
* as an initialized one.
2476
*/
2477
stack_item_type *item;
2478
for (item = stack; item != NULL; item = item->next) {
2479
if (item->item == this_idata->operand.fi) {
2480
CCerror(context,
2481
"Uninitialized object on stack at creating point");
2482
}
2483
}
2484
/* Info for update_registers */
2485
context->swap_table[0] = this_idata->operand.fi;
2486
context->swap_table[1] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
2487
2488
break;
2489
}
2490
}
2491
new_stack_info->stack = stack;
2492
new_stack_info->stack_size = stack_size;
2493
}
2494
2495
2496
/* We've already determined that the instruction is legal. Perform the
2497
* operation on the registers, and return the updated results in
2498
* new_register_count_p and new_registers.
2499
*/
2500
2501
static void
2502
update_registers(context_type *context, unsigned int inumber,
2503
register_info_type *new_register_info)
2504
{
2505
instruction_data_type *idata = context->instruction_data;
2506
instruction_data_type *this_idata = &idata[inumber];
2507
int opcode = this_idata->opcode;
2508
int operand = this_idata->operand.i;
2509
int register_count = this_idata->register_info.register_count;
2510
fullinfo_type *registers = this_idata->register_info.registers;
2511
stack_item_type *stack = this_idata->stack_info.stack;
2512
int mask_count = this_idata->register_info.mask_count;
2513
mask_type *masks = this_idata->register_info.masks;
2514
2515
/* Use these as default new values. */
2516
int new_register_count = register_count;
2517
int new_mask_count = mask_count;
2518
fullinfo_type *new_registers = registers;
2519
mask_type *new_masks = masks;
2520
2521
enum { ACCESS_NONE, ACCESS_SINGLE, ACCESS_DOUBLE } access = ACCESS_NONE;
2522
int i;
2523
2524
/* Remember, we've already verified the type at the top of the stack. */
2525
switch (opcode) {
2526
default: break;
2527
case JVM_OPC_istore: case JVM_OPC_fstore: case JVM_OPC_astore:
2528
access = ACCESS_SINGLE;
2529
goto continue_store;
2530
2531
case JVM_OPC_lstore: case JVM_OPC_dstore:
2532
access = ACCESS_DOUBLE;
2533
goto continue_store;
2534
2535
continue_store: {
2536
/* We have a modification to the registers. Copy them if needed. */
2537
fullinfo_type stack_top_type = stack->item;
2538
int max_operand = operand + ((access == ACCESS_DOUBLE) ? 1 : 0);
2539
2540
if ( max_operand < register_count
2541
&& registers[operand] == stack_top_type
2542
&& ((access == ACCESS_SINGLE) ||
2543
(registers[operand + 1]== stack_top_type + 1)))
2544
/* No changes have been made to the registers. */
2545
break;
2546
new_register_count = MAX(max_operand + 1, register_count);
2547
new_registers = NEW(fullinfo_type, new_register_count);
2548
for (i = 0; i < register_count; i++)
2549
new_registers[i] = registers[i];
2550
for (i = register_count; i < new_register_count; i++)
2551
new_registers[i] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
2552
new_registers[operand] = stack_top_type;
2553
if (access == ACCESS_DOUBLE)
2554
new_registers[operand + 1] = stack_top_type + 1;
2555
break;
2556
}
2557
2558
case JVM_OPC_iload: case JVM_OPC_fload: case JVM_OPC_aload:
2559
case JVM_OPC_iinc: case JVM_OPC_ret:
2560
access = ACCESS_SINGLE;
2561
break;
2562
2563
case JVM_OPC_lload: case JVM_OPC_dload:
2564
access = ACCESS_DOUBLE;
2565
break;
2566
2567
case JVM_OPC_jsr: case JVM_OPC_jsr_w:
2568
for (i = 0; i < new_mask_count; i++)
2569
if (new_masks[i].entry == operand)
2570
CCerror(context, "Recursive call to jsr entry");
2571
new_masks = add_to_masks(context, masks, mask_count, operand);
2572
new_mask_count++;
2573
break;
2574
2575
case JVM_OPC_invokeinit:
2576
case JVM_OPC_new: {
2577
/* For invokeinit, an uninitialized object has been initialized.
2578
* For new, all previous occurrences of an uninitialized object
2579
* from the same instruction must be made bogus.
2580
* We find all occurrences of swap_table[0] in the registers, and
2581
* replace them with swap_table[1];
2582
*/
2583
fullinfo_type from = context->swap_table[0];
2584
fullinfo_type to = context->swap_table[1];
2585
2586
int i;
2587
for (i = 0; i < register_count; i++) {
2588
if (new_registers[i] == from) {
2589
/* Found a match */
2590
break;
2591
}
2592
}
2593
if (i < register_count) { /* We broke out loop for match */
2594
/* We have to change registers, and possibly a mask */
2595
jboolean copied_mask = JNI_FALSE;
2596
int k;
2597
new_registers = NEW(fullinfo_type, register_count);
2598
memcpy(new_registers, registers,
2599
register_count * sizeof(registers[0]));
2600
for ( ; i < register_count; i++) {
2601
if (new_registers[i] == from) {
2602
new_registers[i] = to;
2603
for (k = 0; k < new_mask_count; k++) {
2604
if (!IS_BIT_SET(new_masks[k].modifies, i)) {
2605
if (!copied_mask) {
2606
new_masks = copy_masks(context, new_masks,
2607
mask_count);
2608
copied_mask = JNI_TRUE;
2609
}
2610
SET_BIT(new_masks[k].modifies, i);
2611
}
2612
}
2613
}
2614
}
2615
}
2616
break;
2617
}
2618
} /* of switch */
2619
2620
if ((access != ACCESS_NONE) && (new_mask_count > 0)) {
2621
int i, j;
2622
for (i = 0; i < new_mask_count; i++) {
2623
int *mask = new_masks[i].modifies;
2624
if ((!IS_BIT_SET(mask, operand)) ||
2625
((access == ACCESS_DOUBLE) &&
2626
!IS_BIT_SET(mask, operand + 1))) {
2627
new_masks = copy_masks(context, new_masks, mask_count);
2628
for (j = i; j < new_mask_count; j++) {
2629
SET_BIT(new_masks[j].modifies, operand);
2630
if (access == ACCESS_DOUBLE)
2631
SET_BIT(new_masks[j].modifies, operand + 1);
2632
}
2633
break;
2634
}
2635
}
2636
}
2637
2638
new_register_info->register_count = new_register_count;
2639
new_register_info->registers = new_registers;
2640
new_register_info->masks = new_masks;
2641
new_register_info->mask_count = new_mask_count;
2642
}
2643
2644
2645
2646
/* We've already determined that the instruction is legal, and have updated
2647
* the registers. Update the flags, too.
2648
*/
2649
2650
2651
static void
2652
update_flags(context_type *context, unsigned int inumber,
2653
flag_type *new_and_flags, flag_type *new_or_flags)
2654
2655
{
2656
instruction_data_type *idata = context->instruction_data;
2657
instruction_data_type *this_idata = &idata[inumber];
2658
flag_type and_flags = this_idata->and_flags;
2659
flag_type or_flags = this_idata->or_flags;
2660
2661
/* Set the "we've done a constructor" flag */
2662
if (this_idata->opcode == JVM_OPC_invokeinit) {
2663
fullinfo_type from = context->swap_table[0];
2664
if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
2665
and_flags |= FLAG_CONSTRUCTED;
2666
}
2667
*new_and_flags = and_flags;
2668
*new_or_flags = or_flags;
2669
}
2670
2671
2672
2673
/* We've already determined that the instruction is legal. Perform the
2674
* operation on the stack;
2675
*
2676
* new_stack_size_p and new_stack_p point to the results after the pops have
2677
* already been done. Do the pushes, and then put the results back there.
2678
*/
2679
2680
static void
2681
push_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
2682
{
2683
instruction_data_type *idata = context->instruction_data;
2684
instruction_data_type *this_idata = &idata[inumber];
2685
int opcode = this_idata->opcode;
2686
int operand = this_idata->operand.i;
2687
2688
int stack_size = new_stack_info->stack_size;
2689
stack_item_type *stack = new_stack_info->stack;
2690
char *stack_results;
2691
2692
fullinfo_type full_info = 0;
2693
char buffer[5], *p; /* actually [2] is big enough */
2694
2695
/* We need to look at all those opcodes in which either we can't tell the
2696
* value pushed onto the stack from the opcode, or in which the value
2697
* pushed onto the stack is an object or array. For the latter, we need
2698
* to make sure that full_info is set to the right value.
2699
*/
2700
switch(opcode) {
2701
default:
2702
stack_results = opcode_in_out[opcode][1];
2703
break;
2704
2705
case JVM_OPC_ldc: case JVM_OPC_ldc_w: case JVM_OPC_ldc2_w: {
2706
/* Look to constant pool to determine correct result. */
2707
unsigned char *type_table = context->constant_types;
2708
switch (type_table[operand]) {
2709
case JVM_CONSTANT_Integer:
2710
stack_results = "I"; break;
2711
case JVM_CONSTANT_Float:
2712
stack_results = "F"; break;
2713
case JVM_CONSTANT_Double:
2714
stack_results = "D"; break;
2715
case JVM_CONSTANT_Long:
2716
stack_results = "L"; break;
2717
case JVM_CONSTANT_String:
2718
stack_results = "A";
2719
full_info = context->string_info;
2720
break;
2721
case JVM_CONSTANT_Class:
2722
if (context->major_version < LDC_CLASS_MAJOR_VERSION)
2723
CCerror(context, "Internal error #3");
2724
stack_results = "A";
2725
full_info = make_class_info_from_name(context,
2726
"java/lang/Class");
2727
break;
2728
case JVM_CONSTANT_MethodHandle:
2729
case JVM_CONSTANT_MethodType:
2730
if (context->major_version < LDC_METHOD_HANDLE_MAJOR_VERSION)
2731
CCerror(context, "Internal error #3");
2732
stack_results = "A";
2733
switch (type_table[operand]) {
2734
case JVM_CONSTANT_MethodType:
2735
full_info = make_class_info_from_name(context,
2736
"java/lang/invoke/MethodType");
2737
break;
2738
default: //JVM_CONSTANT_MethodHandle
2739
full_info = make_class_info_from_name(context,
2740
"java/lang/invoke/MethodHandle");
2741
break;
2742
}
2743
break;
2744
default:
2745
CCerror(context, "Internal error #3");
2746
stack_results = ""; /* Never reached: keep lint happy */
2747
}
2748
break;
2749
}
2750
2751
case JVM_OPC_getstatic: case JVM_OPC_getfield: {
2752
/* Look to signature to determine correct result. */
2753
int operand = this_idata->operand.i;
2754
const char *signature = JVM_GetCPFieldSignatureUTF(context->env,
2755
context->class,
2756
operand);
2757
check_and_push(context, signature, VM_STRING_UTF);
2758
#ifdef DEBUG
2759
if (verify_verbose) {
2760
print_formatted_fieldname(context, operand);
2761
}
2762
#endif
2763
buffer[0] = signature_to_fieldtype(context, &signature, &full_info);
2764
buffer[1] = '\0';
2765
stack_results = buffer;
2766
pop_and_free(context);
2767
break;
2768
}
2769
2770
case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
2771
case JVM_OPC_invokeinit:
2772
case JVM_OPC_invokestatic: case JVM_OPC_invokeinterface: {
2773
/* Look to signature to determine correct result. */
2774
int operand = this_idata->operand.i;
2775
const char *signature = JVM_GetCPMethodSignatureUTF(context->env,
2776
context->class,
2777
operand);
2778
const char *result_signature;
2779
check_and_push(context, signature, VM_STRING_UTF);
2780
result_signature = get_result_signature(signature);
2781
if (result_signature++ == NULL) {
2782
CCerror(context, "Illegal signature %s", signature);
2783
}
2784
if (result_signature[0] == JVM_SIGNATURE_VOID) {
2785
stack_results = "";
2786
} else {
2787
buffer[0] = signature_to_fieldtype(context, &result_signature,
2788
&full_info);
2789
buffer[1] = '\0';
2790
stack_results = buffer;
2791
}
2792
pop_and_free(context);
2793
break;
2794
}
2795
2796
case JVM_OPC_aconst_null:
2797
stack_results = opcode_in_out[opcode][1];
2798
full_info = NULL_FULLINFO; /* special NULL */
2799
break;
2800
2801
case JVM_OPC_new:
2802
case JVM_OPC_checkcast:
2803
case JVM_OPC_newarray:
2804
case JVM_OPC_anewarray:
2805
case JVM_OPC_multianewarray:
2806
stack_results = opcode_in_out[opcode][1];
2807
/* Conveniently, this result type is stored here */
2808
full_info = this_idata->operand.fi;
2809
break;
2810
2811
case JVM_OPC_aaload:
2812
stack_results = opcode_in_out[opcode][1];
2813
/* pop_stack() saved value for us. */
2814
full_info = context->swap_table[0];
2815
break;
2816
2817
case JVM_OPC_aload:
2818
stack_results = opcode_in_out[opcode][1];
2819
/* The register hasn't been modified, so we can use its value. */
2820
full_info = this_idata->register_info.registers[operand];
2821
break;
2822
} /* of switch */
2823
2824
for (p = stack_results; *p != 0; p++) {
2825
int type = *p;
2826
stack_item_type *new_item = NEW(stack_item_type, 1);
2827
new_item->next = stack;
2828
stack = new_item;
2829
switch (type) {
2830
case 'I':
2831
stack->item = MAKE_FULLINFO(ITEM_Integer, 0, 0); break;
2832
case 'F':
2833
stack->item = MAKE_FULLINFO(ITEM_Float, 0, 0); break;
2834
case 'D':
2835
stack->item = MAKE_FULLINFO(ITEM_Double, 0, 0);
2836
stack_size++; break;
2837
case 'L':
2838
stack->item = MAKE_FULLINFO(ITEM_Long, 0, 0);
2839
stack_size++; break;
2840
case 'R':
2841
stack->item = MAKE_FULLINFO(ITEM_ReturnAddress, 0, operand);
2842
break;
2843
case '1': case '2': case '3': case '4': {
2844
/* Get the info saved in the swap_table */
2845
fullinfo_type stype = context->swap_table[type - '1'];
2846
stack->item = stype;
2847
if (stype == MAKE_FULLINFO(ITEM_Long, 0, 0) ||
2848
stype == MAKE_FULLINFO(ITEM_Double, 0, 0)) {
2849
stack_size++; p++;
2850
}
2851
break;
2852
}
2853
case 'A':
2854
/* full_info should have the appropriate value. */
2855
assert(full_info != 0);
2856
stack->item = full_info;
2857
break;
2858
default:
2859
CCerror(context, "Internal error #4");
2860
2861
} /* switch type */
2862
stack_size++;
2863
} /* outer for loop */
2864
2865
if (opcode == JVM_OPC_invokeinit) {
2866
/* If there are any instances of "from" on the stack, we need to
2867
* replace it with "to", since calling <init> initializes all versions
2868
* of the object, obviously. */
2869
fullinfo_type from = context->swap_table[0];
2870
stack_item_type *ptr;
2871
for (ptr = stack; ptr != NULL; ptr = ptr->next) {
2872
if (ptr->item == from) {
2873
fullinfo_type to = context->swap_table[1];
2874
stack = copy_stack(context, stack);
2875
for (ptr = stack; ptr != NULL; ptr = ptr->next)
2876
if (ptr->item == from) ptr->item = to;
2877
break;
2878
}
2879
}
2880
}
2881
2882
new_stack_info->stack_size = stack_size;
2883
new_stack_info->stack = stack;
2884
}
2885
2886
2887
/* We've performed an instruction, and determined the new registers and stack
2888
* value. Look at all of the possibly subsequent instructions, and merge
2889
* this stack value into theirs.
2890
*/
2891
2892
static void
2893
merge_into_successors(context_type *context, unsigned int inumber,
2894
register_info_type *register_info,
2895
stack_info_type *stack_info,
2896
flag_type and_flags, flag_type or_flags)
2897
{
2898
instruction_data_type *idata = context->instruction_data;
2899
instruction_data_type *this_idata = &idata[inumber];
2900
int opcode = this_idata->opcode;
2901
int operand = this_idata->operand.i;
2902
struct handler_info_type *handler_info = context->handler_info;
2903
int handler_info_length =
2904
JVM_GetMethodIxExceptionTableLength(context->env,
2905
context->class,
2906
context->method_index);
2907
2908
2909
int buffer[2]; /* default value for successors */
2910
int *successors = buffer; /* table of successors */
2911
int successors_count;
2912
int i;
2913
2914
switch (opcode) {
2915
default:
2916
successors_count = 1;
2917
buffer[0] = inumber + 1;
2918
break;
2919
2920
case JVM_OPC_ifeq: case JVM_OPC_ifne: case JVM_OPC_ifgt:
2921
case JVM_OPC_ifge: case JVM_OPC_iflt: case JVM_OPC_ifle:
2922
case JVM_OPC_ifnull: case JVM_OPC_ifnonnull:
2923
case JVM_OPC_if_icmpeq: case JVM_OPC_if_icmpne: case JVM_OPC_if_icmpgt:
2924
case JVM_OPC_if_icmpge: case JVM_OPC_if_icmplt: case JVM_OPC_if_icmple:
2925
case JVM_OPC_if_acmpeq: case JVM_OPC_if_acmpne:
2926
successors_count = 2;
2927
buffer[0] = inumber + 1;
2928
buffer[1] = operand;
2929
break;
2930
2931
case JVM_OPC_jsr: case JVM_OPC_jsr_w:
2932
if (this_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
2933
idata[this_idata->operand2.i].changed = JNI_TRUE;
2934
/* FALLTHROUGH */
2935
case JVM_OPC_goto: case JVM_OPC_goto_w:
2936
successors_count = 1;
2937
buffer[0] = operand;
2938
break;
2939
2940
2941
case JVM_OPC_ireturn: case JVM_OPC_lreturn: case JVM_OPC_return:
2942
case JVM_OPC_freturn: case JVM_OPC_dreturn: case JVM_OPC_areturn:
2943
case JVM_OPC_athrow:
2944
/* The testing for the returns is handled in pop_stack() */
2945
successors_count = 0;
2946
break;
2947
2948
case JVM_OPC_ret: {
2949
/* This is slightly slow, but good enough for a seldom used instruction.
2950
* The EXTRA_ITEM_INFO of the ITEM_ReturnAddress indicates the
2951
* address of the first instruction of the subroutine. We can return
2952
* to 1 after any instruction that jsr's to that instruction.
2953
*/
2954
if (this_idata->operand2.ip == NULL) {
2955
fullinfo_type *registers = this_idata->register_info.registers;
2956
int called_instruction = GET_EXTRA_INFO(registers[operand]);
2957
int i, count, *ptr;;
2958
for (i = context->instruction_count, count = 0; --i >= 0; ) {
2959
if (((idata[i].opcode == JVM_OPC_jsr) ||
2960
(idata[i].opcode == JVM_OPC_jsr_w)) &&
2961
(idata[i].operand.i == called_instruction))
2962
count++;
2963
}
2964
this_idata->operand2.ip = ptr = NEW(int, count + 1);
2965
*ptr++ = count;
2966
for (i = context->instruction_count, count = 0; --i >= 0; ) {
2967
if (((idata[i].opcode == JVM_OPC_jsr) ||
2968
(idata[i].opcode == JVM_OPC_jsr_w)) &&
2969
(idata[i].operand.i == called_instruction))
2970
*ptr++ = i + 1;
2971
}
2972
}
2973
successors = this_idata->operand2.ip; /* use this instead */
2974
successors_count = *successors++;
2975
break;
2976
2977
}
2978
2979
case JVM_OPC_tableswitch:
2980
case JVM_OPC_lookupswitch:
2981
successors = this_idata->operand.ip; /* use this instead */
2982
successors_count = *successors++;
2983
break;
2984
}
2985
2986
#ifdef DEBUG
2987
if (verify_verbose) {
2988
jio_fprintf(stdout, " [");
2989
for (i = handler_info_length; --i >= 0; handler_info++)
2990
if (handler_info->start <= (int)inumber && handler_info->end > (int)inumber)
2991
jio_fprintf(stdout, "%d* ", handler_info->handler);
2992
for (i = 0; i < successors_count; i++)
2993
jio_fprintf(stdout, "%d ", successors[i]);
2994
jio_fprintf(stdout, "]\n");
2995
}
2996
#endif
2997
2998
handler_info = context->handler_info;
2999
for (i = handler_info_length; --i >= 0; handler_info++) {
3000
if (handler_info->start <= (int)inumber && handler_info->end > (int)inumber) {
3001
int handler = handler_info->handler;
3002
if (opcode != JVM_OPC_invokeinit) {
3003
merge_into_one_successor(context, inumber, handler,
3004
&this_idata->register_info, /* old */
3005
&handler_info->stack_info,
3006
(flag_type) (and_flags
3007
& this_idata->and_flags),
3008
(flag_type) (or_flags
3009
| this_idata->or_flags),
3010
JNI_TRUE);
3011
} else {
3012
/* We need to be a little bit more careful with this
3013
* instruction. Things could either be in the state before
3014
* the instruction or in the state afterwards */
3015
fullinfo_type from = context->swap_table[0];
3016
flag_type temp_or_flags = or_flags;
3017
if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
3018
temp_or_flags |= FLAG_NO_RETURN;
3019
merge_into_one_successor(context, inumber, handler,
3020
&this_idata->register_info, /* old */
3021
&handler_info->stack_info,
3022
this_idata->and_flags,
3023
this_idata->or_flags,
3024
JNI_TRUE);
3025
merge_into_one_successor(context, inumber, handler,
3026
register_info,
3027
&handler_info->stack_info,
3028
and_flags, temp_or_flags, JNI_TRUE);
3029
}
3030
}
3031
}
3032
for (i = 0; i < successors_count; i++) {
3033
int target = successors[i];
3034
if (target >= context->instruction_count)
3035
CCerror(context, "Falling off the end of the code");
3036
merge_into_one_successor(context, inumber, target,
3037
register_info, stack_info, and_flags, or_flags,
3038
JNI_FALSE);
3039
}
3040
}
3041
3042
/* We have a new set of registers and stack values for a given instruction.
3043
* Merge this new set into the values that are already there.
3044
*/
3045
3046
static void
3047
merge_into_one_successor(context_type *context,
3048
unsigned int from_inumber, unsigned int to_inumber,
3049
register_info_type *new_register_info,
3050
stack_info_type *new_stack_info,
3051
flag_type new_and_flags, flag_type new_or_flags,
3052
jboolean isException)
3053
{
3054
instruction_data_type *idata = context->instruction_data;
3055
register_info_type register_info_buf;
3056
stack_info_type stack_info_buf;
3057
#ifdef DEBUG
3058
instruction_data_type *this_idata = &idata[to_inumber];
3059
register_info_type old_reg_info;
3060
stack_info_type old_stack_info;
3061
flag_type old_and_flags = 0;
3062
flag_type old_or_flags = 0;
3063
#endif
3064
3065
#ifdef DEBUG
3066
if (verify_verbose) {
3067
old_reg_info = this_idata->register_info;
3068
old_stack_info = this_idata->stack_info;
3069
old_and_flags = this_idata->and_flags;
3070
old_or_flags = this_idata->or_flags;
3071
}
3072
#endif
3073
3074
/* All uninitialized objects are set to "bogus" when jsr and
3075
* ret are executed. Thus uninitialized objects can't propagate
3076
* into or out of a subroutine.
3077
*/
3078
if (idata[from_inumber].opcode == JVM_OPC_ret ||
3079
idata[from_inumber].opcode == JVM_OPC_jsr ||
3080
idata[from_inumber].opcode == JVM_OPC_jsr_w) {
3081
int new_register_count = new_register_info->register_count;
3082
fullinfo_type *new_registers = new_register_info->registers;
3083
int i;
3084
stack_item_type *item;
3085
3086
for (item = new_stack_info->stack; item != NULL; item = item->next) {
3087
if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
3088
/* This check only succeeds for hand-contrived code.
3089
* Efficiency is not an issue.
3090
*/
3091
stack_info_buf.stack = copy_stack(context,
3092
new_stack_info->stack);
3093
stack_info_buf.stack_size = new_stack_info->stack_size;
3094
new_stack_info = &stack_info_buf;
3095
for (item = new_stack_info->stack; item != NULL;
3096
item = item->next) {
3097
if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
3098
item->item = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3099
}
3100
}
3101
break;
3102
}
3103
}
3104
for (i = 0; i < new_register_count; i++) {
3105
if (GET_ITEM_TYPE(new_registers[i]) == ITEM_NewObject) {
3106
/* This check only succeeds for hand-contrived code.
3107
* Efficiency is not an issue.
3108
*/
3109
fullinfo_type *new_set = NEW(fullinfo_type,
3110
new_register_count);
3111
for (i = 0; i < new_register_count; i++) {
3112
fullinfo_type t = new_registers[i];
3113
new_set[i] = GET_ITEM_TYPE(t) != ITEM_NewObject ?
3114
t : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3115
}
3116
register_info_buf.register_count = new_register_count;
3117
register_info_buf.registers = new_set;
3118
register_info_buf.mask_count = new_register_info->mask_count;
3119
register_info_buf.masks = new_register_info->masks;
3120
new_register_info = &register_info_buf;
3121
break;
3122
}
3123
}
3124
}
3125
3126
/* Returning from a subroutine is somewhat ugly. The actual thing
3127
* that needs to get merged into the new instruction is a joining
3128
* of info from the ret instruction with stuff in the jsr instruction
3129
*/
3130
if (idata[from_inumber].opcode == JVM_OPC_ret && !isException) {
3131
int new_register_count = new_register_info->register_count;
3132
fullinfo_type *new_registers = new_register_info->registers;
3133
int new_mask_count = new_register_info->mask_count;
3134
mask_type *new_masks = new_register_info->masks;
3135
int operand = idata[from_inumber].operand.i;
3136
int called_instruction = GET_EXTRA_INFO(new_registers[operand]);
3137
instruction_data_type *jsr_idata = &idata[to_inumber - 1];
3138
register_info_type *jsr_reginfo = &jsr_idata->register_info;
3139
if (jsr_idata->operand2.i != (int)from_inumber) {
3140
if (jsr_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
3141
CCerror(context, "Multiple returns to single jsr");
3142
jsr_idata->operand2.i = from_inumber;
3143
}
3144
if (jsr_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
3145
/* We don't want to handle the returned-to instruction until
3146
* we've dealt with the jsr instruction. When we get to the
3147
* jsr instruction (if ever), we'll re-mark the ret instruction
3148
*/
3149
;
3150
} else {
3151
int register_count = jsr_reginfo->register_count;
3152
fullinfo_type *registers = jsr_reginfo->registers;
3153
int max_registers = MAX(register_count, new_register_count);
3154
fullinfo_type *new_set = NEW(fullinfo_type, max_registers);
3155
int *return_mask;
3156
struct register_info_type new_new_register_info;
3157
int i;
3158
/* Make sure the place we're returning from is legal! */
3159
for (i = new_mask_count; --i >= 0; )
3160
if (new_masks[i].entry == called_instruction)
3161
break;
3162
if (i < 0)
3163
CCerror(context, "Illegal return from subroutine");
3164
/* pop the masks down to the indicated one. Remember the mask
3165
* we're popping off. */
3166
return_mask = new_masks[i].modifies;
3167
new_mask_count = i;
3168
for (i = 0; i < max_registers; i++) {
3169
if (IS_BIT_SET(return_mask, i))
3170
new_set[i] = i < new_register_count ?
3171
new_registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3172
else
3173
new_set[i] = i < register_count ?
3174
registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3175
}
3176
new_new_register_info.register_count = max_registers;
3177
new_new_register_info.registers = new_set;
3178
new_new_register_info.mask_count = new_mask_count;
3179
new_new_register_info.masks = new_masks;
3180
3181
3182
merge_stack(context, from_inumber, to_inumber, new_stack_info);
3183
merge_registers(context, to_inumber - 1, to_inumber,
3184
&new_new_register_info);
3185
merge_flags(context, from_inumber, to_inumber, new_and_flags, new_or_flags);
3186
}
3187
} else {
3188
merge_stack(context, from_inumber, to_inumber, new_stack_info);
3189
merge_registers(context, from_inumber, to_inumber, new_register_info);
3190
merge_flags(context, from_inumber, to_inumber,
3191
new_and_flags, new_or_flags);
3192
}
3193
3194
#ifdef DEBUG
3195
if (verify_verbose && idata[to_inumber].changed) {
3196
register_info_type *register_info = &this_idata->register_info;
3197
stack_info_type *stack_info = &this_idata->stack_info;
3198
if (memcmp(&old_reg_info, register_info, sizeof(old_reg_info)) ||
3199
memcmp(&old_stack_info, stack_info, sizeof(old_stack_info)) ||
3200
(old_and_flags != this_idata->and_flags) ||
3201
(old_or_flags != this_idata->or_flags)) {
3202
jio_fprintf(stdout, " %2d:", to_inumber);
3203
print_stack(context, &old_stack_info);
3204
print_registers(context, &old_reg_info);
3205
print_flags(context, old_and_flags, old_or_flags);
3206
jio_fprintf(stdout, " => ");
3207
print_stack(context, &this_idata->stack_info);
3208
print_registers(context, &this_idata->register_info);
3209
print_flags(context, this_idata->and_flags, this_idata->or_flags);
3210
jio_fprintf(stdout, "\n");
3211
}
3212
}
3213
#endif
3214
3215
}
3216
3217
static void
3218
merge_stack(context_type *context, unsigned int from_inumber,
3219
unsigned int to_inumber, stack_info_type *new_stack_info)
3220
{
3221
instruction_data_type *idata = context->instruction_data;
3222
instruction_data_type *this_idata = &idata[to_inumber];
3223
3224
int new_stack_size = new_stack_info->stack_size;
3225
stack_item_type *new_stack = new_stack_info->stack;
3226
3227
int stack_size = this_idata->stack_info.stack_size;
3228
3229
if (stack_size == UNKNOWN_STACK_SIZE) {
3230
/* First time at this instruction. Just copy. */
3231
this_idata->stack_info.stack_size = new_stack_size;
3232
this_idata->stack_info.stack = new_stack;
3233
this_idata->changed = JNI_TRUE;
3234
} else if (new_stack_size != stack_size) {
3235
CCerror(context, "Inconsistent stack height %d != %d",
3236
new_stack_size, stack_size);
3237
} else {
3238
stack_item_type *stack = this_idata->stack_info.stack;
3239
stack_item_type *old, *new;
3240
jboolean change = JNI_FALSE;
3241
for (old = stack, new = new_stack; old != NULL;
3242
old = old->next, new = new->next) {
3243
if (!isAssignableTo(context, new->item, old->item)) {
3244
change = JNI_TRUE;
3245
break;
3246
}
3247
}
3248
if (change) {
3249
stack = copy_stack(context, stack);
3250
for (old = stack, new = new_stack; old != NULL;
3251
old = old->next, new = new->next) {
3252
if (new == NULL) {
3253
break;
3254
}
3255
old->item = merge_fullinfo_types(context, old->item, new->item,
3256
JNI_FALSE);
3257
if (GET_ITEM_TYPE(old->item) == ITEM_Bogus) {
3258
CCerror(context, "Mismatched stack types");
3259
}
3260
}
3261
if (old != NULL || new != NULL) {
3262
CCerror(context, "Mismatched stack types");
3263
}
3264
this_idata->stack_info.stack = stack;
3265
this_idata->changed = JNI_TRUE;
3266
}
3267
}
3268
}
3269
3270
static void
3271
merge_registers(context_type *context, unsigned int from_inumber,
3272
unsigned int to_inumber, register_info_type *new_register_info)
3273
{
3274
instruction_data_type *idata = context->instruction_data;
3275
instruction_data_type *this_idata = &idata[to_inumber];
3276
register_info_type *this_reginfo = &this_idata->register_info;
3277
3278
int new_register_count = new_register_info->register_count;
3279
fullinfo_type *new_registers = new_register_info->registers;
3280
int new_mask_count = new_register_info->mask_count;
3281
mask_type *new_masks = new_register_info->masks;
3282
3283
3284
if (this_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
3285
this_reginfo->register_count = new_register_count;
3286
this_reginfo->registers = new_registers;
3287
this_reginfo->mask_count = new_mask_count;
3288
this_reginfo->masks = new_masks;
3289
this_idata->changed = JNI_TRUE;
3290
} else {
3291
/* See if we've got new information on the register set. */
3292
int register_count = this_reginfo->register_count;
3293
fullinfo_type *registers = this_reginfo->registers;
3294
int mask_count = this_reginfo->mask_count;
3295
mask_type *masks = this_reginfo->masks;
3296
3297
jboolean copy = JNI_FALSE;
3298
int i, j;
3299
if (register_count > new_register_count) {
3300
/* Any register larger than new_register_count is now bogus */
3301
this_reginfo->register_count = new_register_count;
3302
register_count = new_register_count;
3303
this_idata->changed = JNI_TRUE;
3304
}
3305
for (i = 0; i < register_count; i++) {
3306
fullinfo_type prev_value = registers[i];
3307
if ((i < new_register_count)
3308
? (!isAssignableTo(context, new_registers[i], prev_value))
3309
: (prev_value != MAKE_FULLINFO(ITEM_Bogus, 0, 0))) {
3310
copy = JNI_TRUE;
3311
break;
3312
}
3313
}
3314
3315
if (copy) {
3316
/* We need a copy. So do it. */
3317
fullinfo_type *new_set = NEW(fullinfo_type, register_count);
3318
for (j = 0; j < i; j++)
3319
new_set[j] = registers[j];
3320
for (j = i; j < register_count; j++) {
3321
if (i >= new_register_count)
3322
new_set[j] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3323
else
3324
new_set[j] = merge_fullinfo_types(context,
3325
new_registers[j],
3326
registers[j], JNI_FALSE);
3327
}
3328
/* Some of the end items might now be bogus. This step isn't
3329
* necessary, but it may save work later. */
3330
while ( register_count > 0
3331
&& GET_ITEM_TYPE(new_set[register_count-1]) == ITEM_Bogus)
3332
register_count--;
3333
this_reginfo->register_count = register_count;
3334
this_reginfo->registers = new_set;
3335
this_idata->changed = JNI_TRUE;
3336
}
3337
if (mask_count > 0) {
3338
/* If the target instruction already has a sequence of masks, then
3339
* we need to merge new_masks into it. We want the entries on
3340
* the mask to be the longest common substring of the two.
3341
* (e.g. a->b->d merged with a->c->d should give a->d)
3342
* The bits set in the mask should be the or of the corresponding
3343
* entries in each of the original masks.
3344
*/
3345
int i, j, k;
3346
int matches = 0;
3347
int last_match = -1;
3348
jboolean copy_needed = JNI_FALSE;
3349
for (i = 0; i < mask_count; i++) {
3350
int entry = masks[i].entry;
3351
for (j = last_match + 1; j < new_mask_count; j++) {
3352
if (new_masks[j].entry == entry) {
3353
/* We have a match */
3354
int *prev = masks[i].modifies;
3355
int *new = new_masks[j].modifies;
3356
matches++;
3357
/* See if new_mask has bits set for "entry" that
3358
* weren't set for mask. If so, need to copy. */
3359
for (k = context->bitmask_size - 1;
3360
!copy_needed && k >= 0;
3361
k--)
3362
if (~prev[k] & new[k])
3363
copy_needed = JNI_TRUE;
3364
last_match = j;
3365
break;
3366
}
3367
}
3368
}
3369
if ((matches < mask_count) || copy_needed) {
3370
/* We need to make a copy for the new item, since either the
3371
* size has decreased, or new bits are set. */
3372
mask_type *copy = NEW(mask_type, matches);
3373
for (i = 0; i < matches; i++) {
3374
copy[i].modifies = NEW(int, context->bitmask_size);
3375
}
3376
this_reginfo->masks = copy;
3377
this_reginfo->mask_count = matches;
3378
this_idata->changed = JNI_TRUE;
3379
matches = 0;
3380
last_match = -1;
3381
for (i = 0; i < mask_count; i++) {
3382
int entry = masks[i].entry;
3383
for (j = last_match + 1; j < new_mask_count; j++) {
3384
if (new_masks[j].entry == entry) {
3385
int *prev1 = masks[i].modifies;
3386
int *prev2 = new_masks[j].modifies;
3387
int *new = copy[matches].modifies;
3388
copy[matches].entry = entry;
3389
for (k = context->bitmask_size - 1; k >= 0; k--)
3390
new[k] = prev1[k] | prev2[k];
3391
matches++;
3392
last_match = j;
3393
break;
3394
}
3395
}
3396
}
3397
}
3398
}
3399
}
3400
}
3401
3402
3403
static void
3404
merge_flags(context_type *context, unsigned int from_inumber,
3405
unsigned int to_inumber,
3406
flag_type new_and_flags, flag_type new_or_flags)
3407
{
3408
/* Set this_idata->and_flags &= new_and_flags
3409
this_idata->or_flags |= new_or_flags
3410
*/
3411
instruction_data_type *idata = context->instruction_data;
3412
instruction_data_type *this_idata = &idata[to_inumber];
3413
flag_type this_and_flags = this_idata->and_flags;
3414
flag_type this_or_flags = this_idata->or_flags;
3415
flag_type merged_and = this_and_flags & new_and_flags;
3416
flag_type merged_or = this_or_flags | new_or_flags;
3417
3418
if ((merged_and != this_and_flags) || (merged_or != this_or_flags)) {
3419
this_idata->and_flags = merged_and;
3420
this_idata->or_flags = merged_or;
3421
this_idata->changed = JNI_TRUE;
3422
}
3423
}
3424
3425
3426
/* Make a copy of a stack */
3427
3428
static stack_item_type *
3429
copy_stack(context_type *context, stack_item_type *stack)
3430
{
3431
int length;
3432
stack_item_type *ptr;
3433
3434
/* Find the length */
3435
for (ptr = stack, length = 0; ptr != NULL; ptr = ptr->next, length++);
3436
3437
if (length > 0) {
3438
stack_item_type *new_stack = NEW(stack_item_type, length);
3439
stack_item_type *new_ptr;
3440
for ( ptr = stack, new_ptr = new_stack;
3441
ptr != NULL;
3442
ptr = ptr->next, new_ptr++) {
3443
new_ptr->item = ptr->item;
3444
new_ptr->next = new_ptr + 1;
3445
}
3446
new_stack[length - 1].next = NULL;
3447
return new_stack;
3448
} else {
3449
return NULL;
3450
}
3451
}
3452
3453
3454
static mask_type *
3455
copy_masks(context_type *context, mask_type *masks, int mask_count)
3456
{
3457
mask_type *result = NEW(mask_type, mask_count);
3458
int bitmask_size = context->bitmask_size;
3459
int *bitmaps = NEW(int, mask_count * bitmask_size);
3460
int i;
3461
for (i = 0; i < mask_count; i++) {
3462
result[i].entry = masks[i].entry;
3463
result[i].modifies = &bitmaps[i * bitmask_size];
3464
memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
3465
}
3466
return result;
3467
}
3468
3469
3470
static mask_type *
3471
add_to_masks(context_type *context, mask_type *masks, int mask_count, int d)
3472
{
3473
mask_type *result = NEW(mask_type, mask_count + 1);
3474
int bitmask_size = context->bitmask_size;
3475
int *bitmaps = NEW(int, (mask_count + 1) * bitmask_size);
3476
int i;
3477
for (i = 0; i < mask_count; i++) {
3478
result[i].entry = masks[i].entry;
3479
result[i].modifies = &bitmaps[i * bitmask_size];
3480
memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
3481
}
3482
result[mask_count].entry = d;
3483
result[mask_count].modifies = &bitmaps[mask_count * bitmask_size];
3484
memset(result[mask_count].modifies, 0, bitmask_size * sizeof(int));
3485
return result;
3486
}
3487
3488
3489
3490
/* We create our own storage manager, since we malloc lots of little items,
3491
* and I don't want to keep trace of when they become free. I sure wish that
3492
* we had heaps, and I could just free the heap when done.
3493
*/
3494
3495
#define CCSegSize 2000
3496
3497
struct CCpool { /* a segment of allocated memory in the pool */
3498
struct CCpool *next;
3499
int segSize; /* almost always CCSegSize */
3500
int poolPad;
3501
char space[CCSegSize];
3502
};
3503
3504
/* Initialize the context's heap. */
3505
static void CCinit(context_type *context)
3506
{
3507
struct CCpool *new = (struct CCpool *) malloc(sizeof(struct CCpool));
3508
/* Set context->CCroot to 0 if new == 0 to tell CCdestroy to lay off */
3509
context->CCroot = context->CCcurrent = new;
3510
if (new == 0) {
3511
CCout_of_memory(context);
3512
}
3513
new->next = NULL;
3514
new->segSize = CCSegSize;
3515
context->CCfree_size = CCSegSize;
3516
context->CCfree_ptr = &new->space[0];
3517
}
3518
3519
3520
/* Reuse all the space that we have in the context's heap. */
3521
static void CCreinit(context_type *context)
3522
{
3523
struct CCpool *first = context->CCroot;
3524
context->CCcurrent = first;
3525
context->CCfree_size = CCSegSize;
3526
context->CCfree_ptr = &first->space[0];
3527
}
3528
3529
/* Destroy the context's heap. */
3530
static void CCdestroy(context_type *context)
3531
{
3532
struct CCpool *this = context->CCroot;
3533
while (this) {
3534
struct CCpool *next = this->next;
3535
free(this);
3536
this = next;
3537
}
3538
/* These two aren't necessary. But can't hurt either */
3539
context->CCroot = context->CCcurrent = NULL;
3540
context->CCfree_ptr = 0;
3541
}
3542
3543
/* Allocate an object of the given size from the context's heap. */
3544
static void *
3545
CCalloc(context_type *context, int size, jboolean zero)
3546
{
3547
3548
register char *p;
3549
/* Round CC to the size of a pointer */
3550
size = (size + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1);
3551
3552
if (context->CCfree_size < size) {
3553
struct CCpool *current = context->CCcurrent;
3554
struct CCpool *new;
3555
if (size > CCSegSize) { /* we need to allocate a special block */
3556
new = (struct CCpool *)malloc(sizeof(struct CCpool) +
3557
(size - CCSegSize));
3558
if (new == 0) {
3559
CCout_of_memory(context);
3560
}
3561
new->next = current->next;
3562
new->segSize = size;
3563
current->next = new;
3564
} else {
3565
new = current->next;
3566
if (new == NULL) {
3567
new = (struct CCpool *) malloc(sizeof(struct CCpool));
3568
if (new == 0) {
3569
CCout_of_memory(context);
3570
}
3571
current->next = new;
3572
new->next = NULL;
3573
new->segSize = CCSegSize;
3574
}
3575
}
3576
context->CCcurrent = new;
3577
context->CCfree_ptr = &new->space[0];
3578
context->CCfree_size = new->segSize;
3579
}
3580
p = context->CCfree_ptr;
3581
context->CCfree_ptr += size;
3582
context->CCfree_size -= size;
3583
if (zero)
3584
memset(p, 0, size);
3585
return p;
3586
}
3587
3588
/* Get the class associated with a particular field or method or class in the
3589
* constant pool. If is_field is true, we've got a field or method. If
3590
* false, we've got a class.
3591
*/
3592
static fullinfo_type
3593
cp_index_to_class_fullinfo(context_type *context, int cp_index, int kind)
3594
{
3595
JNIEnv *env = context->env;
3596
fullinfo_type result;
3597
const char *classname;
3598
switch (kind) {
3599
case JVM_CONSTANT_Class:
3600
classname = JVM_GetCPClassNameUTF(env,
3601
context->class,
3602
cp_index);
3603
break;
3604
case JVM_CONSTANT_Methodref:
3605
classname = JVM_GetCPMethodClassNameUTF(env,
3606
context->class,
3607
cp_index);
3608
break;
3609
case JVM_CONSTANT_Fieldref:
3610
classname = JVM_GetCPFieldClassNameUTF(env,
3611
context->class,
3612
cp_index);
3613
break;
3614
default:
3615
classname = NULL;
3616
CCerror(context, "Internal error #5");
3617
}
3618
3619
check_and_push(context, classname, VM_STRING_UTF);
3620
if (classname[0] == JVM_SIGNATURE_ARRAY) {
3621
/* This make recursively call us, in case of a class array */
3622
signature_to_fieldtype(context, &classname, &result);
3623
} else {
3624
result = make_class_info_from_name(context, classname);
3625
}
3626
pop_and_free(context);
3627
return result;
3628
}
3629
3630
3631
static int
3632
print_CCerror_info(context_type *context)
3633
{
3634
JNIEnv *env = context->env;
3635
jclass cb = context->class;
3636
const char *classname = JVM_GetClassNameUTF(env, cb);
3637
const char *name = 0;
3638
const char *signature = 0;
3639
int n = 0;
3640
if (context->method_index != -1) {
3641
name = JVM_GetMethodIxNameUTF(env, cb, context->method_index);
3642
signature =
3643
JVM_GetMethodIxSignatureUTF(env, cb, context->method_index);
3644
n += jio_snprintf(context->message, context->message_buf_len,
3645
"(class: %s, method: %s signature: %s) ",
3646
(classname ? classname : ""),
3647
(name ? name : ""),
3648
(signature ? signature : ""));
3649
} else if (context->field_index != -1 ) {
3650
name = JVM_GetMethodIxNameUTF(env, cb, context->field_index);
3651
n += jio_snprintf(context->message, context->message_buf_len,
3652
"(class: %s, field: %s) ",
3653
(classname ? classname : 0),
3654
(name ? name : 0));
3655
} else {
3656
n += jio_snprintf(context->message, context->message_buf_len,
3657
"(class: %s) ", classname ? classname : "");
3658
}
3659
JVM_ReleaseUTF(classname);
3660
JVM_ReleaseUTF(name);
3661
JVM_ReleaseUTF(signature);
3662
return n;
3663
}
3664
3665
static void
3666
CCerror (context_type *context, char *format, ...)
3667
{
3668
int n = print_CCerror_info(context);
3669
va_list args;
3670
if (n >= 0 && n < context->message_buf_len) {
3671
va_start(args, format);
3672
jio_vsnprintf(context->message + n, context->message_buf_len - n,
3673
format, args);
3674
va_end(args);
3675
}
3676
context->err_code = CC_VerifyError;
3677
longjmp(context->jump_buffer, 1);
3678
}
3679
3680
static void
3681
CCout_of_memory(context_type *context)
3682
{
3683
int n = print_CCerror_info(context);
3684
context->err_code = CC_OutOfMemory;
3685
longjmp(context->jump_buffer, 1);
3686
}
3687
3688
static void
3689
CFerror(context_type *context, char *format, ...)
3690
{
3691
int n = print_CCerror_info(context);
3692
va_list args;
3693
if (n >= 0 && n < context->message_buf_len) {
3694
va_start(args, format);
3695
jio_vsnprintf(context->message + n, context->message_buf_len - n,
3696
format, args);
3697
va_end(args);
3698
}
3699
context->err_code = CC_ClassFormatError;
3700
longjmp(context->jump_buffer, 1);
3701
}
3702
3703
/*
3704
* Need to scan the entire signature to find the result type because
3705
* types in the arg list and the result type could contain embedded ')'s.
3706
*/
3707
static const char* get_result_signature(const char* signature) {
3708
const char *p;
3709
for (p = signature; *p != JVM_SIGNATURE_ENDFUNC; p++) {
3710
switch (*p) {
3711
case JVM_SIGNATURE_BOOLEAN:
3712
case JVM_SIGNATURE_BYTE:
3713
case JVM_SIGNATURE_CHAR:
3714
case JVM_SIGNATURE_SHORT:
3715
case JVM_SIGNATURE_INT:
3716
case JVM_SIGNATURE_FLOAT:
3717
case JVM_SIGNATURE_DOUBLE:
3718
case JVM_SIGNATURE_LONG:
3719
case JVM_SIGNATURE_FUNC: /* ignore initial (, if given */
3720
break;
3721
case JVM_SIGNATURE_CLASS:
3722
while (*p != JVM_SIGNATURE_ENDCLASS) p++;
3723
break;
3724
case JVM_SIGNATURE_ARRAY:
3725
while (*p == JVM_SIGNATURE_ARRAY) p++;
3726
/* If an array of classes, skip over class name, too. */
3727
if (*p == JVM_SIGNATURE_CLASS) {
3728
while (*p != JVM_SIGNATURE_ENDCLASS) p++;
3729
}
3730
break;
3731
default:
3732
/* Indicate an error. */
3733
return NULL;
3734
}
3735
}
3736
return p++; /* skip over ')'. */
3737
}
3738
3739
static char
3740
signature_to_fieldtype(context_type *context,
3741
const char **signature_p, fullinfo_type *full_info_p)
3742
{
3743
const char *p = *signature_p;
3744
fullinfo_type full_info = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3745
char result;
3746
int array_depth = 0;
3747
3748
for (;;) {
3749
switch(*p++) {
3750
default:
3751
result = 0;
3752
break;
3753
3754
case JVM_SIGNATURE_BOOLEAN: case JVM_SIGNATURE_BYTE:
3755
full_info = (array_depth > 0)
3756
? MAKE_FULLINFO(ITEM_Byte, 0, 0)
3757
: MAKE_FULLINFO(ITEM_Integer, 0, 0);
3758
result = 'I';
3759
break;
3760
3761
case JVM_SIGNATURE_CHAR:
3762
full_info = (array_depth > 0)
3763
? MAKE_FULLINFO(ITEM_Char, 0, 0)
3764
: MAKE_FULLINFO(ITEM_Integer, 0, 0);
3765
result = 'I';
3766
break;
3767
3768
case JVM_SIGNATURE_SHORT:
3769
full_info = (array_depth > 0)
3770
? MAKE_FULLINFO(ITEM_Short, 0, 0)
3771
: MAKE_FULLINFO(ITEM_Integer, 0, 0);
3772
result = 'I';
3773
break;
3774
3775
case JVM_SIGNATURE_INT:
3776
full_info = MAKE_FULLINFO(ITEM_Integer, 0, 0);
3777
result = 'I';
3778
break;
3779
3780
case JVM_SIGNATURE_FLOAT:
3781
full_info = MAKE_FULLINFO(ITEM_Float, 0, 0);
3782
result = 'F';
3783
break;
3784
3785
case JVM_SIGNATURE_DOUBLE:
3786
full_info = MAKE_FULLINFO(ITEM_Double, 0, 0);
3787
result = 'D';
3788
break;
3789
3790
case JVM_SIGNATURE_LONG:
3791
full_info = MAKE_FULLINFO(ITEM_Long, 0, 0);
3792
result = 'L';
3793
break;
3794
3795
case JVM_SIGNATURE_ARRAY:
3796
array_depth++;
3797
continue; /* only time we ever do the loop > 1 */
3798
3799
case JVM_SIGNATURE_CLASS: {
3800
char buffer_space[256];
3801
char *buffer = buffer_space;
3802
char *finish = strchr(p, JVM_SIGNATURE_ENDCLASS);
3803
int length;
3804
if (finish == NULL) {
3805
/* Signature must have ';' after the class name.
3806
* If it does not, return 0 and ITEM_Bogus in full_info. */
3807
result = 0;
3808
break;
3809
}
3810
length = finish - p;
3811
if (length + 1 > (int)sizeof(buffer_space)) {
3812
buffer = malloc(length + 1);
3813
check_and_push(context, buffer, VM_MALLOC_BLK);
3814
}
3815
memcpy(buffer, p, length);
3816
buffer[length] = '\0';
3817
full_info = make_class_info_from_name(context, buffer);
3818
result = 'A';
3819
p = finish + 1;
3820
if (buffer != buffer_space)
3821
pop_and_free(context);
3822
break;
3823
}
3824
} /* end of switch */
3825
break;
3826
}
3827
*signature_p = p;
3828
if (array_depth == 0 || result == 0) {
3829
/* either not an array, or result is bogus */
3830
*full_info_p = full_info;
3831
return result;
3832
} else {
3833
if (array_depth > MAX_ARRAY_DIMENSIONS)
3834
CCerror(context, "Array with too many dimensions");
3835
*full_info_p = MAKE_FULLINFO(GET_ITEM_TYPE(full_info),
3836
array_depth,
3837
GET_EXTRA_INFO(full_info));
3838
return 'A';
3839
}
3840
}
3841
3842
3843
/* Given an array type, create the type that has one less level of
3844
* indirection.
3845
*/
3846
3847
static fullinfo_type
3848
decrement_indirection(fullinfo_type array_info)
3849
{
3850
if (array_info == NULL_FULLINFO) {
3851
return NULL_FULLINFO;
3852
} else {
3853
int type = GET_ITEM_TYPE(array_info);
3854
int indirection = GET_INDIRECTION(array_info) - 1;
3855
int extra_info = GET_EXTRA_INFO(array_info);
3856
if ( (indirection == 0)
3857
&& ((type == ITEM_Short || type == ITEM_Byte || type == ITEM_Char)))
3858
type = ITEM_Integer;
3859
return MAKE_FULLINFO(type, indirection, extra_info);
3860
}
3861
}
3862
3863
3864
/* See if we can assign an object of the "from" type to an object
3865
* of the "to" type.
3866
*/
3867
3868
static jboolean isAssignableTo(context_type *context,
3869
fullinfo_type from, fullinfo_type to)
3870
{
3871
return (merge_fullinfo_types(context, from, to, JNI_TRUE) == to);
3872
}
3873
3874
/* Given two fullinfo_type's, find their lowest common denominator. If
3875
* the assignable_p argument is non-null, we're really just calling to find
3876
* out if "<target> := <value>" is a legitimate assignment.
3877
*
3878
* We treat all interfaces as if they were of type java/lang/Object, since the
3879
* runtime will do the full checking.
3880
*/
3881
static fullinfo_type
3882
merge_fullinfo_types(context_type *context,
3883
fullinfo_type value, fullinfo_type target,
3884
jboolean for_assignment)
3885
{
3886
JNIEnv *env = context->env;
3887
if (value == target) {
3888
/* If they're identical, clearly just return what we've got */
3889
return value;
3890
}
3891
3892
/* Both must be either arrays or objects to go further */
3893
if (GET_INDIRECTION(value) == 0 && GET_ITEM_TYPE(value) != ITEM_Object)
3894
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3895
if (GET_INDIRECTION(target) == 0 && GET_ITEM_TYPE(target) != ITEM_Object)
3896
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3897
3898
/* If either is NULL, return the other. */
3899
if (value == NULL_FULLINFO)
3900
return target;
3901
else if (target == NULL_FULLINFO)
3902
return value;
3903
3904
/* If either is java/lang/Object, that's the result. */
3905
if (target == context->object_info)
3906
return target;
3907
else if (value == context->object_info) {
3908
/* Minor hack. For assignments, Interface := Object, return Interface
3909
* rather than Object, so that isAssignableTo() will get the right
3910
* result. */
3911
if (for_assignment && (WITH_ZERO_EXTRA_INFO(target) ==
3912
MAKE_FULLINFO(ITEM_Object, 0, 0))) {
3913
jclass cb = object_fullinfo_to_classclass(context,
3914
target);
3915
int is_interface = cb && JVM_IsInterface(env, cb);
3916
if (is_interface)
3917
return target;
3918
}
3919
return value;
3920
}
3921
if (GET_INDIRECTION(value) > 0 || GET_INDIRECTION(target) > 0) {
3922
/* At least one is an array. Neither is java/lang/Object or NULL.
3923
* Moreover, the types are not identical.
3924
* The result must either be Object, or an array of some object type.
3925
*/
3926
fullinfo_type value_base, target_base;
3927
int dimen_value = GET_INDIRECTION(value);
3928
int dimen_target = GET_INDIRECTION(target);
3929
3930
if (target == context->cloneable_info ||
3931
target == context->serializable_info) {
3932
return target;
3933
}
3934
3935
if (value == context->cloneable_info ||
3936
value == context->serializable_info) {
3937
return value;
3938
}
3939
3940
/* First, if either item's base type isn't ITEM_Object, promote it up
3941
* to an object or array of object. If either is elemental, we can
3942
* punt.
3943
*/
3944
if (GET_ITEM_TYPE(value) != ITEM_Object) {
3945
if (dimen_value == 0)
3946
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3947
dimen_value--;
3948
value = MAKE_Object_ARRAY(dimen_value);
3949
3950
}
3951
if (GET_ITEM_TYPE(target) != ITEM_Object) {
3952
if (dimen_target == 0)
3953
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3954
dimen_target--;
3955
target = MAKE_Object_ARRAY(dimen_target);
3956
}
3957
/* Both are now objects or arrays of some sort of object type */
3958
value_base = WITH_ZERO_INDIRECTION(value);
3959
target_base = WITH_ZERO_INDIRECTION(target);
3960
if (dimen_value == dimen_target) {
3961
/* Arrays of the same dimension. Merge their base types. */
3962
fullinfo_type result_base =
3963
merge_fullinfo_types(context, value_base, target_base,
3964
for_assignment);
3965
if (result_base == MAKE_FULLINFO(ITEM_Bogus, 0, 0))
3966
/* bogus in, bogus out */
3967
return result_base;
3968
return MAKE_FULLINFO(ITEM_Object, dimen_value,
3969
GET_EXTRA_INFO(result_base));
3970
} else {
3971
/* Arrays of different sizes. If the smaller dimension array's base
3972
* type is java/lang/Cloneable or java/io/Serializable, return it.
3973
* Otherwise return java/lang/Object with a dimension of the smaller
3974
* of the two */
3975
if (dimen_value < dimen_target) {
3976
if (value_base == context->cloneable_info ||
3977
value_base == context ->serializable_info) {
3978
return value;
3979
}
3980
return MAKE_Object_ARRAY(dimen_value);
3981
} else {
3982
if (target_base == context->cloneable_info ||
3983
target_base == context->serializable_info) {
3984
return target;
3985
}
3986
return MAKE_Object_ARRAY(dimen_target);
3987
}
3988
}
3989
} else {
3990
/* Both are non-array objects. Neither is java/lang/Object or NULL */
3991
jclass cb_value, cb_target, cb_super_value, cb_super_target;
3992
fullinfo_type result_info;
3993
3994
/* Let's get the classes corresponding to each of these. Treat
3995
* interfaces as if they were java/lang/Object. See hack note above. */
3996
cb_target = object_fullinfo_to_classclass(context, target);
3997
if (cb_target == 0)
3998
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
3999
if (JVM_IsInterface(env, cb_target))
4000
return for_assignment ? target : context->object_info;
4001
cb_value = object_fullinfo_to_classclass(context, value);
4002
if (cb_value == 0)
4003
return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
4004
if (JVM_IsInterface(env, cb_value))
4005
return context->object_info;
4006
4007
/* If this is for assignment of target := value, we just need to see if
4008
* cb_target is a superclass of cb_value. Save ourselves a lot of
4009
* work.
4010
*/
4011
if (for_assignment) {
4012
cb_super_value = (*env)->GetSuperclass(env, cb_value);
4013
while (cb_super_value != 0) {
4014
jclass tmp_cb;
4015
if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
4016
(*env)->DeleteLocalRef(env, cb_super_value);
4017
return target;
4018
}
4019
tmp_cb = (*env)->GetSuperclass(env, cb_super_value);
4020
(*env)->DeleteLocalRef(env, cb_super_value);
4021
cb_super_value = tmp_cb;
4022
}
4023
(*env)->DeleteLocalRef(env, cb_super_value);
4024
return context->object_info;
4025
}
4026
4027
/* Find out whether cb_value or cb_target is deeper in the class
4028
* tree by moving both toward the root, and seeing who gets there
4029
* first. */
4030
cb_super_value = (*env)->GetSuperclass(env, cb_value);
4031
cb_super_target = (*env)->GetSuperclass(env, cb_target);
4032
while((cb_super_value != 0) &&
4033
(cb_super_target != 0)) {
4034
jclass tmp_cb;
4035
/* Optimization. If either hits the other when going up looking
4036
* for a parent, then might as well return the parent immediately */
4037
if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
4038
(*env)->DeleteLocalRef(env, cb_super_value);
4039
(*env)->DeleteLocalRef(env, cb_super_target);
4040
return target;
4041
}
4042
if ((*env)->IsSameObject(env, cb_super_target, cb_value)) {
4043
(*env)->DeleteLocalRef(env, cb_super_value);
4044
(*env)->DeleteLocalRef(env, cb_super_target);
4045
return value;
4046
}
4047
tmp_cb = (*env)->GetSuperclass(env, cb_super_value);
4048
(*env)->DeleteLocalRef(env, cb_super_value);
4049
cb_super_value = tmp_cb;
4050
4051
tmp_cb = (*env)->GetSuperclass(env, cb_super_target);
4052
(*env)->DeleteLocalRef(env, cb_super_target);
4053
cb_super_target = tmp_cb;
4054
}
4055
cb_value = (*env)->NewLocalRef(env, cb_value);
4056
cb_target = (*env)->NewLocalRef(env, cb_target);
4057
/* At most one of the following two while clauses will be executed.
4058
* Bring the deeper of cb_target and cb_value to the depth of the
4059
* shallower one.
4060
*/
4061
while (cb_super_value != 0) {
4062
/* cb_value is deeper */
4063
jclass cb_tmp;
4064
4065
cb_tmp = (*env)->GetSuperclass(env, cb_super_value);
4066
(*env)->DeleteLocalRef(env, cb_super_value);
4067
cb_super_value = cb_tmp;
4068
4069
cb_tmp = (*env)->GetSuperclass(env, cb_value);
4070
(*env)->DeleteLocalRef(env, cb_value);
4071
cb_value = cb_tmp;
4072
}
4073
while (cb_super_target != 0) {
4074
/* cb_target is deeper */
4075
jclass cb_tmp;
4076
4077
cb_tmp = (*env)->GetSuperclass(env, cb_super_target);
4078
(*env)->DeleteLocalRef(env, cb_super_target);
4079
cb_super_target = cb_tmp;
4080
4081
cb_tmp = (*env)->GetSuperclass(env, cb_target);
4082
(*env)->DeleteLocalRef(env, cb_target);
4083
cb_target = cb_tmp;
4084
}
4085
4086
/* Walk both up, maintaining equal depth, until a join is found. We
4087
* know that we will find one. */
4088
while (!(*env)->IsSameObject(env, cb_value, cb_target)) {
4089
jclass cb_tmp;
4090
cb_tmp = (*env)->GetSuperclass(env, cb_value);
4091
(*env)->DeleteLocalRef(env, cb_value);
4092
cb_value = cb_tmp;
4093
cb_tmp = (*env)->GetSuperclass(env, cb_target);
4094
(*env)->DeleteLocalRef(env, cb_target);
4095
cb_target = cb_tmp;
4096
}
4097
result_info = make_class_info(context, cb_value);
4098
(*env)->DeleteLocalRef(env, cb_value);
4099
(*env)->DeleteLocalRef(env, cb_super_value);
4100
(*env)->DeleteLocalRef(env, cb_target);
4101
(*env)->DeleteLocalRef(env, cb_super_target);
4102
return result_info;
4103
} /* both items are classes */
4104
}
4105
4106
4107
/* Given a fullinfo_type corresponding to an Object, return the jclass
4108
* of that type.
4109
*
4110
* This function always returns a global reference!
4111
*/
4112
4113
static jclass
4114
object_fullinfo_to_classclass(context_type *context, fullinfo_type classinfo)
4115
{
4116
unsigned short info = GET_EXTRA_INFO(classinfo);
4117
return ID_to_class(context, info);
4118
}
4119
4120
static void free_block(void *ptr, int kind)
4121
{
4122
switch (kind) {
4123
case VM_STRING_UTF:
4124
JVM_ReleaseUTF(ptr);
4125
break;
4126
case VM_MALLOC_BLK:
4127
free(ptr);
4128
break;
4129
}
4130
}
4131
4132
static void check_and_push(context_type *context, const void *ptr, int kind)
4133
{
4134
alloc_stack_type *p;
4135
if (ptr == 0)
4136
CCout_of_memory(context);
4137
if (context->alloc_stack_top < ALLOC_STACK_SIZE)
4138
p = &(context->alloc_stack[context->alloc_stack_top++]);
4139
else {
4140
/* Otherwise we have to malloc */
4141
p = malloc(sizeof(alloc_stack_type));
4142
if (p == 0) {
4143
/* Make sure we clean up. */
4144
free_block((void *)ptr, kind);
4145
CCout_of_memory(context);
4146
}
4147
}
4148
p->kind = kind;
4149
p->ptr = (void *)ptr;
4150
p->next = context->allocated_memory;
4151
context->allocated_memory = p;
4152
}
4153
4154
static void pop_and_free(context_type *context)
4155
{
4156
alloc_stack_type *p = context->allocated_memory;
4157
context->allocated_memory = p->next;
4158
free_block(p->ptr, p->kind);
4159
if (p < context->alloc_stack + ALLOC_STACK_SIZE &&
4160
p >= context->alloc_stack)
4161
context->alloc_stack_top--;
4162
else
4163
free(p);
4164
}
4165
4166
static int signature_to_args_size(const char *method_signature)
4167
{
4168
const char *p;
4169
int args_size = 0;
4170
for (p = method_signature; *p != JVM_SIGNATURE_ENDFUNC; p++) {
4171
switch (*p) {
4172
case JVM_SIGNATURE_BOOLEAN:
4173
case JVM_SIGNATURE_BYTE:
4174
case JVM_SIGNATURE_CHAR:
4175
case JVM_SIGNATURE_SHORT:
4176
case JVM_SIGNATURE_INT:
4177
case JVM_SIGNATURE_FLOAT:
4178
args_size += 1;
4179
break;
4180
case JVM_SIGNATURE_CLASS:
4181
args_size += 1;
4182
while (*p != JVM_SIGNATURE_ENDCLASS) p++;
4183
break;
4184
case JVM_SIGNATURE_ARRAY:
4185
args_size += 1;
4186
while ((*p == JVM_SIGNATURE_ARRAY)) p++;
4187
/* If an array of classes, skip over class name, too. */
4188
if (*p == JVM_SIGNATURE_CLASS) {
4189
while (*p != JVM_SIGNATURE_ENDCLASS)
4190
p++;
4191
}
4192
break;
4193
case JVM_SIGNATURE_DOUBLE:
4194
case JVM_SIGNATURE_LONG:
4195
args_size += 2;
4196
break;
4197
case JVM_SIGNATURE_FUNC: /* ignore initial (, if given */
4198
break;
4199
default:
4200
/* Indicate an error. */
4201
return 0;
4202
}
4203
}
4204
return args_size;
4205
}
4206
4207
#ifdef DEBUG
4208
4209
/* Below are for debugging. */
4210
4211
static void print_fullinfo_type(context_type *, fullinfo_type, jboolean);
4212
4213
static void
4214
print_stack(context_type *context, stack_info_type *stack_info)
4215
{
4216
stack_item_type *stack = stack_info->stack;
4217
if (stack_info->stack_size == UNKNOWN_STACK_SIZE) {
4218
jio_fprintf(stdout, "x");
4219
} else {
4220
jio_fprintf(stdout, "(");
4221
for ( ; stack != 0; stack = stack->next)
4222
print_fullinfo_type(context, stack->item,
4223
(jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
4224
jio_fprintf(stdout, ")");
4225
}
4226
}
4227
4228
static void
4229
print_registers(context_type *context, register_info_type *register_info)
4230
{
4231
int register_count = register_info->register_count;
4232
if (register_count == UNKNOWN_REGISTER_COUNT) {
4233
jio_fprintf(stdout, "x");
4234
} else {
4235
fullinfo_type *registers = register_info->registers;
4236
int mask_count = register_info->mask_count;
4237
mask_type *masks = register_info->masks;
4238
int i, j;
4239
4240
jio_fprintf(stdout, "{");
4241
for (i = 0; i < register_count; i++)
4242
print_fullinfo_type(context, registers[i],
4243
(jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
4244
jio_fprintf(stdout, "}");
4245
for (i = 0; i < mask_count; i++) {
4246
char *separator = "";
4247
int *modifies = masks[i].modifies;
4248
jio_fprintf(stdout, "<%d: ", masks[i].entry);
4249
for (j = 0;
4250
j < JVM_GetMethodIxLocalsCount(context->env,
4251
context->class,
4252
context->method_index);
4253
j++)
4254
if (IS_BIT_SET(modifies, j)) {
4255
jio_fprintf(stdout, "%s%d", separator, j);
4256
separator = ",";
4257
}
4258
jio_fprintf(stdout, ">");
4259
}
4260
}
4261
}
4262
4263
4264
static void
4265
print_flags(context_type *context, flag_type and_flags, flag_type or_flags)
4266
{
4267
if (and_flags != ((flag_type)-1) || or_flags != 0) {
4268
jio_fprintf(stdout, "<%x %x>", and_flags, or_flags);
4269
}
4270
}
4271
4272
static void
4273
print_fullinfo_type(context_type *context, fullinfo_type type, jboolean verbose)
4274
{
4275
int i;
4276
int indirection = GET_INDIRECTION(type);
4277
for (i = indirection; i-- > 0; )
4278
jio_fprintf(stdout, "[");
4279
switch (GET_ITEM_TYPE(type)) {
4280
case ITEM_Integer:
4281
jio_fprintf(stdout, "I"); break;
4282
case ITEM_Float:
4283
jio_fprintf(stdout, "F"); break;
4284
case ITEM_Double:
4285
jio_fprintf(stdout, "D"); break;
4286
case ITEM_Double_2:
4287
jio_fprintf(stdout, "d"); break;
4288
case ITEM_Long:
4289
jio_fprintf(stdout, "L"); break;
4290
case ITEM_Long_2:
4291
jio_fprintf(stdout, "l"); break;
4292
case ITEM_ReturnAddress:
4293
jio_fprintf(stdout, "a"); break;
4294
case ITEM_Object:
4295
if (!verbose) {
4296
jio_fprintf(stdout, "A");
4297
} else {
4298
unsigned short extra = GET_EXTRA_INFO(type);
4299
if (extra == 0) {
4300
jio_fprintf(stdout, "/Null/");
4301
} else {
4302
const char *name = ID_to_class_name(context, extra);
4303
const char *name2 = strrchr(name, '/');
4304
jio_fprintf(stdout, "/%s/", name2 ? name2 + 1 : name);
4305
}
4306
}
4307
break;
4308
case ITEM_Char:
4309
jio_fprintf(stdout, "C"); break;
4310
case ITEM_Short:
4311
jio_fprintf(stdout, "S"); break;
4312
case ITEM_Byte:
4313
jio_fprintf(stdout, "B"); break;
4314
case ITEM_NewObject:
4315
if (!verbose) {
4316
jio_fprintf(stdout, "@");
4317
} else {
4318
int inum = GET_EXTRA_INFO(type);
4319
fullinfo_type real_type =
4320
context->instruction_data[inum].operand2.fi;
4321
jio_fprintf(stdout, ">");
4322
print_fullinfo_type(context, real_type, JNI_TRUE);
4323
jio_fprintf(stdout, "<");
4324
}
4325
break;
4326
case ITEM_InitObject:
4327
jio_fprintf(stdout, verbose ? ">/this/<" : "@");
4328
break;
4329
4330
default:
4331
jio_fprintf(stdout, "?"); break;
4332
}
4333
for (i = indirection; i-- > 0; )
4334
jio_fprintf(stdout, "]");
4335
}
4336
4337
4338
static void
4339
print_formatted_fieldname(context_type *context, int index)
4340
{
4341
JNIEnv *env = context->env;
4342
jclass cb = context->class;
4343
const char *classname = JVM_GetCPFieldClassNameUTF(env, cb, index);
4344
const char *fieldname = JVM_GetCPFieldNameUTF(env, cb, index);
4345
jio_fprintf(stdout, " <%s.%s>",
4346
classname ? classname : "", fieldname ? fieldname : "");
4347
JVM_ReleaseUTF(classname);
4348
JVM_ReleaseUTF(fieldname);
4349
}
4350
4351
static void
4352
print_formatted_methodname(context_type *context, int index)
4353
{
4354
JNIEnv *env = context->env;
4355
jclass cb = context->class;
4356
const char *classname = JVM_GetCPMethodClassNameUTF(env, cb, index);
4357
const char *methodname = JVM_GetCPMethodNameUTF(env, cb, index);
4358
jio_fprintf(stdout, " <%s.%s>",
4359
classname ? classname : "", methodname ? methodname : "");
4360
JVM_ReleaseUTF(classname);
4361
JVM_ReleaseUTF(methodname);
4362
}
4363
4364
#endif /*DEBUG*/
4365
4366