-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindLoop.cpp
144 lines (107 loc) · 3.04 KB
/
findLoop.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
/* saraswati mahabhaage vidye kamalalochani vidyarupe visalakshi vidyam dehi namostutey */
#include "pch.h"
#include "dr_api.h"
#include "drmgr.h"
#include <unordered_map>
#include <string>
constexpr auto ITER_LIMIT = 200;
/* Base address */
static app_pc baseAddress;
/* Blocks */
static std::unordered_map<DWORD_PTR, size_t> blocks;
static std::string target_name;
static void
event_exit(void);
static dr_emit_flags_t
event_app_instruction(void* drcontext, void* tag, instrlist_t* bb, instr_t* inst,
bool for_trace, bool translating, void* user_data);
static void
ProcessBlock(DWORD_PTR);
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char* argv[])
{
UNREFERENCED_PARAMETER(id);
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
dr_set_client_name("EDLF",
" ");
drmgr_init();
target_name = dr_get_application_name();
const auto mainModule = dr_get_main_module();
if (mainModule != nullptr)
baseAddress = mainModule->start;
dr_free_module_data(mainModule);
/* also give notification to stderr */
if (dr_is_notify_on())
{
dr_enable_console_printing();
}
/* register events */
dr_register_exit_event(event_exit);
drmgr_register_bb_instrumentation_event(nullptr, event_app_instruction, nullptr);
}
static void
event_exit(void)
{
std::vector<DWORD_PTR> suspiciousBlocks;
for (const auto block : blocks)
{
if (block.second > ITER_LIMIT) // more than ITER_LIMIT iteration
{
// block.first: address
suspiciousBlocks.emplace_back(block.first);
}
}
if (!suspiciousBlocks.empty()) {
std::string idaScript = "";
for (const auto& block : suspiciousBlocks)
{
idaScript += std::to_string(block) + ", ";
}
const char* x = idaScript.c_str();
dr_printf(x);
}
drmgr_exit();
}
static dr_emit_flags_t
event_app_instruction(void* drcontext, void* tag, instrlist_t* bb, instr_t* inst,
bool for_trace, bool translating, void* user_data)
{
UNREFERENCED_PARAMETER(user_data);
UNREFERENCED_PARAMETER(translating);
UNREFERENCED_PARAMETER(for_trace);
drmgr_disable_auto_predication(drcontext, bb);
const auto mod = dr_lookup_module(dr_fragment_app_pc(tag));
if (mod != nullptr)
{
const auto mainModule = (mod->start == baseAddress);
dr_free_module_data(mod);
if (!mainModule)
{
return DR_EMIT_DEFAULT;
}
}
if (!drmgr_is_first_instr(drcontext, inst))
return DR_EMIT_DEFAULT;
const auto instrFirst = instrlist_first(bb);
if (instr_is_return(instrFirst))
return DR_EMIT_DEFAULT;
// check if "application (non-meta)" instruction
if (!instr_is_app(instrFirst))
return DR_EMIT_DEFAULT;
const auto instructionAddress = reinterpret_cast<DWORD_PTR>(instr_get_app_pc(instrFirst)) - DWORD_PTR(baseAddress);
dr_insert_clean_call(drcontext, bb, instrlist_first_app(bb), static_cast<void*>(ProcessBlock),
false /* save fpstate */, 1, OPND_CREATE_INTPTR(instructionAddress));
return DR_EMIT_DEFAULT;
}
static void ProcessBlock(DWORD_PTR instructionAddress)
{
if (blocks.find(instructionAddress) == blocks.end())
{
blocks[instructionAddress] = 1;
}
else
{
blocks[instructionAddress]++;
}
}