-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathllvm_lifter.cpp
More file actions
343 lines (289 loc) · 8.1 KB
/
llvm_lifter.cpp
File metadata and controls
343 lines (289 loc) · 8.1 KB
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
#include "vm.hpp"
vm_lifter::vm_lifter(LLVMContext& ctx, std::vector<handler_t> handlers_)
: context(ctx), builder(ctx), module(new Module("devirt_module", ctx)), handlers(handlers_)
{
Type* int64_t = Type::getInt64Ty(context);
Type* void_t = Type::getVoidTy(context);
Type* ptr_t = PointerType::get(int64_t, 0);
FunctionType* FuncType = FunctionType::get(void_t, { }, false);
function = Function::Create(FuncType, Function::ExternalLinkage, "devirtualized", module);
//rsp = &*function->arg_begin();
//rsp->setName("rsp");
BasicBlock* EntryBB = BasicBlock::Create(context, "entry", function);
builder.SetInsertPoint(EntryBB);
ArrayType* stackType = ArrayType::get(builder.getInt8Ty(), 2048);
AllocaInst* StackArray = builder.CreateAlloca(stackType, nullptr, "vstack_memory");
vsp = builder.CreateGEP(
stackType, StackArray,
{ ConstantInt::get(builder.getInt32Ty(), 0),
ConstantInt::get(builder.getInt32Ty(), 2048) },
"vsp"
);
vtil_ = new vtil_lifter(handlers);
}
void vm_lifter::liftToLLVM()
{
for (int i = 0; i < handlers.size(); i++)
{
handler_t& handler = handlers[i];
BasicBlock* handler_block = BasicBlock::Create(context, std::string(handler.parseToStr() + "_" + std::to_string(i)), function);
handler.block = handler_block;
}
builder.CreateBr(handlers[0].block);
for (int i = 0; i < handlers.size(); i++)
{
handler_t& handler = handlers[i];
builder.SetInsertPoint(handler.block);
if (i == handlers.size() - 1)
break;
BasicBlock* next_block = handlers[i + 1].block;
switch (handler.opcode)
{
case VM_INIT:
//vm_init();
break;
case POP_VR64:
pop_vr64(handler);
break;
case POP_VR32:
pop_vr32(handler);
break;
case PUSH_VR64:
push_vr64(handler);
break;
case PUSH_VR32:
push_vr32(handler);
break;
case PUSH_VSP:
push_vsp();
break;
case POP_VSP:
pop_vsp();
break;
case PUSH_64:
push_64(handler);
break;
case PUSH_32:
push_32(handler);
break;
case SUB64:
sub_64(handler);
break;
case SUB32:
sub_32(handler);
break;
case ADD64:
add_64(handler);
break;
case ADD32:
add_32();
break;
case WRITE32:
write_32();
break;
case LOAD32:
load_32();
break;
case LOAD64:
load_64();
break;
case OR_32:
or_32();
break;
case AND_32:
and_32();
break;
case XOR_32:
xor_32();
break;
case JNZ:
{
BasicBlock* target_block = nullptr;
for (int j = 0; j < handlers.size(); j++)
{
if (handlers[j].address == handler.data)
{
target_block = handlers[j].block;
break;
}
}
llvm::Value* condition = jnz();
builder.CreateCondBr(condition, target_block, next_block);
continue;
}
}
builder.CreateBr(next_block);
}
//vm_exit();
builder.CreateRetVoid();
optimizeLLVM(llvm::OptimizationLevel::O3);
verifyFunction(*function);
module->print(outs(), nullptr);
std::error_code EC;
llvm::raw_fd_ostream dest("output.ll", EC, llvm::sys::fs::OF_None);
if (EC) {
errs() << "Error opening file: " << EC.message() << "\n";
}
else {
module->print(dest, nullptr);
}
module->~Module();
}
void vm_lifter::optimizeLLVM(llvm::OptimizationLevel level)
{
if (!this->module) {
llvm::errs() << "Error: Module is null!\n";
return;
}
llvm::PassBuilder PB;
llvm::LoopAnalysisManager LAM;
llvm::FunctionAnalysisManager FAM;
llvm::CGSCCAnalysisManager CGAM;
llvm::ModuleAnalysisManager MAM;
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
llvm::ModulePassManager MPM = PB.buildPerModuleDefaultPipeline(level);
MPM.run(*this->module, MAM);
}
void vm_lifter::vm_init()
{
//vsp = rsp; return;
for (int i = 0; i <= 15; i++)
{
vpush64(rpop64());
}
}
void vm_lifter::vm_exit()
{
for (int i = 0; i <= 15; i++)
{
rpush64(vpop64());
}
}
void vm_lifter::pop_vr64(handler_t handler)
{
//builder.CreateStore(vpop64(), builder.CreatePointerCast(vregs[handler.data], builder.getInt64Ty()->getPointerTo()));
vregs[handler.data] = vpop64();
}
void vm_lifter::pop_vr32(handler_t handler)
{
//builder.CreateStore(vpop32(), builder.CreatePointerCast(vregs[handler.data], builder.getInt32Ty()->getPointerTo()));
vregs[handler.data] = vpop32();
}
void vm_lifter::push_vr64(handler_t handler)
{
//vpush64(builder.CreateLoad(builder.getInt64Ty(), builder.CreatePointerCast(vregs[handler.data], builder.getInt64Ty()->getPointerTo())));
vpush64(vregs[handler.data]);
}
void vm_lifter::push_vr32(handler_t handler)
{
//vpush32(builder.CreateLoad(builder.getInt32Ty(), builder.CreatePointerCast(vregs[handler.data], builder.getInt32Ty()->getPointerTo())));
vpush32(vregs[handler.data]);
}
void vm_lifter::push_vsp()
{
vpush64(builder.CreatePtrToInt(vsp, builder.getInt64Ty()));
}
void vm_lifter::pop_vsp()
{
vsp = builder.CreateIntToPtr(vpop64(), builder.getInt8Ty()->getPointerTo());
}
void vm_lifter::push_64(handler_t handler)
{
vpush64(ConstantInt::get(builder.getInt64Ty(), handler.data));
}
void vm_lifter::push_32(handler_t handler)
{
vpush32(ConstantInt::get(builder.getInt32Ty(), *(uint32_t*)&handler.data));
}
llvm::Value* vm_lifter::calc_zero_flag(llvm::Value* result)
{
llvm::Value* cmp = builder.CreateICmpEQ(builder.CreateSExt(result, builder.getInt64Ty()), builder.getInt64(0), "zf");
return builder.CreateSExt(cmp, builder.getInt64Ty(), "zf_ext");
}
void vm_lifter::sub_64(handler_t handler)
{
llvm::Value* first = vpop64();
llvm::Value* second = vpop64();
llvm::Value* result = builder.CreateSub(second, first, "sub_64_result");
vpush64(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::sub_32(handler_t handler)
{
llvm::Value* first = vpop32();
llvm::Value* second = vpop32();
llvm::Value* result = builder.CreateSub(second, first, "sub_32_result");
vpush32(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::add_64(handler_t handler)
{
llvm::Value* first = vpop64();
llvm::Value* second = vpop64();
llvm::Value* result = builder.CreateAdd(second, first, "add_64_result");
vpush64(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::add_32()
{
llvm::Value* first = vpop32();
llvm::Value* second = vpop32();
llvm::Value* result = builder.CreateAdd(second, first, "add_32_result");
vpush32(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::write_32()
{
llvm::Value* address = vpop64();
llvm::Value* value = vpop32();
llvm::Value* pointer = builder.CreateIntToPtr(address, builder.getInt32Ty()->getPointerTo());
builder.CreateStore(value, pointer);
}
void vm_lifter::load_32()
{
llvm::Value* address = vpop64();
llvm::Value* pointer = builder.CreateIntToPtr(address, builder.getInt32Ty()->getPointerTo());
llvm::Value* value = builder.CreateLoad(builder.getInt32Ty(), pointer);
vpush32(value);
}
void vm_lifter::load_64()
{
llvm::Value* address = vpop64();
llvm::Value* pointer = builder.CreateIntToPtr(address, builder.getInt64Ty()->getPointerTo());
llvm::Value* value = builder.CreateLoad(builder.getInt64Ty(), pointer);
vpush64(value);
}
void vm_lifter::or_32()
{
llvm::Value* first = vpop32();
llvm::Value* second = vpop32();
llvm::Value* result = builder.CreateOr(second, first, "or_32_result");
vpush32(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::and_32()
{
llvm::Value* first = vpop32();
llvm::Value* second = vpop32();
llvm::Value* result = builder.CreateAnd(second, first, "and_32_result");
vpush32(result);
vpush64(calc_zero_flag(result));
}
void vm_lifter::xor_32()
{
llvm::Value* first = vpop32();
llvm::Value* second = vpop32();
llvm::Value* result = builder.CreateXor(second, first, "xor_32_result");
vpush32(result);
vpush64(calc_zero_flag(result));
}
llvm::Value* vm_lifter::jnz()
{
llvm::Value* zf = vpop64();
llvm::Value* condition = builder.CreateICmpNE(zf, builder.getInt64(0), "zf_cond");
return condition;
}