This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
/
check_code.c
4404 lines (3954 loc) · 164 KB
/
check_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*-
* Verify that the code within a method block doesn't exploit any
* security holes.
*/
/*
Exported function:
jboolean
VerifyClass(JNIEnv *env, jclass cb, char *message_buffer,
jint buffer_length)
jboolean
VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *message_buffer,
jint buffer_length, jint major_version)
This file now only uses the standard JNI and the following VM functions
exported in jvm.h:
JVM_FindClassFromClass
JVM_IsInterface
JVM_GetClassNameUTF
JVM_GetClassCPEntriesCount
JVM_GetClassCPTypes
JVM_GetClassFieldsCount
JVM_GetClassMethodsCount
JVM_GetFieldIxModifiers
JVM_GetMethodIxModifiers
JVM_GetMethodIxExceptionTableLength
JVM_GetMethodIxLocalsCount
JVM_GetMethodIxArgsSize
JVM_GetMethodIxMaxStack
JVM_GetMethodIxNameUTF
JVM_GetMethodIxSignatureUTF
JVM_GetMethodIxExceptionsCount
JVM_GetMethodIxExceptionIndexes
JVM_GetMethodIxByteCodeLength
JVM_GetMethodIxByteCode
JVM_GetMethodIxExceptionTableEntry
JVM_IsConstructorIx
JVM_GetCPClassNameUTF
JVM_GetCPFieldNameUTF
JVM_GetCPMethodNameUTF
JVM_GetCPFieldSignatureUTF
JVM_GetCPMethodSignatureUTF
JVM_GetCPFieldClassNameUTF
JVM_GetCPMethodClassNameUTF
JVM_GetCPFieldModifiers
JVM_GetCPMethodModifiers
JVM_ReleaseUTF
JVM_IsSameClassPackage
*/
#include <string.h>
#include <setjmp.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
#include "classfile_constants.h"
#include "opcodes.in_out"
/* On AIX malloc(0) and calloc(0, ...) return a NULL pointer, which is legal,
* but the code here does not handles it. So we wrap the methods and return non-NULL
* pointers even if we allocate 0 bytes.
*/
#ifdef _AIX
static int aix_dummy;
static void* aix_malloc(size_t len) {
if (len == 0) {
return &aix_dummy;
}
return malloc(len);
}
static void* aix_calloc(size_t n, size_t size) {
if (n == 0) {
return &aix_dummy;
}
return calloc(n, size);
}
static void aix_free(void* p) {
if (p == &aix_dummy) {
return;
}
free(p);
}
#undef malloc
#undef calloc
#undef free
#define malloc aix_malloc
#define calloc aix_calloc
#define free aix_free
#endif
#ifdef __APPLE__
/* use setjmp/longjmp versions that do not save/restore the signal mask */
#define setjmp _setjmp
#define longjmp _longjmp
#endif
#define MAX_ARRAY_DIMENSIONS 255
/* align byte code */
#ifndef ALIGN_UP
#define ALIGN_UP(n,align_grain) (((n) + ((align_grain) - 1)) & ~((align_grain)-1))
#endif /* ALIGN_UP */
#define UCALIGN(n) ((unsigned char *)ALIGN_UP((uintptr_t)(n),sizeof(int)))
#ifdef DEBUG
int verify_verbose = 0;
static struct context_type *GlobalContext;
#endif
enum {
ITEM_Bogus,
ITEM_Void, /* only as a function return value */
ITEM_Integer,
ITEM_Float,
ITEM_Double,
ITEM_Double_2, /* 2nd word of double in register */
ITEM_Long,
ITEM_Long_2, /* 2nd word of long in register */
ITEM_Array,
ITEM_Object, /* Extra info field gives name. */
ITEM_NewObject, /* Like object, but uninitialized. */
ITEM_InitObject, /* "this" is init method, before call
to super() */
ITEM_ReturnAddress, /* Extra info gives instr # of start pc */
/* The following four are only used within array types.
* Normally, we use ITEM_Integer, instead. */
ITEM_Byte,
ITEM_Short,
ITEM_Char,
ITEM_Boolean
};
#define UNKNOWN_STACK_SIZE -1
#define UNKNOWN_REGISTER_COUNT -1
#define UNKNOWN_RET_INSTRUCTION -1
#undef MAX
#undef MIN
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define BITS_PER_INT (CHAR_BIT * sizeof(int)/sizeof(char))
#define SET_BIT(flags, i) (flags[(i)/BITS_PER_INT] |= \
((unsigned)1 << ((i) % BITS_PER_INT)))
#define IS_BIT_SET(flags, i) (flags[(i)/BITS_PER_INT] & \
((unsigned)1 << ((i) % BITS_PER_INT)))
typedef unsigned int fullinfo_type;
typedef unsigned int *bitvector;
#define GET_ITEM_TYPE(thing) ((thing) & 0x1F)
#define GET_INDIRECTION(thing) (((thing) & 0xFFFF) >> 5)
#define GET_EXTRA_INFO(thing) ((thing) >> 16)
#define WITH_ZERO_INDIRECTION(thing) ((thing) & ~(0xFFE0))
#define WITH_ZERO_EXTRA_INFO(thing) ((thing) & 0xFFFF)
#define MAKE_FULLINFO(type, indirect, extra) \
((type) + ((indirect) << 5) + ((extra) << 16))
#define MAKE_Object_ARRAY(indirect) \
(context->object_info + ((indirect) << 5))
#define NULL_FULLINFO MAKE_FULLINFO(ITEM_Object, 0, 0)
/* JVM_OPC_invokespecial calls to <init> need to be treated special */
#define JVM_OPC_invokeinit 0x100
/* A hash mechanism used by the verifier.
* Maps class names to unique 16 bit integers.
*/
#define HASH_TABLE_SIZE 503
/* The buckets are managed as a 256 by 256 matrix. We allocate an entire
* row (256 buckets) at a time to minimize fragmentation. Rows are
* allocated on demand so that we don't waste too much space.
*/
#define MAX_HASH_ENTRIES 65536
#define HASH_ROW_SIZE 256
typedef struct hash_bucket_type {
char *name;
unsigned int hash;
jclass class;
unsigned short ID;
unsigned short next;
unsigned loadable:1; /* from context->class loader */
} hash_bucket_type;
typedef struct {
hash_bucket_type **buckets;
unsigned short *table;
int entries_used;
} hash_table_type;
#define GET_BUCKET(class_hash, ID)\
(class_hash->buckets[ID / HASH_ROW_SIZE] + ID % HASH_ROW_SIZE)
/*
* There are currently two types of resources that we need to keep
* track of (in addition to the CCalloc pool).
*/
enum {
VM_STRING_UTF, /* VM-allocated UTF strings */
VM_MALLOC_BLK /* malloc'ed blocks */
};
#define LDC_CLASS_MAJOR_VERSION 49
#define LDC_METHOD_HANDLE_MAJOR_VERSION 51
#define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION 51
#define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION 52
#define ALLOC_STACK_SIZE 16 /* big enough */
typedef struct alloc_stack_type {
void *ptr;
int kind;
struct alloc_stack_type *next;
} alloc_stack_type;
/* The context type encapsulates the current invocation of the byte
* code verifier.
*/
struct context_type {
JNIEnv *env; /* current JNIEnv */
/* buffers etc. */
char *message;
jint message_buf_len;
jboolean err_code;
alloc_stack_type *allocated_memory; /* all memory blocks that we have not
had a chance to free */
/* Store up to ALLOC_STACK_SIZE number of handles to allocated memory
blocks here, to save mallocs. */
alloc_stack_type alloc_stack[ALLOC_STACK_SIZE];
int alloc_stack_top;
/* these fields are per class */
jclass class; /* current class */
jint major_version;
jint nconstants;
unsigned char *constant_types;
hash_table_type class_hash;
fullinfo_type object_info; /* fullinfo for java/lang/Object */
fullinfo_type string_info; /* fullinfo for java/lang/String */
fullinfo_type throwable_info; /* fullinfo for java/lang/Throwable */
fullinfo_type cloneable_info; /* fullinfo for java/lang/Cloneable */
fullinfo_type serializable_info; /* fullinfo for java/io/Serializable */
fullinfo_type currentclass_info; /* fullinfo for context->class */
fullinfo_type superclass_info; /* fullinfo for superclass */
/* these fields are per method */
int method_index; /* current method */
unsigned short *exceptions; /* exceptions */
unsigned char *code; /* current code object */
jint code_length;
int *code_data; /* offset to instruction number */
struct instruction_data_type *instruction_data; /* info about each */
struct handler_info_type *handler_info;
fullinfo_type *superclasses; /* null terminated superclasses */
int instruction_count; /* number of instructions */
fullinfo_type return_type; /* function return type */
fullinfo_type swap_table[4]; /* used for passing information */
int bitmask_size; /* words needed to hold bitmap of arguments */
/* these fields are per field */
int field_index;
/* Used by the space allocator */
struct CCpool *CCroot, *CCcurrent;
char *CCfree_ptr;
int CCfree_size;
/* Jump here on any error. */
jmp_buf jump_buffer;
#ifdef DEBUG
/* keep track of how many global refs are allocated. */
int n_globalrefs;
#endif
};
struct stack_info_type {
struct stack_item_type *stack;
int stack_size;
};
struct register_info_type {
int register_count; /* number of registers used */
fullinfo_type *registers;
int mask_count; /* number of masks in the following */
struct mask_type *masks;
};
struct mask_type {
int entry;
int *modifies;
};
typedef unsigned short flag_type;
struct instruction_data_type {
int opcode; /* may turn into "canonical" opcode */
unsigned changed:1; /* has it changed */
unsigned protected:1; /* must accessor be a subclass of "this" */
union {
int i; /* operand to the opcode */
int *ip;
fullinfo_type fi;
} operand, operand2;
fullinfo_type p;
struct stack_info_type stack_info;
struct register_info_type register_info;
#define FLAG_REACHED 0x01 /* instruction reached */
#define FLAG_NEED_CONSTRUCTOR 0x02 /* must call this.<init> or super.<init> */
#define FLAG_NO_RETURN 0x04 /* must throw out of method */
flag_type or_flags; /* true for at least one path to this inst */
#define FLAG_CONSTRUCTED 0x01 /* this.<init> or super.<init> called */
flag_type and_flags; /* true for all paths to this instruction */
};
struct handler_info_type {
int start, end, handler;
struct stack_info_type stack_info;
};
struct stack_item_type {
fullinfo_type item;
struct stack_item_type *next;
};
typedef struct context_type context_type;
typedef struct instruction_data_type instruction_data_type;
typedef struct stack_item_type stack_item_type;
typedef struct register_info_type register_info_type;
typedef struct stack_info_type stack_info_type;
typedef struct mask_type mask_type;
static void read_all_code(context_type *context, jclass cb, int num_methods,
int** code_lengths, unsigned char*** code);
static void verify_method(context_type *context, jclass cb, int index,
int code_length, unsigned char* code);
static void free_all_code(context_type* context, int num_methods,
unsigned char** code);
static void verify_field(context_type *context, jclass cb, int index);
static void verify_opcode_operands (context_type *, unsigned int inumber, int offset);
static void set_protected(context_type *, unsigned int inumber, int key, int);
static jboolean is_superclass(context_type *, fullinfo_type);
static void initialize_exception_table(context_type *);
static int instruction_length(unsigned char *iptr, unsigned char *end);
static jboolean isLegalTarget(context_type *, int offset);
static void verify_constant_pool_type(context_type *, int, unsigned);
static void initialize_dataflow(context_type *);
static void run_dataflow(context_type *context);
static void check_register_values(context_type *context, unsigned int inumber);
static void check_flags(context_type *context, unsigned int inumber);
static void pop_stack(context_type *, unsigned int inumber, stack_info_type *);
static void update_registers(context_type *, unsigned int inumber, register_info_type *);
static void update_flags(context_type *, unsigned int inumber,
flag_type *new_and_flags, flag_type *new_or_flags);
static void push_stack(context_type *, unsigned int inumber, stack_info_type *stack);
static void merge_into_successors(context_type *, unsigned int inumber,
register_info_type *register_info,
stack_info_type *stack_info,
flag_type and_flags, flag_type or_flags);
static void merge_into_one_successor(context_type *context,
unsigned int from_inumber,
unsigned int inumber,
register_info_type *register_info,
stack_info_type *stack_info,
flag_type and_flags, flag_type or_flags,
jboolean isException);
static void merge_stack(context_type *, unsigned int inumber,
unsigned int to_inumber, stack_info_type *);
static void merge_registers(context_type *, unsigned int inumber,
unsigned int to_inumber,
register_info_type *);
static void merge_flags(context_type *context, unsigned int from_inumber,
unsigned int to_inumber,
flag_type new_and_flags, flag_type new_or_flags);
static stack_item_type *copy_stack(context_type *, stack_item_type *);
static mask_type *copy_masks(context_type *, mask_type *masks, int mask_count);
static mask_type *add_to_masks(context_type *, mask_type *, int , int);
static fullinfo_type decrement_indirection(fullinfo_type);
static fullinfo_type merge_fullinfo_types(context_type *context,
fullinfo_type a,
fullinfo_type b,
jboolean assignment);
static jboolean isAssignableTo(context_type *,
fullinfo_type a,
fullinfo_type b);
static jclass object_fullinfo_to_classclass(context_type *, fullinfo_type);
#define NEW(type, count) \
((type *)CCalloc(context, (count)*(sizeof(type)), JNI_FALSE))
#define ZNEW(type, count) \
((type *)CCalloc(context, (count)*(sizeof(type)), JNI_TRUE))
static void CCinit(context_type *context);
static void CCreinit(context_type *context);
static void CCdestroy(context_type *context);
static void *CCalloc(context_type *context, int size, jboolean zero);
static fullinfo_type cp_index_to_class_fullinfo(context_type *, int, int);
static const char* get_result_signature(const char* signature);
static char signature_to_fieldtype(context_type *context,
const char **signature_p, fullinfo_type *info);
static void CCerror (context_type *, char *format, ...);
static void CFerror (context_type *, char *format, ...);
static void CCout_of_memory (context_type *);
/* Because we can longjmp any time, we need to be very careful about
* remembering what needs to be freed. */
static void check_and_push_malloc_block(context_type *context, void *ptr);
static void check_and_push_string_utf(context_type *context, const char *ptr);
static void pop_and_free(context_type *context);
static int signature_to_args_size(const char *method_signature);
#ifdef DEBUG
static void print_stack (context_type *, stack_info_type *stack_info);
static void print_registers(context_type *, register_info_type *register_info);
static void print_flags(context_type *, flag_type, flag_type);
static void print_formatted_fieldname(context_type *context, int index);
static void print_formatted_methodname(context_type *context, int index);
#endif
/*
* Declare library specific JNI_Onload entry if static build
*/
DEF_STATIC_JNI_OnLoad
void initialize_class_hash(context_type *context)
{
hash_table_type *class_hash = &(context->class_hash);
class_hash->buckets = (hash_bucket_type **)
calloc(MAX_HASH_ENTRIES / HASH_ROW_SIZE, sizeof(hash_bucket_type *));
class_hash->table = (unsigned short *)
calloc(HASH_TABLE_SIZE, sizeof(unsigned short));
if (class_hash->buckets == 0 ||
class_hash->table == 0)
CCout_of_memory(context);
class_hash->entries_used = 0;
}
static void finalize_class_hash(context_type *context)
{
hash_table_type *class_hash = &(context->class_hash);
JNIEnv *env = context->env;
int i;
/* 4296677: bucket index starts from 1. */
for (i=1;i<=class_hash->entries_used;i++) {
hash_bucket_type *bucket = GET_BUCKET(class_hash, i);
assert(bucket != NULL);
free(bucket->name);
if (bucket->class) {
(*env)->DeleteGlobalRef(env, bucket->class);
#ifdef DEBUG
context->n_globalrefs--;
#endif
}
}
if (class_hash->buckets) {
for (i=0;i<MAX_HASH_ENTRIES / HASH_ROW_SIZE; i++) {
if (class_hash->buckets[i] == 0)
break;
free(class_hash->buckets[i]);
}
}
free(class_hash->buckets);
free(class_hash->table);
}
static hash_bucket_type *
new_bucket(context_type *context, unsigned short *pID)
{
hash_table_type *class_hash = &(context->class_hash);
int i = *pID = class_hash->entries_used + 1;
int row = i / HASH_ROW_SIZE;
if (i >= MAX_HASH_ENTRIES)
CCerror(context, "Exceeded verifier's limit of 65535 referred classes");
if (class_hash->buckets[row] == 0) {
class_hash->buckets[row] = (hash_bucket_type*)
calloc(HASH_ROW_SIZE, sizeof(hash_bucket_type));
if (class_hash->buckets[row] == 0)
CCout_of_memory(context);
}
class_hash->entries_used++; /* only increment when we are sure there
is no overflow. */
return GET_BUCKET(class_hash, i);
}
static unsigned int
class_hash_fun(const char *s)
{
int i;
unsigned raw_hash;
for (raw_hash = 0; (i = *s) != '\0'; ++s)
raw_hash = raw_hash * 37 + i;
return raw_hash;
}
/*
* Find a class using the defining loader of the current class
* and return a local reference to it.
*/
static jclass load_class_local(context_type *context,const char *classname)
{
jclass cb = JVM_FindClassFromClass(context->env, classname,
JNI_FALSE, context->class);
if (cb == 0)
CCerror(context, "Cannot find class %s", classname);
return cb;
}
/*
* Find a class using the defining loader of the current class
* and return a global reference to it.
*/
static jclass load_class_global(context_type *context, const char *classname)
{
JNIEnv *env = context->env;
jclass local, global;
local = load_class_local(context, classname);
global = (*env)->NewGlobalRef(env, local);
if (global == 0)
CCout_of_memory(context);
#ifdef DEBUG
context->n_globalrefs++;
#endif
(*env)->DeleteLocalRef(env, local);
return global;
}
/*
* Return a unique ID given a local class reference. The loadable
* flag is true if the defining class loader of context->class
* is known to be capable of loading the class.
*/
static unsigned short
class_to_ID(context_type *context, jclass cb, jboolean loadable)
{
JNIEnv *env = context->env;
hash_table_type *class_hash = &(context->class_hash);
unsigned int hash;
hash_bucket_type *bucket;
unsigned short *pID;
const char *name = JVM_GetClassNameUTF(env, cb);
check_and_push_string_utf(context, name);
hash = class_hash_fun(name);
pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
while (*pID) {
bucket = GET_BUCKET(class_hash, *pID);
if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
/*
* There is an unresolved entry with our name
* so we're forced to load it in case it matches us.
*/
if (bucket->class == 0) {
assert(bucket->loadable == JNI_TRUE);
bucket->class = load_class_global(context, name);
}
/*
* It's already in the table. Update the loadable
* state if it's known and then we're done.
*/
if ((*env)->IsSameObject(env, cb, bucket->class)) {
if (loadable && !bucket->loadable)
bucket->loadable = JNI_TRUE;
goto done;
}
}
pID = &bucket->next;
}
bucket = new_bucket(context, pID);
bucket->next = 0;
bucket->hash = hash;
bucket->name = malloc(strlen(name) + 1);
if (bucket->name == 0)
CCout_of_memory(context);
strcpy(bucket->name, name);
bucket->loadable = loadable;
bucket->class = (*env)->NewGlobalRef(env, cb);
if (bucket->class == 0)
CCout_of_memory(context);
#ifdef DEBUG
context->n_globalrefs++;
#endif
done:
pop_and_free(context);
return *pID;
}
/*
* Return a unique ID given a class name from the constant pool.
* All classes are lazily loaded from the defining loader of
* context->class.
*/
static unsigned short
class_name_to_ID(context_type *context, const char *name)
{
hash_table_type *class_hash = &(context->class_hash);
unsigned int hash = class_hash_fun(name);
hash_bucket_type *bucket;
unsigned short *pID;
jboolean force_load = JNI_FALSE;
pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
while (*pID) {
bucket = GET_BUCKET(class_hash, *pID);
if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
if (bucket->loadable)
goto done;
force_load = JNI_TRUE;
}
pID = &bucket->next;
}
if (force_load) {
/*
* We found at least one matching named entry for a class that
* was not known to be loadable through the defining class loader
* of context->class. We must load our named class and update
* the hash table in case one these entries matches our class.
*/
JNIEnv *env = context->env;
jclass cb = load_class_local(context, name);
unsigned short id = class_to_ID(context, cb, JNI_TRUE);
(*env)->DeleteLocalRef(env, cb);
return id;
}
bucket = new_bucket(context, pID);
bucket->next = 0;
bucket->class = 0;
bucket->loadable = JNI_TRUE; /* name-only IDs are implicitly loadable */
bucket->hash = hash;
bucket->name = malloc(strlen(name) + 1);
if (bucket->name == 0)
CCout_of_memory(context);
strcpy(bucket->name, name);
done:
return *pID;
}
#ifdef DEBUG
static const char *
ID_to_class_name(context_type *context, unsigned short ID)
{
hash_table_type *class_hash = &(context->class_hash);
hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
return bucket->name;
}
#endif
static jclass
ID_to_class(context_type *context, unsigned short ID)
{
hash_table_type *class_hash = &(context->class_hash);
hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
if (bucket->class == 0) {
assert(bucket->loadable == JNI_TRUE);
bucket->class = load_class_global(context, bucket->name);
}
return bucket->class;
}
static fullinfo_type
make_loadable_class_info(context_type *context, jclass cb)
{
return MAKE_FULLINFO(ITEM_Object, 0,
class_to_ID(context, cb, JNI_TRUE));
}
static fullinfo_type
make_class_info(context_type *context, jclass cb)
{
return MAKE_FULLINFO(ITEM_Object, 0,
class_to_ID(context, cb, JNI_FALSE));
}
static fullinfo_type
make_class_info_from_name(context_type *context, const char *name)
{
return MAKE_FULLINFO(ITEM_Object, 0,
class_name_to_ID(context, name));
}
/* RETURNS
* 1: on success chosen to be consistent with previous VerifyClass
* 0: verify error
* 2: out of memory
* 3: class format error
*
* Called by verify_class. Verify the code of each of the methods
* in a class. Note that this function apparently can't be JNICALL,
* because if it is the dynamic linker doesn't appear to be able to
* find it on Win32.
*/
#define CC_OK 1
#define CC_VerifyError 0
#define CC_OutOfMemory 2
#define CC_ClassFormatError 3
JNIEXPORT jboolean
VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *buffer, jint len,
jint major_version)
{
context_type context_structure;
context_type *context = &context_structure;
jboolean result = CC_OK;
int i;
int num_methods;
int* code_lengths;
unsigned char** code;
#ifdef DEBUG
GlobalContext = context;
#endif
memset(context, 0, sizeof(context_type));
context->message = buffer;
context->message_buf_len = len;
context->env = env;
context->class = cb;
/* Set invalid method/field index of the context, in case anyone
calls CCerror */
context->method_index = -1;
context->field_index = -1;
/* Don't call CCerror or anything that can call it above the setjmp! */
if (!setjmp(context->jump_buffer)) {
jclass super;
CCinit(context); /* initialize heap; may throw */
initialize_class_hash(context);
context->major_version = major_version;
context->nconstants = JVM_GetClassCPEntriesCount(env, cb);
context->constant_types = (unsigned char *)
malloc(sizeof(unsigned char) * context->nconstants + 1);
if (context->constant_types == 0)
CCout_of_memory(context);
JVM_GetClassCPTypes(env, cb, context->constant_types);
if (context->constant_types == 0)
CCout_of_memory(context);
context->object_info =
make_class_info_from_name(context, "java/lang/Object");
context->string_info =
make_class_info_from_name(context, "java/lang/String");
context->throwable_info =
make_class_info_from_name(context, "java/lang/Throwable");
context->cloneable_info =
make_class_info_from_name(context, "java/lang/Cloneable");
context->serializable_info =
make_class_info_from_name(context, "java/io/Serializable");
context->currentclass_info = make_loadable_class_info(context, cb);
super = (*env)->GetSuperclass(env, cb);
if (super != 0) {
fullinfo_type *gptr;
int i = 0;
context->superclass_info = make_loadable_class_info(context, super);
while(super != 0) {
jclass tmp_cb = (*env)->GetSuperclass(env, super);
(*env)->DeleteLocalRef(env, super);
super = tmp_cb;
i++;
}
(*env)->DeleteLocalRef(env, super);
super = 0;
/* Can't go on context heap since it survives more than
one method */
context->superclasses = gptr =
malloc(sizeof(fullinfo_type)*(i + 1));
if (gptr == 0) {
CCout_of_memory(context);
}
super = (*env)->GetSuperclass(env, context->class);
while(super != 0) {
jclass tmp_cb;
*gptr++ = make_class_info(context, super);
tmp_cb = (*env)->GetSuperclass(env, super);
(*env)->DeleteLocalRef(env, super);
super = tmp_cb;
}
*gptr = 0;
} else {
context->superclass_info = 0;
}
(*env)->DeleteLocalRef(env, super);
/* Look at each method */
for (i = JVM_GetClassFieldsCount(env, cb); --i >= 0;)
verify_field(context, cb, i);
num_methods = JVM_GetClassMethodsCount(env, cb);
read_all_code(context, cb, num_methods, &code_lengths, &code);
for (i = num_methods - 1; i >= 0; --i)
verify_method(context, cb, i, code_lengths[i], code[i]);
free_all_code(context, num_methods, code);
result = CC_OK;
} else {
result = context->err_code;
}
/* Cleanup */
finalize_class_hash(context);
while(context->allocated_memory)
pop_and_free(context);
#ifdef DEBUG
GlobalContext = 0;
#endif
if (context->exceptions)
free(context->exceptions);
if (context->constant_types)
free(context->constant_types);
if (context->superclasses)
free(context->superclasses);
#ifdef DEBUG
/* Make sure all global refs created in the verifier are freed */
assert(context->n_globalrefs == 0);
#endif
CCdestroy(context); /* destroy heap */
return result;
}
#define OLD_FORMAT_MAX_MAJOR_VERSION 48
JNIEXPORT jboolean
VerifyClass(JNIEnv *env, jclass cb, char *buffer, jint len)
{
static int warned = 0;
if (!warned) {
jio_fprintf(stdout, "Warning! An old version of jvm is used. This is not supported.\n");
warned = 1;
}
return VerifyClassForMajorVersion(env, cb, buffer, len,
OLD_FORMAT_MAX_MAJOR_VERSION);
}
static void
verify_field(context_type *context, jclass cb, int field_index)
{
JNIEnv *env = context->env;
int access_bits = JVM_GetFieldIxModifiers(env, cb, field_index);
context->field_index = field_index;
if ( ((access_bits & JVM_ACC_PUBLIC) != 0) &&
((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
CCerror(context, "Inconsistent access bits.");
}
context->field_index = -1;
}
/**
* We read all of the class's methods' code because it is possible that
* the verification of one method could resulting in linking further
* down the stack (due to class loading), which could end up rewriting
* some of the bytecode of methods we haven't verified yet. Since we
* don't want to see the rewritten bytecode, cache all the code and
* operate only on that.
*/
static void
read_all_code(context_type* context, jclass cb, int num_methods,
int** lengths_addr, unsigned char*** code_addr)
{
int* lengths;
unsigned char** code;
int i;
lengths = malloc(sizeof(int) * num_methods);
check_and_push_malloc_block(context, lengths);
code = malloc(sizeof(unsigned char*) * num_methods);
check_and_push_malloc_block(context, code);
*(lengths_addr) = lengths;
*(code_addr) = code;
for (i = 0; i < num_methods; ++i) {
lengths[i] = JVM_GetMethodIxByteCodeLength(context->env, cb, i);
if (lengths[i] > 0) {
code[i] = malloc(sizeof(unsigned char) * (lengths[i] + 1));
check_and_push_malloc_block(context, code[i]);
JVM_GetMethodIxByteCode(context->env, cb, i, code[i]);
} else {
code[i] = NULL;
}
}
}
static void
free_all_code(context_type* context, int num_methods, unsigned char** code)
{
int i;
for (i = 0; i < num_methods; ++i) {
if (code[i] != NULL) {
pop_and_free(context);
}
}
pop_and_free(context); /* code */
pop_and_free(context); /* lengths */
}
/* Verify the code of one method */
static void
verify_method(context_type *context, jclass cb, int method_index,
int code_length, unsigned char* code)
{
JNIEnv *env = context->env;
int access_bits = JVM_GetMethodIxModifiers(env, cb, method_index);
int *code_data;