Skip to content

Commit 5fb5151

Browse files
committed
WIP testing
1 parent 819bf18 commit 5fb5151

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lifter/lifter.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,77 @@ void InitFunction_and_LiftInstructions(const ZyanU64 runtime_address,
238238
return;
239239
}
240240

241+
int testInit() {
242+
llvm::LLVMContext context;
243+
std::string mod_name = "my_lifting_module";
244+
llvm::Module lifting_module = llvm::Module(mod_name.c_str(), context);
245+
246+
std::vector<llvm::Type*> argTypes;
247+
argTypes.push_back(llvm::Type::getInt64Ty(context));
248+
argTypes.push_back(llvm::Type::getInt64Ty(context));
249+
argTypes.push_back(llvm::Type::getInt64Ty(context));
250+
argTypes.push_back(llvm::Type::getInt64Ty(context));
251+
argTypes.push_back(llvm::Type::getInt64Ty(context));
252+
argTypes.push_back(llvm::Type::getInt64Ty(context));
253+
argTypes.push_back(llvm::Type::getInt64Ty(context));
254+
argTypes.push_back(llvm::Type::getInt64Ty(context));
255+
argTypes.push_back(llvm::Type::getInt64Ty(context));
256+
argTypes.push_back(llvm::Type::getInt64Ty(context));
257+
argTypes.push_back(llvm::Type::getInt64Ty(context));
258+
argTypes.push_back(llvm::Type::getInt64Ty(context));
259+
argTypes.push_back(llvm::Type::getInt64Ty(context));
260+
argTypes.push_back(llvm::Type::getInt64Ty(context));
261+
argTypes.push_back(llvm::Type::getInt64Ty(context));
262+
argTypes.push_back(llvm::Type::getInt64Ty(context));
263+
argTypes.push_back(llvm::PointerType::get(context, 0));
264+
argTypes.push_back(llvm::PointerType::get(context, 0)); // temp fix TEB
265+
266+
auto functionType =
267+
llvm::FunctionType::get(llvm::Type::getInt64Ty(context), argTypes, 0);
268+
269+
const std::string function_name = "main";
270+
auto function =
271+
llvm::Function::Create(functionType, llvm::Function::ExternalLinkage,
272+
function_name.c_str(), lifting_module);
273+
const std::string block_name = "entry";
274+
auto bb = llvm::BasicBlock::Create(context, block_name.c_str(), function);
275+
276+
llvm::InstSimplifyFolder Folder(lifting_module.getDataLayout());
277+
llvm::IRBuilder<llvm::InstSimplifyFolder> builder =
278+
llvm::IRBuilder<llvm::InstSimplifyFolder>(bb, Folder);
279+
280+
// auto RegisterList = InitRegisters(builder, function, runtime_address);
281+
282+
lifterClass* main = new lifterClass(builder, 0x133700);
283+
// main->InitRegisters(function, );
284+
// main->blockInfo = BBInfo(0x133700, bb);
285+
286+
auto tester = Tester(main, true);
287+
std::vector<uint8_t> bytes = {0x48, 0x01, 0xc8};
288+
tester.setRegister(ZYDIS_REGISTER_RAX, 5);
289+
tester.setRegister(ZYDIS_REGISTER_RCX, 5);
290+
tester.disassembleBytesAndLift(bytes);
291+
auto a = tester.getRegister(ZYDIS_REGISTER_RAX);
292+
tester.getRegister(ZYDIS_REGISTER_RCX);
293+
294+
if (auto a_c = dyn_cast<ConstantInt>(a)) {
295+
return !(a_c->equalsInt(10));
296+
}
297+
return 1;
298+
}
299+
300+
// #define TEST
301+
241302
int main(int argc, char* argv[]) {
242303
vector<string> args(argv, argv + argc);
243304
argparser::parseArguments(args);
244305
timer::startTimer();
306+
307+
#ifdef MERGEN_TEST
308+
if (1 == 1)
309+
return testInit();
310+
#endif
311+
245312
// use parser
246313
if (args.size() < 3) {
247314
cerr << "Usage: " << args[0] << " <filename> <startAddr>" << endl;

lifter/test_instructions.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
#include "lifterClass.h"
3+
#include <Zydis/Decoder.h>
4+
#include <Zydis/DecoderTypes.h>
5+
#include <Zydis/Disassembler.h>
6+
#include <Zydis/Register.h>
7+
8+
class Tester {
9+
public:
10+
ZydisDecoder decoder;
11+
lifterClass* lifter;
12+
13+
Tester(lifterClass* lifter, bool is64Bit = true) : lifter(lifter) {
14+
15+
ZydisDecoderInit(&decoder,
16+
is64Bit ? ZYDIS_MACHINE_MODE_LONG_64
17+
: ZYDIS_MACHINE_MODE_LEGACY_32,
18+
is64Bit ? ZYDIS_STACK_WIDTH_64 : ZYDIS_STACK_WIDTH_32);
19+
}
20+
21+
Value* getRegister(ZydisRegister reg) {
22+
auto val = lifter->GetRegisterValue(reg);
23+
printvalueforce(val);
24+
return val;
25+
}
26+
27+
void setRegister(ZydisRegister reg, uint64_t value) {
28+
lifter->SetRegisterValue(reg, lifter->builder.getInt64(value));
29+
}
30+
31+
Value* getMemory(ZydisRegister reg) {
32+
33+
auto val = lifter->GetRegisterValue(reg);
34+
return val;
35+
}
36+
37+
void setMemory(ZydisRegister reg, uint64_t value) {
38+
lifter->SetRegisterValue(reg, lifter->builder.getInt64(value));
39+
}
40+
41+
void disassembleBytesAndLift(const std::vector<uint8_t>& bytes) {
42+
43+
ZydisDecoderDecodeFull(&decoder, bytes.data(), 15, &(lifter->instruction),
44+
lifter->operands);
45+
46+
lifter->liftInstructionSemantics();
47+
}
48+
};

0 commit comments

Comments
 (0)