-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
77 lines (58 loc) · 2.05 KB
/
app.cpp
File metadata and controls
77 lines (58 loc) · 2.05 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
#include "./Config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <limits.h>
#include <time.h>
#include "./Include/Assert.h"
#include "./Include/Defines.h"
#include "./Processor/Include/Constants.h"
#include "./Processor/Include/TechInfo.h"
#include "./Processor/Include/CheckFile.h"
#include "./Include/IR.h"
#include "./Include/Translator.h"
const char FILENAME_INPUT_DEFAULT[] = "./Source.exe";
const char* FILENAME_INPUT = nullptr;
#define GetTime(cmds) \
{ \
clock_t timer_start = clock(); \
cmds \
clock_t timer_end = clock(); \
double seconds = ((double) (timer_end - timer_start)); \
printf("%lf seconds\n", seconds); \
}
#define MAX(a, b) ((a) >= (b) ? a : b)
int Execute_x86Code(const x86Buf* x86_buf)
{
ASSERT(x86_buf != nullptr);
int mprotect_res = mprotect(x86_buf->buf, x86_buf->prog_size, PROT_EXEC);
ASSERT(mprotect_res == 0);
void (*exec_code)() = (void (*)())(x86_buf->buf);
// for (int i = 0; i < 10000; i++)
exec_code();
mprotect_res = mprotect(x86_buf->buf, x86_buf->buf_size, PROT_READ | PROT_WRITE);
ASSERT(mprotect_res == 0);
return 1;
}
int main(const int argc, const char** argv)
{
if (!CheckFile(argc, argv, &FILENAME_INPUT))
FILENAME_INPUT = FILENAME_INPUT_DEFAULT;
FILE* bin_file = fopen(FILENAME_INPUT, "rb");
ASSERT(bin_file != nullptr);
IR ir = {};
IRCtor(&ir, bin_file);
fclose(bin_file);
PatchIR(&ir);
IRDump(&ir);
x86Buf x86_buf = {};
x86BufCtor(&x86_buf);
IRTranslate_x86(&ir, &x86_buf);
x86BufDump(&x86_buf, MAX(256, x86_buf.prog_size+1));
// GetTime(
Execute_x86Code(&x86_buf);
// )
IRDtor(&ir);
x86BufDtor(&x86_buf);
return 1;
}