-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuter.c
830 lines (710 loc) · 25.2 KB
/
executer.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define DEFAULT_INPUT_FILE "out.lx"
#define u_char unsigned char
#define STACK_SIZE 2048
#define HEAP_SIZE 2048
#define COMMENT_CHAR 0x23 // so #
#define BREAKPOINT asm("int $3");
// CMP FLAGS ENUM
#define ZERO 1
#define GREATER 2
#define LESS 4
#define FLAGS_REGISTER_BYTECODE 0x60
#define RIP_REGISTER_CODE 0x58
#define RSP_REGISTER_CODE 0x56
#define RAX_REGISTER_CODE 0x50
#define RDI_REGISTER_CODE 0x53
#define RSI_REGISTER_CODE 0x54
#define RDX_REGISTER_CODE 0x55
#define RCX_REGISTER_CODE 0x52
#define JUMP_INSTRUCTIONS_SIZE 5
char* MEMORY [STACK_SIZE+HEAP_SIZE];
int* OPEN_FILE_DESCRIPTORS[64];
/*
Defining byte code - register mapping table
*/
typedef struct {
long long* rax;
long long* rbx;
long long* rcx;
long long* rdi;
long long* rsi;
long long* rdx;
long long* rsp;
long long* rbp;
long long* rbf;
long long* rip;
long long* flags;
} s_registers;
typedef struct { u_char key; long long* regLoc; } t_regByteCodeLookup;
static t_regByteCodeLookup registerByteCodeVariable[] = {
{0x50,NULL},
{0x51,NULL},
{0x52,NULL},
{0x53,NULL},
{0x54,NULL},
{0x55,NULL},
{0x56,NULL},
{0x57,NULL},
{0x58,NULL},
{0x59,NULL},
{0x60,NULL}
};
#define BYTE_LOOKUP_LEN (sizeof(registerByteCodeVariable)/sizeof(t_regByteCodeLookup))
s_registers* initRegisterLookup(void){
/*
Initialising the lookup table with the right register pointers,
which aren't constant.
*/
s_registers* registers = malloc(sizeof(s_registers));
/*
Allocating 8 byte memory for each register
*/
registers->rax = malloc(sizeof(long long));
registers->rbx = malloc(sizeof(long long));
registers->rcx = malloc(sizeof(long long));
registers->rdi = malloc(sizeof(long long));
registers->rsi = malloc(sizeof(long long));
registers->rdx = malloc(sizeof(long long));
registers->rsp = malloc(sizeof(long long));
registers->rbp = malloc(sizeof(long long));
registers->rip = malloc(sizeof(long long));
registers->rbf = malloc(sizeof(long long));
registers->flags = malloc(sizeof(long long));
/*
Setting every register to zero
*/
*(registers->rax) = 0;
*(registers->rbx) = 0;
*(registers->rcx) = 0;
*(registers->rdi) = 0;
*(registers->rsi) = 0;
*(registers->rdx) = 0;
*(registers->rbp) = 0;
*(registers->rsp) = 0;
*(registers->rbf) = 0;
*(registers->rip) = 0;
*(registers->flags) = 0;
/*
Initialising dictionary with pointer location of registers
*/
registerByteCodeVariable[0].regLoc = registers->rax;
registerByteCodeVariable[1].regLoc = registers->rbx;
registerByteCodeVariable[2].regLoc = registers->rcx;
registerByteCodeVariable[3].regLoc = registers->rdi;
registerByteCodeVariable[4].regLoc = registers->rsi;
registerByteCodeVariable[5].regLoc = registers->rdx;
registerByteCodeVariable[6].regLoc = registers->rbp;
registerByteCodeVariable[7].regLoc = registers->rsp;
registerByteCodeVariable[8].regLoc = registers->rip;
registerByteCodeVariable[9].regLoc = registers->rbf;
registerByteCodeVariable[10].regLoc = registers->flags;
return registers;
}
unsigned long getFileSize(char* filename){
/*
Getting the size of a file in bytes and returning it.
Returns 0xffffff if file couldn't be opened.
*/
unsigned long fileSize = 0;
FILE* file_fd = NULL;
file_fd = fopen(filename,"rb");
if (file_fd == NULL){
fprintf(stderr,"File '%s' couldn't be opened.",filename);
}
fseek(file_fd,0L,SEEK_END);
fileSize = ftell(file_fd);
fseek(file_fd,0L,SEEK_SET);
fclose(file_fd);
return fileSize;
}
u_char* loadByteCode(char* filename){
/*
Creating buffer of the right size for the contents of the given file
and copying over all the bytes. Also appends 0xff to end of file acting as
EOF character.
Then returning pointer to loaded bytes.
*/
unsigned long fileSize = getFileSize(filename);
unsigned char* byteCode = malloc(fileSize+1);
*(byteCode+fileSize) = 0xff;
FILE* file_fd = NULL;
file_fd = fopen(filename,"rb");
if (file_fd == NULL){
fprintf(stderr,"File '%s' couldn't be opened.",filename);
}
/* Copying over bytes */
fread(byteCode,1,fileSize,file_fd);
fclose(file_fd);
return byteCode;
}
long long* getRegisterPointer(u_char searchByte){
/*
Searches the byte-Code/register-pointer table for the
*/
unsigned int i;
for (i = 0; i<BYTE_LOOKUP_LEN;i++){
t_regByteCodeLookup sym = registerByteCodeVariable[i];
if ( sym.key == searchByte){
return sym.regLoc;
}
}
printf("Invalid register in instruction given. [%x]\n",searchByte);
return NULL;
}
void setZeroFlag(long long result){
/*
Function that first resets the zero flag and then sets it if the result
is equal to zero.
*/
long long* flagsPointer = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
/*
Setting the first bit to zero/ Resetting zero flag
*/
*flagsPointer &= ~(1UL << 0);
if (result == 0){
/*
Setting the zero flag (first bit = 1)
*/
*flagsPointer |= 1;
}
}
int addInstruction(u_char* instruction){
/*
Returns the length of the entire instruction (including 1 byte for opcode)
Then adds the second register onto the first one.
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
*reg1_pointer = *reg1_pointer+*reg2_pointer;
setZeroFlag(*reg1_pointer+*reg2_pointer);
return 3;
}
int subInstruction(u_char* instruction){
/*
Returns the length of the entire instruction (including 1 byte for opcode)
Then subtracts the second register from the first register.
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
*reg1_pointer = *reg1_pointer - *reg2_pointer;
setZeroFlag(*reg1_pointer-*reg2_pointer);
return 3;
}
int andInstruction(u_char* instruction){
/*
Returns the length of the entire instruction (including 1 byte for opcode)
Then performs a binary AND (&) on both and puts the result into the first
register.
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
*reg1_pointer = *reg1_pointer & *reg2_pointer;
setZeroFlag(*reg1_pointer&*reg2_pointer);
return 3;
}
int mulInstruction(u_char* instruction){
/*
Returns the length of the entire instruction (including 1 byte for opcode)
Then multiplies both registers and puts the resulting value into the
first provided register.
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
*reg1_pointer = (*reg1_pointer) * (*reg2_pointer);
setZeroFlag((*reg1_pointer) * (*reg2_pointer));
return 3;
}
int divInstruction(u_char* instruction){
/*
Returns the length of the entire instruction (including 1 byte for opcode)
Then divides both registers and puts the resulting value into the
first provided register. Value is returned as an integer (simply casted
to a ll)
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
/*
If the dividend is 0, return zero division error
*/
if (*reg2_pointer == 0){
puts("Zero division error encountered.");
exit(1);
}
*reg1_pointer = (long long)(*reg1_pointer) / (*reg2_pointer);
setZeroFlag((*reg1_pointer) / (*reg2_pointer));
return 3;
}
int xorInstruction(u_char* instruction){
/*
Returns the length of the xor instruction (3) and modifies the
first register to be the xor result of reg1 and reg2.
*/
u_char reg1_code = *(instruction+1);
u_char reg2_code = *(instruction+2);
long long* reg1_pointer = getRegisterPointer(reg1_code);
long long* reg2_pointer = getRegisterPointer(reg2_code);
if (reg1_pointer == NULL || reg2_pointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
*reg1_pointer = (*reg1_pointer) ^ (*reg2_pointer);
setZeroFlag((*reg1_pointer) ^ (*reg2_pointer));
return 3;
}
int movInstruction(u_char* instruction){
/*
Returns the length of the xor instruction (3) and modifies the
first register to be the xor result of reg1 and reg2.
*/
/*
Tells the execution engine whether to treat the following register byte
as the register to put the value into or the stack/heap location to write
to.
1 = register / 3 = register location
*/
u_char specifier = *(instruction+1);
u_char reg1 = *(instruction+2);
long long* firstRegister = getRegisterPointer(reg1);
if (firstRegister == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
/*
This value specifies whether the following 8 bytes shall be treated
as a simple number, a register or the location of a register to
read data from.
1 = register/ 2 = val / 3 = register location
*/
u_char valSpecifier = *(instruction+3);
long long writeValue = 0;
if (valSpecifier == 1){
/*
Read the least significant byte to get byte value for the register.
*/
u_char registerByte = *(instruction+11);
long long* registerPointer = getRegisterPointer(registerByte);
if (registerPointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
writeValue = *registerPointer;
}else if(valSpecifier == 2){
/*
Read 8 bytes and store them as the write Value
*/
writeValue = *((long long*)(instruction+4));
/*
We read it in the wrong byte order so we have to change it here.
*/
writeValue = __builtin_bswap64(writeValue);
}else if(valSpecifier == 3){
/*
First get the value of the register (stored in least significant byte)
and then check if it is inbounds of stack and/or heap. Later read
8 bytes from this address and store them in writeValue.
*/
u_char registerByte = *(instruction+11);
long long* registerPointer = getRegisterPointer(registerByte);
if (registerPointer == NULL){
puts("Execution Aborted, couldn't parse registers.");
exit(1);
}
long long registerValue = *registerPointer;
/*
Checking whether the read is out of bounds or not.
*/
if (registerValue >= 0 && registerValue+8 <= STACK_SIZE+HEAP_SIZE){
writeValue = *((long long*)(MEMORY+registerValue));
}else{
printf("Out of bounds read to %lld detected!\n",registerValue);
exit(1);
}
}else{
printf("Read invalid valueSpecifier in mov instruction (second part)\
[%d]\n",valSpecifier);
exit(1);
}
/*
Checking the write-to parameter
*/
if (specifier == 1){
// Write to the register
*firstRegister = writeValue;
}else if(specifier == 3){
// Write to register location
long long registerValue = *firstRegister;
if (registerValue >= 0 && registerValue+8 <= STACK_SIZE+HEAP_SIZE){
// Performing write operation
*((long long*)((u_char*)MEMORY+registerValue)) = writeValue;
}else{
printf("Out of bounds write to %lld detected!\n",registerValue);
exit(1);
}
}else{
printf("Read invalid specifier in mov instruction (first part)\
[%d]\n",specifier);
exit(1);
}
return 12;
}
int jmpInstruction(u_char* instruction, unsigned long codeSize){
/*
Returns the new value for the instruction pointer.
*/
unsigned int jumpAddress = *((int*)(instruction+1));
/*
Making sure you can only jump in the region the loaded code is in.
*/
if (jumpAddress <= codeSize){
return jumpAddress;
}
printf("Prohibited invalid jump to %d. (Instruction starts @ %p with\
codeSize of %lu\n",jumpAddress,instruction,codeSize);
exit(1);
}
int jneInstruction(u_char* instruction,unsigned long codeSize){
/*
Returns either the new rip if the zero flag was NOT set or -1 telling the
executor to just increment rip by 5 (the size of je/jne/jg/jl)
*/
/*
Check if zero flag is set
*/
long long* flags = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
if (*flags & 1) {
return -1;
}else{
return jmpInstruction(instruction,codeSize);
}
}
int jeInstruction(u_char* instruction,unsigned long codeSize){
/*
Returns either the new rip if the zero flag was set or -1 telling the
executor to just increment rip by 5 (the size of je/jne/jg/jl)
*/
/*
Check if zero flag is set
*/
long long* flags = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
if (*flags & 1) {
return jmpInstruction(instruction,codeSize);
}else{
return -1;
}
}
int jlInstruction(u_char* instruction,unsigned long codeSize){
/*
Returns either the new rip if the LESS flag was set or -1 telling the
executor to just increment rip by 5 (the size of je/jne/jg/jl)
*/
/*
Check if LESS flag is set
*/
long long* flags = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
if ((*flags>>2) & 1) {
return jmpInstruction(instruction,codeSize);
}else{
return -1;
}
}
int jgInstruction(u_char* instruction,unsigned long codeSize){
/*
Returns either the new rip if the GREATER flag was set or -1 telling the
executor to just increment rip by 5 (the size of je/jne/jg/jl)
*/
/*
Check if GREATER flag is set
*/
long long* flags = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
if ((*flags>>1) & 1) {
return jmpInstruction(instruction,codeSize);
}else{
return -1;
}
}
int pushInstruction(u_char* instruction){
/*
Pushes the 8 byte value stored in the provided register onto the stack
(at rsp) and increments rsp by 8.
Returns 2, the length of the instruction
*/
/*
Check if enough space for the variable is remaining
*/
long long* rspPointer = getRegisterPointer(RSP_REGISTER_CODE);
if (*rspPointer+8 > STACK_SIZE){
puts("Stack full, about to be oveflowed by push instruction.");
exit(1);
}
/*
Get the value of the register to be pushed
Create space on the stack for the variable
*/
u_char registerByteCode = *(instruction+1);
long long* registerPointer = getRegisterPointer(registerByteCode);
long long registerValue = *registerPointer;
*((long long*)((u_char*)MEMORY+*(rspPointer))) = registerValue;
*(rspPointer) += 8;
return 2;
}
int popInstruction(u_char* instruction){
/*
Checks if rsp is bigger than 8, if so it pops the value at rsp into
the first provided register and then decrements rsp by 8.
returns 2, length of instruction (with opcode)
*/
long long* rspPointer = getRegisterPointer(RSP_REGISTER_CODE);
if (*(rspPointer) >= 8){
long long* registerPointer = getRegisterPointer(*(instruction+1));
*(registerPointer) = *((long long*)(*rspPointer+(u_char*)MEMORY));
*(rspPointer) -= 8;
}else{
puts("Can't pop any more values, rsp is already smaller than 8");
exit(1);
}
return 2;
}
int cmpInstruction(u_char* instruction){
/*
The compare instruction compares the two provided registers and sets
the according FLAGS register bits. When both register contain the same
value, it sets the ZERO flag. When the second register is bigger than the
first one, it sets the GREATER flag (2) and if the second registers value
smaller, it sets the LESS flag (4).
It returns the instruction (+opcode) length.
*/
long long* firstRegister = getRegisterPointer(*(instruction+1));
long long* secondRegister = getRegisterPointer(*(instruction+2));
long long* flagsRegister = getRegisterPointer(FLAGS_REGISTER_BYTECODE);
if (*firstRegister == *secondRegister){
/*
Setting zero flag
*/
*flagsRegister |= 1UL << 0;
}else if(*secondRegister > *firstRegister){
/*
Setting GREATER flag
*/
*flagsRegister |= 1UL << 1;
}else if(*secondRegister < *firstRegister){
/*
Setting LESS flag
*/
*flagsRegister |= 1UL << 2;
}
return 3;
}
int syscallInstruction(unsigned long codeSize){
/*
This function allows the service to interact with the actual operation
system. This includes printing (as shortcut for write(stdin,...)), exit,
write, open, close and read. More can easily be added here.
RAX contains the number of the syscall to execute and the parameters are
given through rdi, rsi, rdx in this order.
A list with the number-syscall mapping can be seen in the documentation.
*/
long long* raxRegisterPointer = getRegisterPointer(RAX_REGISTER_CODE);
long long raxValue = *raxRegisterPointer;
long long* rdiRegisterPointer = getRegisterPointer(RDI_REGISTER_CODE);
long long* rsiRegisterPointer = getRegisterPointer(RSI_REGISTER_CODE);
long long* rdxRegisterPointer = getRegisterPointer(RDX_REGISTER_CODE);
switch(raxValue){
/* exit */
case 0x80:
if (*rdiRegisterPointer == 0){
exit(0);
}else{
printf("Exited with exit code %lld\n",*rdiRegisterPointer);
exit(*rdiRegisterPointer);
}
break;
/* print */
case 0x86:
/* RDI contains pointer to 0x0 terminated string */
if (*rdiRegisterPointer >= (STACK_SIZE+HEAP_SIZE)){
puts("Attempted out of bounds read!");
exit(1);
}
printf("%s",MEMORY+(*rdiRegisterPointer));
break;
default:
puts("Invalid syscall number!");
exit(1);
}
return 1;
}
void executeInstruction(u_char* instruction,s_registers* registers,
unsigned long codeSize){
/*
instruction is a pointer starting at the instruction we are executing.
`instruction` shall be code+rip. After execution we increment rip by
the length of the previously executed instruction
CodeSize is needed for the jumps to check whether the jump is performed
in bounds of the loaded code to prevent exploits of the executer.
*/
u_char opCode = *(instruction);
int lenInstruction;
int newRIP = 0;
switch(opCode){
/* push */
case 0x10:
lenInstruction = pushInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* pop */
case 0x11:
lenInstruction = popInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* add */
case 0x12:
lenInstruction = addInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* sub */
case 0x13:
lenInstruction = subInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* and */
case 0x14:
lenInstruction = andInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* mul */
case 0x15:
lenInstruction = mulInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* div */
case 0x16:
lenInstruction = divInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* xor */
case 0x17:
lenInstruction = xorInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* mov */
case 0x18:
lenInstruction = movInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* cmp */
case 0x19:
lenInstruction = cmpInstruction(instruction);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
/* je */
case 0x20:
newRIP = jeInstruction(instruction,codeSize);
if (newRIP == -1){
*(registers->rip) += JUMP_INSTRUCTIONS_SIZE;
}else{
*(registers->rip) = newRIP;
}
break;
/* jne */
case 0x21:
newRIP = jneInstruction(instruction,codeSize);
if (newRIP == -1){
*(registers->rip) += JUMP_INSTRUCTIONS_SIZE;
}else{
*(registers->rip) = newRIP;
}
break;
/* jmp */
case 0x22:
newRIP = jmpInstruction(instruction,codeSize);
*(registers->rip) = (long long)newRIP;
break;
/* jg */
case 0x23:
newRIP = jgInstruction(instruction,codeSize);
if (newRIP == -1){
*(registers->rip) += JUMP_INSTRUCTIONS_SIZE;
}else{
*(registers->rip) = newRIP;
}
break;
/* jl */
case 0x24:
newRIP = jlInstruction(instruction,codeSize);
if (newRIP == -1){
*(registers->rip) += JUMP_INSTRUCTIONS_SIZE;
}else{
*(registers->rip) = newRIP;
}
break;
/* syscall */
case 0x25:
lenInstruction = syscallInstruction(codeSize);
*(registers->rip) = *(registers->rip) + lenInstruction;
break;
default:
printf("Opcode (0x%x) is invalid!\n",opCode);
exit(1);
}
return;
}
int execute_assembly(u_char* code,s_registers* registers,char* filename){
/*
First skips shebang then
Execute the instruction at the current instruction pointer forever.
Instruction with exit syscall can quit program/loop
*/
/* Getting rid of shebang */
if (code[0] == 0x23 && code[1] == 0x21){
while (*(code) != 0xa) code++;
}
unsigned long codeSize = getFileSize(filename);
while (true){
executeInstruction(code+*(registers->rip),registers,codeSize);
}
}
int main(int argc, char* argv[]){
s_registers* registers = initRegisterLookup();
char filename[] = DEFAULT_INPUT_FILE;
u_char* byteCode = loadByteCode(filename);
execute_assembly(byteCode,registers,filename);
return 0;
}