-
Notifications
You must be signed in to change notification settings - Fork 6
/
xhexrays.hpp
237 lines (199 loc) · 6.3 KB
/
xhexrays.hpp
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
/*
IDASDK extension library (c) Elias Bachaalany.
Hexrays utilities
*/
#pragma once
#include <algorithm>
#include <map>
#include <memory>
#include <hexrays.hpp>
#include "xkernwin.hpp"
//----------------------------------------------------------------------------------
inline update_state_ah_t hexrays_default_enable_for_vd_expr = FO_ACTION_UPDATE([],
auto vu = get_widget_vdui(widget);
return (vu == nullptr) ? AST_DISABLE_FOR_WIDGET
: vu->item.citype == VDI_EXPR ? AST_ENABLE : AST_DISABLE;
);
inline update_state_ah_t hexrays_default_enable_for_vd = FO_ACTION_UPDATE([],
auto vu = get_widget_vdui(widget);
return vu == nullptr ? AST_DISABLE_FOR_WIDGET : AST_ENABLE;
);
//----------------------------------------------------------------------------------
class hexrays_ctreeparent_visitor_t : public ctree_parentee_t
{
private:
std::map<const citem_t*, const citem_t*> parent;
std::map<const ea_t, const citem_t*> ea2item;
public:
int idaapi visit_expr(cexpr_t* e) override
{
ea2item[e->ea] = parent[e] = parent_expr();
return 0;
}
int idaapi visit_insn(cinsn_t* ins) override
{
parent[ins] = parent_insn();
return 0;
}
const citem_t* parent_of(const citem_t* item)
{
return parent[item];
}
const citem_t* by_ea(ea_t ea) const
{
auto p = ea2item.find(ea);
return p == std::end(ea2item) ? nullptr : p->second;
}
bool is_ancestor_of(const citem_t* parent, const citem_t* item)
{
while (item != nullptr)
{
item = parent_of(item);
if (item == parent)
return true;
}
return false;
}
};
using hexrays_ctreeparent_visitor_ptr_t = std::unique_ptr<hexrays_ctreeparent_visitor_t>;
//----------------------------------------------------------------------------------
inline ea_t get_selection_range(
TWidget* widget,
ea_t* end_ea = nullptr,
int widget_type = BWN_DISASM)
{
ea_t ea1 = BADADDR, ea2 = BADADDR;
do
{
if (widget == nullptr || (widget_type != -1 && get_widget_type(widget) != widget_type))
break;
if (!read_range_selection(widget, &ea1, &ea2))
{
ea1 = get_screen_ea();
if (ea1 == BADADDR)
break;
ea2 = next_head(ea1, BADADDR);
if (ea2 == BADADDR)
ea2 = ea1 + 1;
}
} while (false);
if (end_ea != nullptr)
*end_ea = ea2;
return ea1;
}
//----------------------------------------------------------------------------------
inline const cinsn_t* hexrays_get_stmt_insn(
cfunc_t* cfunc,
const citem_t* ui_item,
hexrays_ctreeparent_visitor_ptr_t *ohelper = nullptr)
{
auto func_body = &cfunc->body;
const citem_t* item = ui_item;
const citem_t* stmt_item;
hexrays_ctreeparent_visitor_t* helper = nullptr;
if (ohelper != nullptr)
{
// Start a new helper
if (*ohelper == nullptr)
{
helper = new hexrays_ctreeparent_visitor_t();
helper->apply_to(func_body, nullptr);
ohelper->reset(helper);
}
else
{
helper = ohelper->get();
}
}
auto get_parent = [func_body, &helper](const citem_t* item)
{
return helper == nullptr ? func_body->find_parent_of(item)
: helper->parent_of(item);
};
// Get the top level statement from this item
for (stmt_item = item;
item != nullptr && item->is_expr();
item = get_parent(item))
{
stmt_item = item;
}
// ...then the actual instruction item
if (stmt_item->is_expr())
stmt_item = get_parent(stmt_item);
return (const cinsn_t*)stmt_item;
}
//----------------------------------------------------------------------------------
inline bool hexrays_get_stmt_block_pos(
cfunc_t* cfunc,
const citem_t* stmt_item,
cblock_t** p_cblock,
cblock_t::iterator* p_pos,
hexrays_ctreeparent_visitor_t* helper = nullptr)
{
auto func_body = &cfunc->body;
auto cblock_insn = (cinsn_t*)(
helper == nullptr ? func_body->find_parent_of(stmt_item)
: helper->parent_of(stmt_item));
if (cblock_insn == nullptr || cblock_insn->op != cit_block)
return false;
cblock_t* cblock = cblock_insn->cblock;
for (auto p = cblock->begin(); p != cblock->end(); ++p)
{
if (&*p == stmt_item)
{
*p_pos = p;
*p_cblock = cblock;
return true;
}
}
return false;
}
//----------------------------------------------------------------------------------
inline bool hexrays_are_acenstor_of(
hexrays_ctreeparent_visitor_t* h,
cinsnptrvec_t& inst,
citem_t* item)
{
for (auto parent : inst)
{
if (h->is_ancestor_of(parent, item))
return true;
}
return false;
};
//----------------------------------------------------------------------------------
inline void hexrays_keep_lca_cinsns(
cfunc_t* cfunc,
hexrays_ctreeparent_visitor_t* helper,
cinsnptrvec_t& bulk_list)
{
cinsnptrvec_t new_list;
while (!bulk_list.empty())
{
auto item = bulk_list.back();
bulk_list.pop_back();
if (!hexrays_are_acenstor_of(helper, bulk_list, item) && !hexrays_are_acenstor_of(helper, new_list, item))
new_list.push_back(item);
}
new_list.swap(bulk_list);
}
//----------------------------------------------------------------------------------
inline void hexrays_find_expr(
cfuncptr_t func,
std::function<int(cexpr_t*)> cb,
int flags = CV_FAST,
citem_t* parent = nullptr)
{
struct visitor_wrapper : public ctree_visitor_t
{
std::function<int(cexpr_t*)> cb;
visitor_wrapper(int flags, std::function<int(cexpr_t*)> cb)
: ctree_visitor_t(flags), cb(cb) {}
virtual int idaapi visit_expr(cexpr_t* expr)
{
return cb(expr);
}
};
visitor_wrapper v(flags, cb);
v.apply_to(&func->body, parent);
}