-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMBA-Simplifier.cpp
More file actions
72 lines (59 loc) · 2.08 KB
/
MBA-Simplifier.cpp
File metadata and controls
72 lines (59 loc) · 2.08 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
#include <iostream>
#include <windows.h>
#include <cstring>
#include "simulator.h"
#include "analysis.h"
#include "InstructionSynthesizer.h"
void print_colored(const std::string& text, WORD color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
std::cout << text << std::endl;
SetConsoleTextAttribute(hConsole, 7);
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <hex>\n";
return 1;
}
std::string hex;
for (int i = 1; i < argc; i++) hex += argv[i];
auto original_code = Simulator::hex_to_bytes(hex);
if (original_code.empty()) {
std::cerr << "No code\n";
return 1;
}
Simulator sim;
RegMap init_regs = Simulator::make_random_regs();
RegMap final_regs_original;
sim.emulate(original_code, init_regs, final_regs_original);
ExecutionResult result = analyze_execution(sim, original_code, init_regs, final_regs_original);
InstructionSynthesizer synth;
auto out = synth.synthesize(result, init_regs, final_regs_original);
std::cout << "Assembly: " << out.asm_code << "\n";
std::cout << "Machine code: ";
for (auto b : out.machine_code) {
printf("%02X ", b);
}
std::cout << std::endl;
print_execution_result(result);
RegMap final_regs_synth;
init_regs = Simulator::make_random_regs();
sim.emulate(original_code, init_regs, final_regs_original);
sim.emulate(out.machine_code, init_regs, final_regs_synth);
bool success = true;
for (auto& reg : final_regs_original) {
if (final_regs_synth[reg.first] != reg.second) {
success = false;
std::cout << "Mismatch in register " << sim.reg_name(reg.first)
<< ": original=" << reg.second
<< ", synthesized=" << final_regs_synth[reg.first] << "\n";
}
}
if (success) {
print_colored("succsess.", FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}
else {
print_colored("fail.", FOREGROUND_RED | FOREGROUND_INTENSITY);
}
return 0;
}