forked from rednaxelafx/ajvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classloader.c
1772 lines (1468 loc) · 53.4 KB
/
classloader.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
/*
* classreader.c - jvm class file parser.
*
* (c) wzt 2012, 2013 http://www.cloud-sec.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#include "jvm.h"
#include "type.h"
#include "list.h"
#include "log.h"
#include "vm_error.h"
static int class_fd;
static int class_file_len;
static void *class_start_mem;
static char *p_mem;
int mmap_class_file(const char *class_file)
{
struct stat f_stat;
class_fd = open(class_file, O_RDONLY);
if (class_fd == -1) {
__error("open %s failed.", class_file);
return -1;
}
if (stat(class_file, &f_stat) == -1) {
__error("stat failed.");
close(class_fd);
return -1;
}
class_file_len = f_stat.st_size;
__debug2("%s file len: %d\n", class_file, class_file_len);
class_start_mem = mmap(NULL, class_file_len, PROT_READ, MAP_PRIVATE,
class_fd, 0);
if (!class_start_mem) {
__error("mmap failed.");
close(class_fd);
return -1;
}
__debug2("mmap %s at %p\n", class_file, class_start_mem);
return 0;
}
int mmap_exit(void)
{
if (munmap(class_start_mem, class_file_len) == -1) {
__error("munmap failed.");
return -1;
}
close(class_fd);
return 0;
}
void show_class_info(char *fmt, ...)
{
va_list arg;
char buf[256];
va_start(arg, fmt);
vsprintf(buf, fmt, arg);
va_end(arg);
if (jvm_arg->print_class) {
printf("%s", buf);
}
else {
__debug2("%s", buf);
}
}
int parse_class_magic(CLASS *jvm_class)
{
/* read class magic number. */
CLASS_READ_U4(jvm_class->class_magic, p_mem)
show_class_info("magic: 0x%x\n", jvm_class->class_magic);
if (jvm_class->class_magic != JVM_CLASS_MAGIC) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM class magic not match.\n");
return -1;
}
show_class_info("jvm class magic match: 0x%x\n", jvm_class->class_magic);
return 0;
}
int parse_class_version(CLASS *jvm_class)
{
/* read class minor_version. */
CLASS_READ_U2(jvm_class->minor_version, p_mem)
show_class_info("jvm class minor_version: %d\n", jvm_class->minor_version);
/* read class major_version. */
CLASS_READ_U2(jvm_class->major_version, p_mem)
show_class_info("jvm class major_version: %d\n", jvm_class->major_version);
if (jvm_class->major_version < 45 || jvm_class->major_version > 50) {
char err_buf[128];
snprintf(err_buf, 1024, "JVM version error: %d.%d\n",
jvm_class->major_version, jvm_class->minor_version);
jvm_error(VM_ERROR_CLASS_FILE, err_buf);
return -1;
}
return 0;
}
int handle_class_info(CLASS *jvm_class, u2 constant_pool_count, u2 idx, u1 tag)
{
struct CONSTANT_Class_info *class_info;
class_info = (struct CONSTANT_Class_info *)
malloc(sizeof(struct CONSTANT_Class_info));
if (!class_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U2(class_info->name_index, p_mem);
if (class_info->name_index < 1 || class_info->name_index >= constant_pool_count) {
char err_buf[128];
snprintf(err_buf, 128, "JVM class_info->name_index error: %d\n",
class_info->name_index);
jvm_error(VM_ERROR_CLASS_FILE, err_buf);
return -1;
}
show_class_info("name_index: %d\n", class_info->name_index);
class_info->base = jvm_class->constant_info[class_info->name_index].base;
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].index = tag;
jvm_class->constant_info[idx].base = (u1 *)class_info;
return 0;
}
int handle_class_InvokeDynamic(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_InvokeDynamic_info *invoke_dyc_info;
invoke_dyc_info = (struct CONSTANT_InvokeDynamic_info *)
malloc(sizeof(struct CONSTANT_InvokeDynamic_info));
if (!invoke_dyc_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U2(invoke_dyc_info->bootstrap_method_attr_index, p_mem);
CLASS_READ_U2(invoke_dyc_info->name_and_type_index, p_mem);
if (invoke_dyc_info->bootstrap_method_attr_index < 1 ||
invoke_dyc_info->bootstrap_method_attr_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM bootstrap_method_attr_index error.");
free(invoke_dyc_info);
return -1;
}
if (invoke_dyc_info->name_and_type_index < 1 ||
invoke_dyc_info->name_and_type_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM name_and_type_index error.");
free(invoke_dyc_info);
return -1;
}
show_class_info("bootstrap_method_attr_index: %d, name_and_type_index: %d\n",
invoke_dyc_info->bootstrap_method_attr_index,
invoke_dyc_info->name_and_type_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)invoke_dyc_info;
return 0;
}
int handle_class_utf8(CLASS *jvm_class, u2 idx, u1 tag)
{
u2 len;
u1 *buf;
CLASS_READ_U2(len, p_mem);
buf = (u1 *)malloc(len + 1);
if (!buf) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.\n");
return -1;
}
memset(buf, '\0', len + 1);
memcpy(buf, p_mem, len);
show_class_info("len: %d\t%s\n", len, buf);
p_mem += len;
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = buf;
return 0;
}
int handle_class_method_type(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_MethodType_info *method_type_info;
method_type_info = (struct CONSTANT_MethodType_info *)
malloc(sizeof(struct CONSTANT_MethodType_info));
if (!method_type_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U2(method_type_info->descriptor_index, p_mem);
if (method_type_info->descriptor_index < 1 ||
method_type_info->descriptor_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM method descriptor_index error.");
free(method_type_info);
return -1;
}
show_class_info("descriptor_index %d\n", method_type_info->descriptor_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)method_type_info;
return 0;
}
int handle_class_method_handle(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_MethodHandle_info *method_handle_info;
method_handle_info = (struct CONSTANT_MethodHandle_info *)
malloc(sizeof(struct CONSTANT_MethodHandle_info));
if (!method_handle_info) {
jvm_error(VM_ERROR_MEMORY, "malloc failed.");
return -1;
}
CLASS_READ_U1(method_handle_info->reference_kind, p_mem);
CLASS_READ_U2(method_handle_info->reference_index, p_mem);
if (method_handle_info->reference_kind < 1 &&
method_handle_info->reference_kind > 9) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM method reference_kind error.");
free(method_handle_info);
return -1;
}
if (method_handle_info->reference_index < 1 ||
method_handle_info->reference_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM method reference_index error.");
free(method_handle_info);
return -1;
}
show_class_info("reference_kind: %d, reference_index: %d\n",
method_handle_info->reference_kind,
method_handle_info->reference_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)method_handle_info;
return 0;
}
int handle_class_name_and_type(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_NameAndType_info *name_type_info;
name_type_info = (struct CONSTANT_NameAndType_info *)
malloc(sizeof(struct CONSTANT_NameAndType_info));
if (!name_type_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U2(name_type_info->name_index, p_mem);
CLASS_READ_U2(name_type_info->descriptor_index, p_mem);
if (name_type_info->name_index < 1 ||
name_type_info->name_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM class_name_and_type index error.");
free(name_type_info);
return -1;
}
if (name_type_info->descriptor_index < 1 ||
name_type_info->descriptor_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE,
"JVM class_name_and_type descriptor_index error.");
free(name_type_info);
return -1;
}
show_class_info("name_index: %d, descriptor_index: %d\n",
name_type_info->name_index, name_type_info->descriptor_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)name_type_info;
return 0;
}
int handle_class_double(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_Double_info *double_info;
double_info = (struct CONSTANT_Double_info *)
malloc(sizeof(struct CONSTANT_Double_info));
if (!double_info) {
jvm_error(VM_ERROR_MEMORY, "malloc failed.");
return -1;
}
CLASS_READ_U4(double_info->high_bytes, p_mem);
CLASS_READ_U4(double_info->low_bytes, p_mem);
show_class_info("high_bytes: %d, low_bytes: %d\n",
double_info->high_bytes, double_info->low_bytes);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)double_info;
return 0;
}
int handle_class_float(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_Float_info *float_info;
float_info = (struct CONSTANT_Float_info *)
malloc(sizeof(struct CONSTANT_Float_info));
if (!float_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U4(float_info->bytes, p_mem);
show_class_info("bytes: %d\n", float_info->bytes);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)float_info;
return 0;
}
int handle_class_integer(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_Integer_info *integer_info;
integer_info = (struct CONSTANT_Integer_info *)
malloc(sizeof(struct CONSTANT_Integer_info));
if (!integer_info) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return -1;
}
CLASS_READ_U4(integer_info->bytes, p_mem);
show_class_info("bytes: %d\n", integer_info->bytes);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)integer_info;
return 0;
}
int handle_class_long(CLASS *jvm_class, u2 idx, u1 tag)
{
struct CONSTANT_Long_info *long_info;
long_info = (struct CONSTANT_Long_info *)
malloc(sizeof(struct CONSTANT_Long_info));
if (!long_info) {
jvm_error(VM_ERROR_MEMORY, "malloc failed.");
return -1;
}
CLASS_READ_U4(long_info->high_bytes, p_mem);
CLASS_READ_U4(long_info->low_bytes, p_mem);
show_class_info("high bytes: %d, low bytes: %d\n",
long_info->high_bytes, long_info->low_bytes);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)long_info;
return 0;
}
int hanlde_class_string(CLASS *jvm_class, u2 constant_pool_count,
u2 idx, u1 tag)
{
struct CONSTANT_String_info *string_info;
string_info = (struct CONSTANT_String_info *)
malloc(sizeof(struct CONSTANT_String_info));
if (!string_info) {
jvm_error(VM_ERROR_MEMORY, "malloc failed.");
return -1;
}
CLASS_READ_U2(string_info->string_index, p_mem);
if (string_info->string_index < 1 ||
string_info->string_index >= constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM string_index error.");
free(string_info);
return -1;
}
show_class_info("string index: %d\n", string_info->string_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)string_info;
return 0;
}
int handle_class_methodref_info(CLASS *jvm_class, u2 constant_pool_count,
u2 idx, u1 tag)
{
struct CONSTANT_Methodref_info *methodref_info;
methodref_info = (struct CONSTANT_Methodref_info *)
malloc(sizeof(struct CONSTANT_Methodref_info));
if (!methodref_info) {
jvm_error(VM_ERROR_MEMORY, "malloc failed.");
return -1;
}
CLASS_READ_U2(methodref_info->class_index, p_mem);
if (methodref_info->class_index < 1 ||
methodref_info->class_index >= constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM string_index error.");
free(methodref_info);
return -1;
}
CLASS_READ_U2(methodref_info->name_and_type_index, p_mem);
if (methodref_info->class_index < 1 ||
methodref_info->class_index >= constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM string_index error.");
free(methodref_info);
return -1;
}
show_class_info("class_index: %d, name_and_type_index: %d\n",
methodref_info->class_index,
methodref_info->name_and_type_index);
jvm_class->constant_info[idx].index = idx;
jvm_class->constant_info[idx].tag = tag;
jvm_class->constant_info[idx].base = (u1 *)methodref_info;
return 0;
}
int parse_constant_count(CLASS *jvm_class)
{
show_class_info("\n-----------parse contant pool count----------------------:\n\n");
CLASS_READ_U2(jvm_class->constant_pool_count, p_mem)
show_class_info("jvm constant_pool_count: %d\n", jvm_class->constant_pool_count);
if (jvm_class->constant_pool_count >= 65535) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM constant_pool_count too bigger.\n");
return -1;
}
return 0;
}
int alloc_constant_info(CLASS *jvm_class)
{
jvm_class->constant_info = (struct constant_info_st *)
malloc(sizeof(struct constant_info_st) * jvm_class->constant_pool_count);
if (!jvm_class->constant_info) {
__error("Malloc failed.");
return -1;
}
memset(jvm_class->constant_info, '\0', sizeof(struct constant_info_st) *
jvm_class->constant_pool_count);
return 0;
}
void free_constant_info(CLASS *jvm_class)
{
if (jvm_class->constant_info)
free(jvm_class->constant_info);
}
int __parse_class_constant(CLASS *jvm_class, int idx)
{
u1 constant_tag;
CLASS_READ_U1(constant_tag, p_mem)
show_class_info("- idx: %d constant tag: %d\t", idx, (int)constant_tag);
switch (constant_tag) {
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
if (handle_class_methodref_info(jvm_class, jvm_class->constant_pool_count,
idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_Class:
if (handle_class_info(jvm_class, jvm_class->constant_pool_count,
idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_String:
if (hanlde_class_string(jvm_class, jvm_class->constant_pool_count,
idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_Long:
if (handle_class_long(jvm_class, idx, constant_tag) == -1)
return -1;
idx++;
break;
case CONSTANT_Integer:
if (handle_class_integer(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_Float:
if (handle_class_float(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_Double:
if (handle_class_double(jvm_class, idx, constant_tag) == -1)
return -1;
idx++;
break;
case CONSTANT_NameAndType:
if (handle_class_name_and_type(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_MethodHandle:
if (handle_class_method_handle(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_MethodType:
if (handle_class_method_type(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_InvokeDynamic:
if (handle_class_InvokeDynamic(jvm_class, idx, constant_tag) == -1)
return -1;
break;
case CONSTANT_Utf8:
if (handle_class_utf8(jvm_class, idx, constant_tag) == -1)
return -1;
break;
default:
jvm_error(VM_ERROR_CLASS_FILE, "constant error.");
return -1;
}
return 0;
}
int parse_class_constant(CLASS *jvm_class)
{
u2 idx;
if (parse_constant_count(jvm_class) == -1)
return -1;
if (alloc_constant_info(jvm_class) == -1)
return -1;
/* The constant_pool table is indexed from 1 to constant_pool_count-1. */
for (idx = 1; idx <= jvm_class->constant_pool_count - 1; idx++ ) {
if (__parse_class_constant(jvm_class, idx) == -1)
goto out;
}
show_class_info("\n");
return 0;
out:
free_constant_info(jvm_class);
mmap_exit();
return -1;
}
int parse_class_access_flag(CLASS *jvm_class)
{
u2 access_flag;
/* read class access flag. */
CLASS_READ_U2(jvm_class->access_flag, p_mem)
show_class_info("class access_flag: 0x%x\n", jvm_class->access_flag);
access_flag = jvm_class->access_flag;
if (access_flag & ACC_INTERFACE) {
if (!(access_flag & ACC_ABSTRACT))
goto out;
if ((access_flag & ACC_FINAL) || (access_flag & ACC_SUPER) ||
(access_flag & ACC_ENUM)) {
goto out;
}
}
if (access_flag & ACC_ANNOTATION) {
if (!(access_flag & ACC_INTERFACE))
goto out;
if ((access_flag & ACC_FINAL) || (access_flag & ACC_ABSTRACT))
goto out;
}
return 0;
out:
jvm_error(VM_ERROR_CLASS_FILE, "JVM access_flag error.\n");
return -1;
}
int parse_class_this_super(CLASS *jvm_class)
{
CLASS_READ_U2(jvm_class->this_class, p_mem)
CLASS_READ_U2(jvm_class->super_class, p_mem)
show_class_info("this_class: %d\tsuper_class: %d\n\n", jvm_class->this_class,
jvm_class->super_class);
if (jvm_class->this_class < 1 &&
jvm_class->this_class >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM string_index error.");
return -1;
}
if (jvm_class->super_class < 1 &&
jvm_class->super_class >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM string_index error.");
return -1;
}
return 0;
}
int parse_class_interface(CLASS *jvm_class)
{
u2 idx, index;
CLASS_READ_U2(jvm_class->interfaces_count, p_mem)
show_class_info("interfaces_count: %d\n", jvm_class->interfaces_count);
for (idx = 0; idx < jvm_class->interfaces_count; idx++ ) {
CLASS_READ_U2(index, p_mem);
show_class_info("index: %d\n", index);
}
return 0;
}
int parse_constant_value(CLASS_FILED *new_filed)
{
u4 attribute_length;
u2 constantvalue_index;
CLASS_READ_U4(attribute_length, p_mem)
show_class_info("\tattribute_length: %d\n", attribute_length);
CLASS_READ_U2(constantvalue_index, p_mem)
show_class_info("\tconstantvalue_index: %d\n", constantvalue_index);
return 0;
}
int __parse_class_filed_attribute(CLASS *jvm_class, CLASS_FILED *new_filed)
{
u2 name_index;
u1 *attr_name;
CLASS_READ_U2(name_index, p_mem)
show_class_info("attritbutes name_index: %d\n", name_index);
attr_name = jvm_class->constant_info[name_index].base;
if (!strcmp(attr_name, "ConstantValue")) {
show_class_info("parse ConstantValue attribute:\n");
if (parse_constant_value(new_filed) == -1)
return -1;
}
else if (!strcmp(attr_name, "Signature")) {
show_class_info("parse Signature:\n");
}
else if (!strcmp(attr_name, "Synthetic")) {
show_class_info("parse Synthetic:\n");
}
else if (!strcmp(attr_name, "Deprecated")) {
show_class_info("parse Deprecated:\n");
}
else if (!strcmp(attr_name, "RuntimeVisibleAnnotations")) {
show_class_info("parse RuntimeVisibleAnnotations:\n");
}
else if (!strcmp(attr_name, "RuntimeInvisibleAnnotations")) {
show_class_info("parse RuntimeInvisibleAnnotations:\n");
}
else {
jvm_error(VM_ERROR_CLASS_FILE, "JVM parse wrong attributes.");
return -1;
}
return 0;
}
int parse_class_filed_attribute(CLASS *jvm_class, CLASS_FILED *new_filed)
{
u2 count;
for (count = 0; count < new_filed->attributes_count; count++) {
if (__parse_class_filed_attribute(jvm_class, new_filed) == -1) {
return -1;
}
}
return 0;
}
CLASS_FILED *__parse_class_filed(CLASS *jvm_class)
{
CLASS_FILED *new_filed;
new_filed = (CLASS_FILED *)malloc(sizeof(CLASS_FILED));
if (!new_filed) {
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
return NULL;
}
CLASS_READ_U2(new_filed->access_flag, p_mem)
show_class_info("\naccess_flag: 0x%x\n", new_filed->access_flag);
CLASS_READ_U2(new_filed->name_index, p_mem)
show_class_info("name_index: 0x%x\n", new_filed->name_index);
if (new_filed->name_index < 1 &&
new_filed->name_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM parse filed error.\n");
free(new_filed);
return -1;
}
CLASS_READ_U2(new_filed->descriptor_index, p_mem)
show_class_info("descriptor_index: 0x%x\n", new_filed->descriptor_index);
if (new_filed->descriptor_index < 1 &&
new_filed->descriptor_index >= jvm_class->constant_pool_count) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM parse filed error.\n");
free(new_filed);
return -1;
}
CLASS_READ_U2(new_filed->attributes_count, p_mem)
show_class_info("attributes_count: 0x%x\n", new_filed->attributes_count);
if (parse_class_filed_attribute(jvm_class, new_filed) == -1) {
free(new_filed);
return NULL;
}
new_filed->name_base = jvm_class->constant_info[new_filed->name_index].base;
new_filed->desc_base = jvm_class->constant_info[new_filed->descriptor_index].base;
new_filed->class = jvm_class;
show_class_info("#%s\t%s\n", new_filed->name_base, new_filed->desc_base);
list_add_tail(&(new_filed->list), &(jvm_class->filed_list_head));
return new_filed;
}
int parse_class_filed(CLASS *jvm_class)
{
CLASS_FILED *new_filed;
u2 idx;
INIT_LIST_HEAD(&(jvm_class->filed_list_head));
show_class_info("---------------parse class filed--------------------------:\n");
CLASS_READ_U2(jvm_class->fileds_count, p_mem)
show_class_info("filed_count: %d\n", jvm_class->fileds_count);
for (idx = 0; idx < jvm_class->fileds_count; idx++) {
show_class_info("\n--------%d-----------", idx);
new_filed = __parse_class_filed(jvm_class);
if (!new_filed) {
jvm_error(VM_ERROR_CLASS_FILE, "JVM parse filed error.");
return -1;
}
}
return 0;
}
int __parse_exception_table(CLASS_CODE *code, u4 len)
{
EXCEPTION_TABLE *exception_table;
u2 idx;
exception_table = (EXCEPTION_TABLE *)malloc(sizeof(EXCEPTION_TABLE) * len);
if (!exception_table) {
__error("malloc failed.");
return -1;
}
for (idx = 0; idx < len; idx++ ) {
CLASS_READ_U2(exception_table[idx].start_pc, p_mem)
show_class_info("start_pc: %d\n", exception_table[idx].start_pc);
CLASS_READ_U2(exception_table[idx].end_pc, p_mem)
show_class_info("end_pc: %d\n", exception_table[idx].end_pc);
CLASS_READ_U2(exception_table[idx].handler_pc, p_mem)
show_class_info("handler_pc: %d\n", exception_table[idx].handler_pc);
CLASS_READ_U2(exception_table[idx].catch_type, p_mem)
show_class_info("catch_type: %d\n", exception_table[idx].catch_type);
}
code->exception_table = exception_table;
return 0;
}
void print_line_number_table(LINE_NUMBER_TABLE_ATTR *table_attr)
{
u2 idx;
show_class_info("\nLineNumberTable:\n");
for (idx = 0; idx < table_attr->line_number_table_length; idx++) {
show_class_info("line: %d : %d\n",
table_attr->table_base[idx].start_pc,
table_attr->table_base[idx].line_number);
}
}
int __parse_line_number_table(CLASS_CODE *code, int index)
{
LINE_NUMBER_TABLE_ATTR *table_attr;
u2 idx;
table_attr = (LINE_NUMBER_TABLE_ATTR *)malloc(sizeof(LINE_NUMBER_TABLE_ATTR));
if (!table_attr) {
__error("malloc failed.");
return -1;
}
CLASS_READ_U4(table_attr->attribute_length, p_mem)
show_class_info("\t\tattribute_length: %d\n", table_attr->attribute_length);
CLASS_READ_U2(table_attr->line_number_table_length, p_mem)
show_class_info("\t\tline_number_table_length: %d\n",
table_attr->line_number_table_length);
table_attr->table_base = (LINE_NUMBER_TABLE *)
malloc(sizeof(LINE_NUMBER_TABLE) * table_attr->line_number_table_length);
if (!table_attr->table_base) {
free(table_attr);
return -1;
}
for (idx = 0; idx < table_attr->line_number_table_length; idx++ ) {
CLASS_READ_U2((table_attr->table_base)[idx].start_pc, p_mem)
show_class_info("\t\tstart_pc: %d\n", (table_attr->table_base)[idx].start_pc);
CLASS_READ_U2(table_attr->table_base[idx].line_number, p_mem)
show_class_info("\t\tline_number: %d\n", table_attr->table_base[idx].line_number);
}
code->table_attr = table_attr;
return 0;
}
int __parse_verification_type_info(union verification_type_info *ver_info)
{
u1 tag;
CLASS_READ_U1(tag, p_mem)
show_class_info("\t\ttag: %d\n", tag);
switch (tag) {
case ITEM_Top:
show_class_info("\t\tITEM_Top.\n");
ver_info->tag = tag;
break;
case ITEM_Integer:
show_class_info("\t\tITEM_Integer.\n");
ver_info->tag = tag;
break;
case ITEM_Float:
show_class_info("\t\tITEM_float.\n");
ver_info->tag = tag;
break;
case ITEM_Double:
show_class_info("\t\tITEM_Double.\n");
ver_info->tag = tag;
break;
case ITEM_Long:
show_class_info("\t\tITEM_Long.\n");
ver_info->tag = tag;
break;
case ITEM_Null:
show_class_info("\t\tITEM_NULL.\n");
ver_info->tag = tag;
break;
case ITEM_UninitializedThis:
show_class_info("\t\tITEM_UninitializedThis.\n");
ver_info->tag = tag;
break;
case ITEM_Object:
{
show_class_info("\t\tITEM_Object.\n");
ver_info->tag = tag;
CLASS_READ_U2(ver_info->a.cpool_index, p_mem)
show_class_info("\t\tcpool_index: %d\n", ver_info->a.cpool_index);
break;
}
case ITEM_Uninitialized:
{
show_class_info("\t\tITEM_Uninitialized.\n");
ver_info->tag = tag;
CLASS_READ_U2(ver_info->b.offset, p_mem)
show_class_info("\t\toffset: %d\n", ver_info->b.offset);
break;
}
default:
return -1;
}
return 0;
}
union verification_type_info *parse_ver_info(u1 stack_num)
{
union verification_type_info *ver_info;
u1 idx;
ver_info = (union verification_type_info *)malloc(
sizeof(union verification_type_info) * stack_num);
if (!ver_info) {
__error("malloc failed.");
return NULL;
}
for (idx = 0; idx < stack_num; idx++) {
if (__parse_verification_type_info(ver_info + idx) == -1) {
goto out;
}
}
return ver_info;
out:
for (; idx > 0; idx--)
free(ver_info + idx);
return NULL;
}
int __parse_stack_map_frame(STACK_MAP_FRAME *stack_frame)
{
u1 frame_type;
u1 idx;
CLASS_READ_U1(frame_type, p_mem)
show_class_info("\t\tframe_type: %d\n", frame_type);