-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfjit.c
177 lines (164 loc) · 4.07 KB
/
bfjit.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/mman.h>
#include "bfvm.h"
#ifdef DEBUG
#define DPRINTF printf
#else
#define DPRINTF(...)
#endif
static uint16_t *
read_file(const char *file_name, long *size)
{
FILE *fp;
uint16_t *buf;
long s;
if(!(fp = fopen(file_name, "r")))
goto err;
if (fseek(fp, 0, SEEK_END))
goto err;
if (0 > (s = ftell(fp)))
goto err;
if (!(buf = (uint16_t *)malloc(s)))
goto err;
if (fseek(fp, 0, SEEK_SET))
goto err;
if (fread(buf, 1, (size_t)s, fp) < s) {
fclose(fp);
printf("can't read file\n");
return NULL;
}
fclose(fp);
*size = s / sizeof(uint16_t);
return buf;
err:
if (fp)
fclose(fp);
err(1, NULL);
}
static void
interpret(uint16_t *bytecode, long size)
{
int stack[32768];
int i = 0, j = 0;
long psize;
unsigned char *program;
void *func_table[2] = {(void *)putchar, (void *)getchar};
int address_list[65536];
int start_address;
if (size > 65536) {
printf("bytecode too big\n");
return;
}
#ifdef BSD
psize = getpagesize();
#else
psize = sysconf(_SC_PAGE_SIZE);
#endif
if((posix_memalign((void **)&program, psize, psize)))
err(1, "posix_memalign");
if(mprotect((void*)program, psize, PROT_READ | PROT_WRITE | PROT_EXEC))
err(1, "mprotect");
program[j++] = 0x55;// push %ebp
program[j++] = 0x89; program[j++] = 0xe5;// mov %esp,%ebp
program[j++] = 0x83; program[j++] = 0xec; program[j++] = 0x18;// sub $0x18,%esp
program[j++] = 0x8b; program[j++] = 0x5d; program[j++] = 0x08;// mov 0x8(%ebp),%ebx
program[j++] = 0x8b; program[j++] = 0x75; program[j++] = 0x0c;// mov 0xc(%ebp),%esi
start_address = j;
DPRINTF("size:%lu\n", size);
for (;;) {
uint16_t byte = bytecode[i];
if (i >= size) {
DPRINTF("i >= size\n");
break;
}
address_list[i] = j;
switch(byte & 0xff00) {
case BF_PINC:
program[j++] = 0x43; // inc %ebx
break;
case BF_PDEC:
program[j++] = 0x4b; // dec %ebx
break;
case BF_VINC:
program[j++] = 0xff; program[j++] = 0x03;// incl (%ebx)
break;
case BF_VDEC:
program[j++] = 0xff; program[j++] = 0x0b;// decl (%ebx)
break;
case BF_PUTC:
program[j++] = 0x8b; program[j++] = 0x03; // mov (%ebx),%eax
program[j++] = 0x89; program[j++] = 0x04; program[j++] = 0x24;// mov %eax,(%esp)
program[j++] = 0xff; program[j++] = 0x16; // call *(%esi)
break;
case BF_GETC:
program[j++] = 0x89; program[j++] = 0xf0; // mov %esi,%eax
program[j++] = 0xff; program[j++] = 0x50; program[j++] = 0x04; // call *0x4(%eax)
program[j++] = 0x89; program[j++] = 0x03; // mov %eax,(%ebx)
break;
case BF_BEQZ:
program[j++] = 0x83; program[j++] = 0x3b; program[j++] = 0x00; // cmpl $0x0,(%ebx)
program[j++] = 0x74; program[j++] = 0x00; // je 0
break;
case BF_BNEZ:
program[j++] = 0x83; program[j++] = 0x3b; program[j++] = 0x00; // cmpl $0x0,(%ebx)
program[j++] = 0x75; program[j++] = 0x00; // jne 0
break;
default:
printf("unknown operation:%x\n", byte);
return;
}
i++;
}
program[j++] = 0xc9;// leave
program[j++] = 0xc3;// ret
j = start_address;
i = 0;
for (;;) {
uint16_t byte = bytecode[i];
if (i >= size) {
DPRINTF("i >= size\n");
break;
}
switch(byte & 0xff00) {
case BF_BEQZ: {
uint8_t skip = 0x00ff & byte;
j = address_list[i];
j++; j++; j++; // cmpl $0x0,(%ebx)
j++; program[j++] = address_list[i+skip] - j; // je
DPRINTF("je %d\n", address_list[i+skip] - j);
break;
}
case BF_BNEZ: {
uint8_t skip = 0x00ff & byte;
j = address_list[i];
j++; j++; j++; // cmpl $0x0,(%ebx)
j++; program[j++] = 0xff - (j - address_list[i-skip]); // jne
DPRINTF("jne %d\n", 0xff - (j - address_list[i-skip]));
break;
}
default:
break;
}
i++;
}
memset(stack, 0, 32768);
((void (*)(int *, void**))program)(stack, func_table);
}
int main(int argc, char **argv)
{
long size;
uint16_t *bytecode;
if (argc < 2) {
fprintf(stderr, "%s bytecode\n", argv[0]);
return 1;
}
if (!(bytecode = read_file(argv[1], &size)))
return 1;
interpret(bytecode, size);
free(bytecode);
return 0;
}