-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbintran.cpp
257 lines (215 loc) · 7.23 KB
/
bintran.cpp
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
/*!
bintran.cpp - binary translator from stack process architecture to x86.
Copyright 2017 Vyacheslav "ZeronSix" Zhdanovskiy <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "bintran.hpp"
#include "datatools.hpp"
#include "exceptions.hpp"
#include "x86arch.hpp"
#include <experimental/filesystem>
#include <sys/mman.h>
namespace fs = std::experimental::filesystem;
namespace zvm {
BinTran::BinTran(std::size_t alloc_size)
: zvmbinary_(nullptr),
zvmbinary_size_(0),
translated_code_(AllocWriteableMemory(alloc_size)),
allocated_size_(alloc_size),
actual_x86_size_(0) {}
BinTran::~BinTran() {
delete[] zvmbinary_;
if (translated_code_)
munmap((void*)translated_code_, allocated_size_);
}
void BinTran::LoadBinary(const std::string& filename) {
if (!fs::exists(filename))
throw IoException(filename, ERR_FILE_DOESNT_EXIST);
std::size_t filesize = fs::file_size(filename);
std::FILE* f = std::fopen(filename.c_str(), "rb");
if (!f)
throw IoException(filename, ERR_FILE_OPEN_FAILURE);
if (zvmbinary_)
delete[] zvmbinary_;
zvmbinary_ = new Byte[filesize]();
if (!zvmbinary_)
throw AllocException();
std::fread(zvmbinary_, 1, filesize, f);
std::fclose(f);
zvmbinary_size_ = filesize;
}
inline void InitDataLocations(BtInstr& btinstr) {
switch (btinstr.opcode) {
case OPCODE_POP:
case OPCODE_HALT:
case OPCODE_POPBP:
case OPCODE_PUSHBP:
btinstr.op1_loc = DATALOC_NONE;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_NONE;
break;
case OPCODE_PUSH:
case OPCODE_LOAD:
case OPCODE_STORE:
btinstr.op1_loc = DATALOC_IMM;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_STACK;
break;
case OPCODE_CALL:
case OPCODE_JMP:
case OPCODE_JMC:
btinstr.op1_loc = DATALOC_IMM;
btinstr.op2_loc = DATALOC_STACK;
btinstr.res_loc = DATALOC_NONE;
break;
case OPCODE_RET:
btinstr.op1_loc = DATALOC_STACK;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_NONE;
break;
case OPCODE_ADD:
case OPCODE_SUB:
case OPCODE_MUL:
case OPCODE_DIV:
btinstr.op1_loc = DATALOC_STACK;
btinstr.op2_loc = DATALOC_STACK;
btinstr.res_loc = DATALOC_STACK;
break;
case OPCODE_GZ:
case OPCODE_BZ:
case OPCODE_BEZ:
case OPCODE_GEZ:
case OPCODE_EQZ:
case OPCODE_NEQZ:
btinstr.op1_loc = DATALOC_STACK;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_STACK;
break;
case OPCODE_INPUT:
btinstr.op1_loc = DATALOC_STDIN;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_STACK;
break;
case OPCODE_OUTPUT:
btinstr.op1_loc = DATALOC_STACK;
btinstr.op2_loc = DATALOC_NONE;
btinstr.res_loc = DATALOC_STDOUT;
break;
default:
throw UndefinedOpcodeException(btinstr.opcode);
}
}
void BinTran::Translate() {
Register pc = 0;
while (pc < zvmbinary_size_) {
Register bpc = pc;
DecodedInstr instr = FetchInstr(zvmbinary_, pc);
BtInstr btinstr = { .opcode = instr.opcode,
.arg = instr.args[0],
.zvm_addr = std::size_t(bpc) };
InitDataLocations(btinstr);
program_.push_back(btinstr);
zvmaddr_map_[bpc] = &program_.back();
}
// scan for jumps
for (auto it = program_.begin(); it != program_.end(); it++) {
if (it->opcode == OPCODE_JMP || it->opcode == OPCODE_JMC ||
it->opcode == OPCODE_CALL) {
jmp_map_[&(*it)] = zvmaddr_map_[it->arg];
}
}
Optimize();
Byte* program_ptr = (Byte*)translated_code_;
WriteCodeHeader(program_ptr);
for (auto it = program_.begin(); it != program_.end(); it++) {
WriteInstr(program_ptr, *it);
}
WriteCodeFooter(program_ptr);
// patch jumps
for (const auto& it: jmp_map_) {
const int PATCH_OFFSET = 1;
const int JMC_ADD_OFFSET = 6;
const int JUMP_OFFSET = -5;
BtInstr* source = it.first;
BtInstr* dest = it.second;
int src_x86_addr = source->x86_addr;
int dest_x86_addr = dest->x86_addr;
int jmp_dist = dest_x86_addr - src_x86_addr + JUMP_OFFSET;
Byte* patch_addr = (Byte*)translated_code_ + src_x86_addr + PATCH_OFFSET;
if (source->opcode == OPCODE_JMC) {
patch_addr += JMC_ADD_OFFSET;
jmp_dist -= JMC_ADD_OFFSET;
}
*(int*)patch_addr = jmp_dist;
}
actual_x86_size_ = program_ptr - (Byte*)translated_code_;
}
#define IT_PTR() (&(*it))
void BinTran::Optimize() {
if (program_.size() < 2)
return;
auto it = program_.begin();
BtInstr* instr_prev = IT_PTR();
it++;
BtInstr* instr = IT_PTR();
while (it != program_.end()) {
it++;
if (jmp_map_.find(instr) == jmp_map_.end() && instr->IsArithmetic() &&
jmp_map_.find(instr_prev) == jmp_map_.end() &&
instr_prev->IsArithmetic())
{
instr_prev->res_loc = DATALOC_R9;
instr->op2_loc = DATALOC_R9;
}
instr_prev = instr;
instr = IT_PTR();
}
}
#undef IT_PTR
Data Input() {
Data d = 0;
std::scanf("%d", &d);
return d;
}
void Output(Data val) {
std::printf("%d\n", val);
}
void BinTran::Execute() {
translated_code_(&Input, &Output, NULL);
}
void BinTran::LoadX86CodeFromFile(const std::string& filename) {
if (!fs::exists(filename))
throw IoException(filename, ERR_FILE_DOESNT_EXIST);
std::size_t filesize = fs::file_size(filename);
std::FILE* f = std::fopen(filename.c_str(), "rb");
if (!f)
throw IoException(filename, ERR_FILE_OPEN_FAILURE);
std::fread((void*)translated_code_, 1, allocated_size_, f);
std::fclose(f);
actual_x86_size_ = filesize;
}
void BinTran::SaveX86CodeToFile(const std::string& filename) {
std::FILE* f = std::fopen(filename.c_str(), "wb");
if (!f)
throw IoException(filename, ERR_FILE_OPEN_FAILURE);
std::fwrite((void*)translated_code_, 1, actual_x86_size_, f);
std::fclose(f);
}
BinTran::JittedCode BinTran::AllocWriteableMemory(std::size_t size) const {
void* ptr = mmap(0, size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == (void*)-1) {
throw AllocException();
}
return (JittedCode)ptr;
}
} // namespace zvm