-
Notifications
You must be signed in to change notification settings - Fork 4
/
leitor.c
678 lines (573 loc) · 19.8 KB
/
leitor.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
/**
*@file
*@section DESCRIPTION
*Universidade de Brasilia
*
*Gabriel Mesquita de Araujo 13/0009121\n
*Gabriel Ferreira Silva 14/0140131\n
*Renato 13/0009121\n
*Leandro 13/0009121\n
*Carlos 13/0009121\n\n
*
* Software Basico - 1/2016\n
* Professor: Marcelo Ladeira\n\n
*
* Arquivo que contém Funções de leitura do arquivo .class e montagem das estruturas.
* Para salvar a saida em um arquivo execute da seguinte forma:
* ./jvm.exe .class 1 > log.txt*
*/
#include "./includes/leitor.h"
#include "./includes/exibidor.h"
/**
* Função que recebe o nome do arquivo .class a ser lido. Seguindo o jclasslib.
*
* @param nome do arquivo ".class" a ser lido.
* @return estrutura que representa um classFile.
*/
classFile* leitorClasse(char * nomeClass){
FILE* file;
file = fopen(nomeClass, "rb");
if(file == NULL){
printf("Arquivo não encontrado! Erro ao abrir o arquivo!\n");
return 0;
}
//Aloca memória para a estrutura do .class
classFile* cf = NULL;
cf = (classFile*) calloc(sizeof(classFile),1);
if(cf == NULL)
printf("taNULL");
//Le informações gerais.
generalInfo(cf,file);
//Le a constant pool
constantPool(cf,file);
//le informações gerais após a constant pool
secondGeneralInfo(cf,file);
//liberando ponteiros.
fclose(file);
//Retorna referencia ao classFile preenchido.
return cf;
}
/**
* Função que le as informações gerais de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
*/
void generalInfo(classFile* cf, FILE* file){
cf->magic = le4Bytes(file);
if(cf->magic != 0xCAFEBABE){
printf("Arquivo .class inválido!!\n\n");
exit(0);
}
cf->minor_version = le2Bytes(file);
cf->major_version = le2Bytes(file);
cf->constant_pool_count = le2Bytes(file);
}
/**
* Função que le a constant pool de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
*/
void constantPool(classFile* cf, FILE* file){
cf->constant_pool = (cp_info*) malloc((cf->constant_pool_count-1)*sizeof(cp_info));
cp_info* cp;
// loop q simplesmente faz a leitura da constant pool
int i = 0;
for(cp = cf->constant_pool; i < (cf->constant_pool_count-1); cp++){
cp->tag = le1Byte(file);
switch(cp->tag){
case CONSTANT_Class:
cp->info.Class.name_index = le2Bytes(file);
break;
case CONSTANT_Fieldref:
cp->info.Fieldref.class_index = le2Bytes(file);
cp->info.Fieldref.name_and_type_index = le2Bytes(file);
break;
case CONSTANT_NameAndType:
cp->info.NameAndType.name_index = le2Bytes(file);
cp->info.NameAndType.descriptor_index = le2Bytes(file);
break;
case CONSTANT_Utf8:
cp->info.Utf8.length = le2Bytes(file);
cp->info.Utf8.bytes = (uint8_t*) calloc ((cp->info.Utf8.length) + 1 ,sizeof(uint8_t));
fread(cp->info.Utf8.bytes,1,cp->info.Utf8.length,file);
cp->info.Utf8.bytes[cp->info.Utf8.length] = '\0';
break;
case CONSTANT_Methodref:
cp->info.Methodref.class_index = le2Bytes(file);
cp->info.Methodref.name_and_type_index = le2Bytes(file);
break;
case CONSTANT_InterfaceMethodref:
cp->info.InterfaceMethodref.class_index = le2Bytes(file);
cp->info.InterfaceMethodref.name_and_type_index = le2Bytes(file);
break;
case CONSTANT_String:
cp->info.String.string_index = le2Bytes(file);
break;
case CONSTANT_Integer:
cp->info.Integer.bytes = le4Bytes(file);
break;
case CONSTANT_Float:
cp->info.Float.bytes = le4Bytes(file);
break;
case CONSTANT_Double:
cp->info.Double.high_bytes = le4Bytes(file);
cp->info.Double.low_bytes = le4Bytes(file);
cp++;
i++;
break;
case CONSTANT_Long:
cp->info.Long.high_bytes = le4Bytes(file);
cp->info.Long.low_bytes = le4Bytes(file);
cp++;
i++;
break;
default:
break;
}
i++;
}
}
/**
* Função que le as interfaces de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
* @param Quantidade de interfaces a serem lidas.
*/
void interfaceInfo(classFile* cf, FILE* file, uint16_t interfaces_count){
if(interfaces_count == 0)
return;
else{
// aloca espaco apropriado
cf->interfaces = (uint16_t *) malloc((interfaces_count) * sizeof(uint16_t));
// le interface, pondo no array de interfaces
for (int i = 0; i < interfaces_count; i++)
{
cf->interfaces[i] = le2Bytes(file);
}
}
}
/**
* Função que le os fields de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
* @param Quantidade de fields a serem lidas.
*/
void fieldInfo(classFile* cf, FILE* file, uint16_t fields_count){
if(fields_count == 0)
return;
else{
// aloca espaco apropriado
cf->fields = (field_info*) malloc(fields_count * sizeof(field_info));
// le field information, pondo no array de field
for (int i = 0; i < fields_count; i++)
{
// pega bits de acesso, indice do nome e indice do descritor
cf->fields[i].access_flags = le2Bytes(file);
cf->fields[i].name_index = le2Bytes(file);
cf->fields[i].descriptor_index = le2Bytes(file);
cf->fields[i].attributes_count = le2Bytes(file);
// aloca espaco para o array de atributos
cf->fields[i].attributes = (CV_info*) malloc(cf->fields[i].attributes_count * sizeof(CV_info));
// vai lendo atributos
for (int j = 0; j < cf->fields[i].attributes_count; j++)
{
// pega indice do nome do atributo e comprimento do atributo
cf->fields[i].attributes->attribute_name_index = le2Bytes(file);
cf->fields[i].attributes->attribute_length = le4Bytes(file);
// pega constant value index
cf->fields[i].attributes->constantvalue_index = le2Bytes(file);
}
}
}
}
/**
* Função que le os methods de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
* @param Quantidade de methods a serem lidas.
*/
void methodInfo(classFile* cf, FILE* file, uint16_t methods_count){
uint16_t name_ind;
uint32_t att_len;
if(methods_count == 0)
return;
else{
cf->methods = (method_info*) malloc(methods_count*sizeof(method_info));
method_info* cp = cf->methods;
for(int i = 0; i < methods_count; cp++){
cp->access_flags = le2Bytes(file);
if(cp->access_flags == 0x010a ||cp->access_flags == 0x0101||cp->access_flags == 0x0111){
cp->name_index = le2Bytes(file);
cp->descriptor_index = le2Bytes(file);
cp->attributes_count = le2Bytes(file);
i++;
for (int j = 0; j < cp->attributes_count; j++)
{
int64_t temp, temp2;
// pega atributo name index do metodo
temp = le2Bytes(file);
// pega attributo length do metodo
temp = le4Bytes(file);
// vai lendo info
for (int k = 0; k < temp; k++)
{
temp2 = le1Byte(file);
}
}
continue;
}
cp->name_index = le2Bytes(file);
cp->descriptor_index = le2Bytes(file);
cp->attributes_count = le2Bytes(file);
for(int j = 0; j < cp->attributes_count; j++)
{
// pega nome e indice
name_ind = le2Bytes(file);
att_len = le4Bytes(file);
// se for um code
if (strcmp( (char*) cf->constant_pool[name_ind - 1].info.Utf8.bytes, "Code") == 0)
{
// aloca espaco para o attribute
cp->cd_atrb = (code_attribute*) malloc(sizeof(code_attribute));
// le atribute
le_code(&(cp->cd_atrb), name_ind, att_len, file);
}
// senao, se for um exceptions
else if (strcmp( (char*) cf->constant_pool[name_ind - 1].info.Utf8.bytes, "Exceptions") == 0)
{
// aloca espaco para o exceptions
cp->exc_atrb = (exceptions_attribute*) malloc(sizeof(exceptions_attribute));
// le exceptions
le_exc(&(cp->exc_atrb), name_ind, att_len, file);
}
}
i++;
}
}
}
/**
* Função que le as exceptions de um arquivo .class
*
* @param referencia ao array de exceções
* @param indice do nome.
* @param tamanho do atributo.
* @param referência ao file .class.
*/
void le_exc(exceptions_attribute** exc_atrb, uint16_t name_ind, uint32_t att_len, FILE* file)
{
// transfere informacoes relacionadas ao atributo
(*exc_atrb)->attribute_name_index = name_ind;
(*exc_atrb)->attribute_length = att_len;
// pega numero de excecoes
(*exc_atrb)->number_of_exceptions = le2Bytes(file);
// aloca espaco apropriado para indices da tabela de excecoes
(*exc_atrb)->exception_index_table = (uint16_t*) malloc( \
(*exc_atrb)->number_of_exceptions * sizeof(exception_table));
// vai lendo dados e imprimindo
for (int k = 0; k < (*exc_atrb)->number_of_exceptions; k++)
{
(*exc_atrb)->exception_index_table[k] = le2Bytes(file);
//printf("%d - %d", k, (*exc_atrb)->exception_index_table[k]);
}
}
/**
* Função que le os bytecodes de um arquivo .class
*
* @param referencia ao array de bytecodes
* @param indice do nome.
* @param tamanho do atributo.
* @param referência ao file .class.
*/
void le_code(code_attribute** cd_atrb, uint16_t name_ind, uint32_t att_len, FILE* file)
{
int posicao_inicial = ftell(file);
// transfere informacoes relacionadas ao atributo
(*cd_atrb)->attribute_name_index = name_ind;
(*cd_atrb)->attribute_length = att_len;
// leitura do bytecode relacionado a informacoes gerais
(*cd_atrb)->max_stack = le2Bytes(file);
(*cd_atrb)->max_locals = le2Bytes(file);
(*cd_atrb)->code_length = le4Bytes(file);
// salva instrucoes
salva_instrucoes(cd_atrb, file);
// pega tamanho da tabela de excecoes
(*cd_atrb)->exception_table_length = le2Bytes(file);
// aloca espaco apropriado
(*cd_atrb)->exception_table = (exception_table*) malloc( \
(*cd_atrb)->exception_table_length * sizeof(exception_table));
// le dados da tabela de excecoes
for (int k = 0; k < (*cd_atrb)->exception_table_length; k++)
{
(*cd_atrb)->exception_table[k].start_pc = le2Bytes(file);
(*cd_atrb)->exception_table[k].end_pc = le2Bytes(file);
(*cd_atrb)->exception_table[k].catch_type = le2Bytes(file);
}
// pega numero de atributos
(*cd_atrb)->attributes_count = le2Bytes(file);
// aloca espaco apropriado
(*cd_atrb)->attributes = (attribute_info*) malloc ( \
(*cd_atrb)->attributes_count * sizeof(attribute_info));
// le atributos opcionais de debug
// nao precisa preocupar muito com isso
while (ftell(file) - posicao_inicial < (int32_t)((*cd_atrb)->attribute_length))
{
le1Byte(file);
}
}
/**
* Função que salva a instruções lidas do arquivo .class
*
* @param referencia ao array de exceções
* @param referência ao file .class.
*/
void salva_instrucoes(code_attribute** cd_atrb, FILE* file)
{
int opcode, pos_referencia;
int bytes_preench, offsets;
uint32_t default_v, low, high, npairs;
// obtem decodificador de instrucoes
decodificador dec[NUM_INSTRUCAO];
inicializa_decodificador(dec);
// leitura do bytecode relacionado a instrucoes do metodo
// aloca espaco conveniente
(*cd_atrb)->code = (uint8_t*) malloc((*cd_atrb)->code_length * \
sizeof(uint8_t));
// poe valor no espacos corretos
// incrementamos k conforme formos passando no loop
for(uint32_t k = 0; k < (*cd_atrb)->code_length; )
{
// le opcode da instrucao atual
fread(&((*cd_atrb)->code[k]), 1, 1, file);
// pega opcode da instrucao
opcode = (*cd_atrb)->code[k];
k++;
if (opcode == TABLESWITCH)
{
// a posicao de referencia eh o label numerico associado a tableswitch
// k - 1 pois ja incrementamos o k para a proxima instrucao
pos_referencia = k - 1;
// pega bytes de preenchimento
//bytes_preench = k % 4;
bytes_preench = (4 - (k % 4)) % 4;
for (int l = 0; l < bytes_preench; l++)
{
k++;
fread(&((*cd_atrb)->code[k]), 1, 1, file);
}
// pega bytes do target default
default_v = 0;
for (int l = 0; l < 4; l++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
default_v = (default_v << 8) + (*cd_atrb)->code[k];
k++;
}
// pega bytes low
low = 0;
for (int l = 0; l < 4; l++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
low = (low << 8) + (*cd_atrb)->code[k];
k++;
}
// pega bytes high
high = 0;
for (int l = 0; l < 4; l++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
high = (high << 8) + (*cd_atrb)->code[k];
k++;
}
// pega bytes de offset
offsets = 1 + high - low;
for (int l = 0; l < offsets; l++)
{
// pega valor do offset atual
for (int i = 0; i < 4; i++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
}
}
}
else if (opcode == LOOKUPSWITCH)
{
// a posicao de referencia eh o label numerico associado a tableswitch
// k - 1 pois ja incrementamos o k para a proxima instrucao
pos_referencia = k - 1;
// pega bytes de preenchimento - nao salva em nenhum lugar
//bytes_preench = k % 4;
bytes_preench = (4 - (k % 4)) % 4;
for (int l = 0; l < bytes_preench; l++)
{
k++;
fread(&((*cd_atrb)->code[k]), 1, 1, file);
}
// pega bytes do target default
default_v = 0;
for (int l = 0; l < 4; l++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
default_v = (default_v << 8) + (*cd_atrb)->code[k];
k++;
}
// pega npairs
npairs = 0;
for (int l = 0; l < 4; l++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
npairs = (npairs << 8) + (*cd_atrb)->code[k];
k++;
}
// pega npairs
for (uint32_t l = 0; l < npairs; l++)
{
// pega valor do match atual
for (int i = 0; i < 4; i++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
}
// pega valor do offset
for (int i = 0; i < 4; i++)
{
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
}
}
}
else if (opcode == WIDE)
{
// pega opcode que segue
fread(&((*cd_atrb)->code[k]), 1, 1, file);
opcode = (*cd_atrb)->code[k];
k++;
// se o opcode for um iload, fload, ...
if (opcode == ILOAD || opcode == FLOAD || opcode == ALOAD || opcode == LLOAD || \
opcode == DLOAD || opcode == ISTORE || opcode == FSTORE || opcode == ASTORE || \
opcode == LSTORE || opcode == DSTORE || opcode == RET)
{
// pega index byte1
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
// pega index byte2
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
}
// se for um iinc
else if (opcode == IINC)
{
// pega indexbyte1
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
// pega indexbyte2
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
// pega constbyte1
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
// pega constbyte2
fread(&((*cd_atrb)->code[k]), 1, 1, file);
k++;
}
// senao
else
{
// arquivo .class corrompido!
printf("arquivo .class invalido na instrucao wide");
exit(1);
}
}
else
{
// obtem quantos operandos a instrucao tem e vai lendo operandos
int num_bytes = dec[opcode].bytes;
for (int l = 0; l < num_bytes; l++)
{
// pega operando
fread(&((*cd_atrb)->code[k]), 1, 1, file);
// atualiza valor de k
k++;
}
}
}
}
/**
* Função que le os attributes de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
* @param Quantidade de attributes a serem lidas.
*/
void attributeInfo(classFile* cf, FILE* file, uint16_t attributes_count){
if(attributes_count == 0)
return;
else{
cf->attributes = (attribute_info*) malloc(attributes_count*sizeof(attribute_info));
attribute_info* cp = cf->attributes;
for(int i = 0; i < attributes_count; cp++){
cp->attribute_name_index = le2Bytes(file);
cp->attribute_length = le4Bytes(file);
cp->info = (uint8_t*) malloc((cp->attribute_length)*sizeof(uint8_t));
for(uint32_t j = 0; j < cp->attribute_length; j++){
fread(&cp->info[j],1,1,file);
}
i++;
}
}
}
/**
* Função que le a segunda parte de informações gerais de um arquivo .class
*
* @param referência ao classfile.
* @param referência ao arquivo sendo lido.
*/
void secondGeneralInfo(classFile* cf, FILE* file){
cf->access_flags = le2Bytes(file);
cf->this_class = le2Bytes(file);
cf->super_class = le2Bytes(file);
cf->interfaces_count = le2Bytes(file);
interfaceInfo(cf,file,cf->interfaces_count);
cf->fields_count = le2Bytes(file);
fieldInfo(cf,file,cf->fields_count);
cf->methods_count = le2Bytes(file);
methodInfo(cf,file,cf->methods_count);
cf->attributes_count = le2Bytes(file);
attributeInfo(cf,file,cf->attributes_count);
}
/**
* Função que le 1 byte de um arquivo.
*
* @param referência ao arquivo a ser lido
*/
static inline uint8_t le1Byte(FILE* fp){
uint8_t retorno = getc(fp);
return retorno;
}
/**
* Função que le 2 bytes de um arquivo.
*
* @param referência ao arquivo a ser lido
*/
static inline uint16_t le2Bytes(FILE* fp){
uint16_t retorno = getc(fp);
retorno = (retorno << 8) | (getc(fp));
return retorno;
}
/**
* Função que le 4 bytes de um arquivo.
*
* @param referência ao arquivo a ser lido
*/
static inline uint32_t le4Bytes(FILE* fp){
uint32_t retorno = getc(fp);
retorno = (retorno << 8) | (getc(fp));
retorno = (retorno << 8) | (getc(fp));
retorno = (retorno << 8) | (getc(fp));
return retorno;
}