-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathplugin_config.cpp
452 lines (410 loc) · 13.5 KB
/
plugin_config.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* @file idaplugin/plugin_config.cpp
* @brief Module deals with GhidraDec plugin configuration.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/
#include <fstream>
#include <iostream>
#include <json/json.h>
#define JSONCPP_STRING std::string
#include "retdec/utils/file_io.h"
#include "retdec/utils/string.h"
#include "plugin_config.h"
#include "decompiler.h"
#include "sleighinterface.h"
#include "idaplugin.h"
namespace {
const std::string JSON_ghidraPath = "ghidraPath";
const std::string JSON_viewFeatures = "viewFeatures";
const std::string JSON_cacheSize = "cacheSize";
const std::string JSON_maxPayload = "maxPayload";
const std::string JSON_timeout = "timeout";
const std::string JSON_cmtLevel = "cmtLevel";
const std::string JSON_alysChecks = "alysChecks";
const std::string JSON_dispChecks = "dispChecks";
const std::string JSON_maxChars = "maxChars";
const std::string JSON_numChars = "numChars";
const std::string JSON_comStyle = "comStyle";
const std::string JSON_intFormat = "intFormat";
} // anonymous namespace
namespace idaplugin {
/**
* Get root value from the provided JSON string.
* @param[in] json String containing entire JSON file.
* @param[out] root JSON root value to get from config.
* @param[in] silent Should the function throw warning at user if
* something goes wrong?
* @return @c False is @a root value read ok, @c true otherwise.
*/
bool getConfigRootFromString(
const std::string& json,
Json::Value& root,
bool silent = true)
{
std::istringstream input(json);
Json::CharReaderBuilder builder;
JSONCPP_STRING errors;
bool success = Json::parseFromStream(builder, input, &root, &errors);
if (!success || root.isNull() || !root.isObject())
{
if ((!silent) && (errors.size() != 0))
{
WARNING_GUI("Failed to parse JSON content.\n" << errors << "\n");
}
return true;
}
return false;
}
/**
* Get root value from the provided JSON file.
* @param[in] file JSON file.
* @param[out] root JSON root value to get from config.
* @return @c False is @a root value read ok, @c true otherwise.
*/
bool getConfigRootFromFile(
const std::string& file,
Json::Value& root)
{
std::ifstream jsonFile(file, std::ios::in | std::ios::binary);
if (!jsonFile)
{
return true;
}
std::string jsonContent;
jsonFile.seekg(0, std::ios::end);
jsonContent.resize(jsonFile.tellg());
jsonFile.seekg(0, std::ios::beg);
jsonFile.read(&jsonContent[0], jsonContent.size());
jsonFile.close();
return getConfigRootFromString(jsonContent, root);
}
/**
* Read provided JSON file into plugins's global information.
* @param rdgi Plugin's global information.
* @return @c False is @a config read ok, @c true otherwise.
*/
bool readConfigFile(RdGlobalInfo& rdgi)
{
Json::Value root;
if (getConfigRootFromFile(rdgi.pluginConfigFile.getPath(), root))
{
return true;
}
rdgi.ghidraPath = root.get(JSON_ghidraPath, "").asString();
rdgi.viewFeatures = root.get(JSON_viewFeatures, 1 | 2).asUInt();
rdgi.cacheSize = (sval_t)root.get(JSON_cacheSize, 10).asUInt64();
rdgi.maxPayload = (sval_t)root.get(JSON_maxPayload, 50).asUInt64();
rdgi.timeout = (sval_t)root.get(JSON_timeout, 30).asUInt64();
rdgi.cmtLevel = (sval_t)root.get(JSON_cmtLevel, 20).asUInt64();
rdgi.alysChecks = root.get(JSON_alysChecks, 1 | 4 | 8 | 16 | 32).asUInt();
rdgi.dispChecks = root.get(JSON_dispChecks, 4 | 8 | 128 | 256 | 1024).asUInt();
rdgi.maxChars = root.get(JSON_maxChars, 100).asUInt();
rdgi.numChars = root.get(JSON_numChars, 2).asUInt();
rdgi.comStyle = root.get(JSON_comStyle, 0).asInt();
rdgi.intFormat = root.get(JSON_intFormat, 2).asInt();
netnode nn("GhidraConfig");
qstring qs;
;
if (netnode_qvalstr(nn, &qs) != -1) {
int idx = 0;
std::vector<std::string> split({ std::string() });
const uchar delim = ';';
std::for_each(qs.begin(), qs.end(), [&idx, &split, delim](uchar ch) { if (ch == delim) { idx++; split.push_back(std::string()); } else if (ch != 0) split[idx].push_back(ch); });
rdgi.customPspec = split[0];
rdgi.customCspec = split[1];
rdgi.customSlafile = split[2];
rdgi.customCallStyle = split[3];
}
return false;
}
/**
* Save plugin's configuration into provided JSON file.
* File content is rewritten.
* @param rdgi Plugin's global information.
*/
void saveConfigTofile(RdGlobalInfo& rdgi)
{
Json::Value root;
if (getConfigRootFromFile(rdgi.pluginConfigFile.getPath(), root))
{
// Problem when reading config -- does not matter, we use empty root.
}
root[JSON_ghidraPath] = rdgi.ghidraPath;
root[JSON_viewFeatures] = rdgi.viewFeatures;
root[JSON_cacheSize] = rdgi.cacheSize;
root[JSON_maxPayload] = rdgi.maxPayload;
root[JSON_timeout] = rdgi.timeout;
root[JSON_cmtLevel] = rdgi.cmtLevel;
root[JSON_alysChecks] = rdgi.alysChecks;
root[JSON_dispChecks] = rdgi.dispChecks;
root[JSON_maxChars] = rdgi.maxChars;
root[JSON_numChars] = rdgi.numChars;
root[JSON_comStyle] = rdgi.comStyle;
root[JSON_intFormat] = rdgi.intFormat;
netnode nn("GhidraConfig", 0, true);
nn.set((rdgi.customPspec + ";" + rdgi.customCspec + ";" + rdgi.customSlafile + ";" + rdgi.customCallStyle).c_str());
Json::StreamWriterBuilder writer;
writer.settings_["commentStyle"] = "All";
std::ofstream jsonFile(rdgi.pluginConfigFile.getPath().c_str());
jsonFile << Json::writeString(writer, root);
}
int idaapi sleighCB(int button_code, form_actions_t& fa)
{
RdGlobalInfo* decompInfo = (RdGlobalInfo*)fa.get_ud();
qstring formGhidraDecPluginSettings =
"BUTTON YES Compile\n"
"GhidraDec Sleigh Pcode Snippet Compiler\n"
"\n"
"\n"
"Enter Pcode snippet here and use the compile button to see the output.\n"
"\n"
"<Editor:t1:0:40::>\n"
"<Output:t2:0:40::>\n"
"\n";
textctrl_info_t tinfo, toutinfo;
tinfo.flags |= TXTF_AUTOINDENT | TXTF_ACCEPTTABS | TXTF_LINENUMBERS | TXTF_FIXEDFONT;
tinfo.tabsize = 2;
tinfo.text = "";
toutinfo.flags |= TXTF_READONLY | TXTF_FIXEDFONT;
toutinfo.tabsize = 0;
toutinfo.text = "";
int ok;
std::string pspec, cspec, sleighfilename;
if (decompInfo->customSlafile.empty() &&
decompInfo->pm->canDecompileInput() &&
detectProcCompiler(decompInfo, pspec, cspec, sleighfilename)) {
}
if (!decompInfo->customSlafile.empty()) sleighfilename = decompInfo->customSlafile;
do {
if (tinfo.text.size() != 0) {
try {
toutinfo.text = DecompInterface::compilePcodeSnippet(sleighfilename,
tinfo.text.c_str(), std::vector<std::pair<std::string, int>>(), std::vector<std::pair<std::string, int>>()).c_str();
} catch (DecompError& err) {
toutinfo.text = err.explain.c_str();
}
}
ok = ask_form(formGhidraDecPluginSettings.c_str(), &tinfo, &toutinfo);
} while (ok == ASKBTN_YES);
if (ok == 0) {
// ESC or CANCEL
return true;
}
return false;
}
int idaapi displayCB(int button_code, form_actions_t& fa)
{
RdGlobalInfo* decompInfo = (RdGlobalInfo*)fa.get_ud();
sval_t cmtLevel = decompInfo->cmtLevel, maxChars = decompInfo->maxChars, numChars = decompInfo->numChars;
int comStyle = decompInfo->comStyle, intFormat = decompInfo->intFormat;
ushort dispChecks = decompInfo->dispChecks;
qstring formGhidraDecPluginSettings =
"GhidraDec Plugin Settings\n"
"\n"
"\n"
"Settings will be permanently stored and you will not have to fill them each time you run decompilation.\n"
"\n"
"<Display printing of type casts:C>\n"
"<Display EOL comments:C>\n"
"<Display Header comment:C>\n"
"<Display Line Numbers (future):C>\n"
"<Display Namespaces (future):C>\n"
"<Display PLATE comments:C>\n"
"<Display POST comments:C>\n"
"<Display PRE comments:C>\n"
"<Display Warning comments:C>\n"
"<Print 'NULL' for null pointers:C>\n"
"<Print calling convention name:C>>\n"
"<Comment line indent level:D::::>\n"
"<Maximum characters in a code line:D::::>\n"
"<Number of characters per indent level:D::::>\n"
"<Comment style:b::::>\n"
"<Integer format:b::::>\n"
"\n";
qstrvec_t cstyles, iformats;
cstyles.push_back("/* C-style comments */");
cstyles.push_back("// C++-style comments");
iformats.push_back("Force Hexadecimal");
iformats.push_back("Force decimal");
iformats.push_back("Best Fit");
int ok = ask_form(formGhidraDecPluginSettings.c_str(),
&dispChecks,
&cmtLevel, &maxChars, &numChars,
&cstyles, &comStyle, &iformats, &intFormat
);
if (ok != ASKBTN_YES) {
// ESC or CANCEL
return true;
} else {
decompInfo->cmtLevel = cmtLevel;
decompInfo->dispChecks = dispChecks;
decompInfo->maxChars = maxChars;
decompInfo->numChars = numChars;
decompInfo->comStyle = comStyle;
decompInfo->intFormat = intFormat;
}
return false;
}
/**
* Present plugin configuration form to developer.
* @param rdgi Plugin's global information.
* @return @c True if cancelled, @c false otherwise.
*/
bool askUserToConfigurePlugin(RdGlobalInfo& rdgi)
{
char cGhidraPath[QMAXPATH], cPspecPath[QMAXPATH],
cCspecPath[QMAXPATH], cSlaPath[QMAXPATH];
if (rdgi.ghidraPath.empty())
{
std::string pattern = rdgi.ghidraPath;
qstrncpy(cGhidraPath, pattern.c_str(), QMAXPATH);
}
else
{
qstrncpy(cGhidraPath, rdgi.ghidraPath.c_str(), QMAXPATH);
}
qstring formGhidraDecPluginSettings =
//"BUTTON NO Refresh Prototype (must if Compiler Spec changed)\n"
"GhidraDec Plugin Settings\n"
"%*\n"
"\n"
"Settings will be permanently stored and you will not have to fill them each time you run decompilation.\n"
"\n"
"Path to %A (unnecessary if it is in the system PATH):\n"
"<Ghidra Folder:F1::60::>\n"
"<Show Control-Flow Graph View:C>\n"
"<Use Extra Parameter Identification Phase:C>>\n"
"<Cache Size (Functions):D::::>\n"
"<Decompiler Max-Payload (MBytes):D::::>\n"
"<Decompiler Timeout (seconds):D::::>\n"
"<Eliminate unreachable code:C>\n"
"<Ignore unimplemented instructions:C>\n"
"<Infer constant pointers:C>\n"
"<Respect readonly flags:C>\n"
"<Simplify extended integer operations:C>\n"
"<Simplify predication:C>\n"
"<Use inplace assignment operators:C>>\n"
"<Display Options:B::::>\n"
"<Sleigh Pcode Snippet Compiler:B::::>\n"
"\n"
"Following Settings are per database and stored in the .idb/.i64 file (IDA reports %A-bit processor %A).\n"
"<Processor Spec:f2::60::>\n"
"<Compiler Spec:f3::60::>\n"
"<Sleigh file:f4::60::>\n"
"<Prototype Evaluation:b::::>\n"
"\n";
int curProto = -1;
std::string cspec, pspec, sleighfilename, protoeval;
if ((rdgi.customCspec.empty() || rdgi.customPspec.empty() || rdgi.customSlafile.empty()) &&
rdgi.pm->canDecompileInput() &&
detectProcCompiler(&rdgi, pspec, cspec, sleighfilename)) {
}
if (!rdgi.customCspec.empty()) cspec = rdgi.customCspec;
if (!rdgi.customPspec.empty()) pspec = rdgi.customPspec;
if (!rdgi.customSlafile.empty()) sleighfilename = rdgi.customSlafile;
qstrncpy(cPspecPath, pspec.c_str(), QMAXPATH);
qstrncpy(cCspecPath, cspec.c_str(), QMAXPATH);
qstrncpy(cSlaPath, sleighfilename.c_str(), QMAXPATH);
int ok;
sval_t cacheSize = rdgi.cacheSize, maxPayload = rdgi.maxPayload, timeout = rdgi.timeout;
ushort viewFeatures = rdgi.viewFeatures;
ushort alysChecks = rdgi.alysChecks;
std::vector<std::string> vec;
do {
qstrvec_t protoTypes;
vec.clear();
int defIdx = -1;
if (!cspec.empty()) DecompInterface::getProtoEvals(cspec, vec, &defIdx);
if (!rdgi.customCallStyle.empty()) protoeval = rdgi.customCallStyle;
else protoeval = ccToStr(inf_cc_cm, 0, true);
vec.insert(vec.begin(), "default");
vec.insert(vec.begin(), "unknown");
for (size_t i = 0; i < vec.size(); i++) {
if (strcmp(protoeval.c_str(), vec[i].c_str()) == 0) curProto = (int)i;
protoTypes.push_back(vec[i].c_str());
}
if (curProto == -1) curProto = 0;
ok = ask_form(formGhidraDecPluginSettings.c_str(),
&rdgi,
rdgi.ghidraPath.c_str(),
cGhidraPath, &viewFeatures,
&cacheSize, &maxPayload, &timeout,
&alysChecks, &displayCB, &sleighCB,
inf_is_32bit() ? (inf_is_64bit() ? "32/64" : "32") : (inf_is_64bit() ? "64" : "?"),
inf_procname.c_str(),
cPspecPath, cCspecPath, cSlaPath,
&protoTypes, &curProto
);
if (ok == ASKBTN_YES) {
if (cspec != cCspecPath) {
cspec = cCspecPath;
warning("Since the compiler specification file was changed, please confirm the prototype in use");
continue;
}
}
} while (false);
if (ok != ASKBTN_YES)
{
// ESC or CANCEL
return true;
}
else
{
rdgi.ghidraPath = cGhidraPath;
rdgi.viewFeatures = viewFeatures;
rdgi.cacheSize = cacheSize;
rdgi.maxPayload = maxPayload;
rdgi.timeout = timeout;
rdgi.alysChecks = alysChecks;
//these are saved and loaded through the IDB
rdgi.customPspec = cPspecPath;
rdgi.customCspec = cCspecPath;
rdgi.customSlafile = cSlaPath;
rdgi.customCallStyle = curProto == -1 ? "" : vec[curProto];
}
return false;
}
/**
* @return @c True if cancelled, @c false otherwise.
*/
bool pluginConfigurationMenu(RdGlobalInfo& rdgi)
{
bool canceled = askUserToConfigurePlugin(rdgi);
if (!canceled)
{
saveConfigTofile(rdgi);
}
return canceled;
}
/**
* @return @c False if success, @c true otherwise.
*/
bool addConfigurationMenuOption(RdGlobalInfo& rdgi)
{
char optionsActionName[] = "ghidradec:ShowOptions";
char optionsActionLabel[] = "GhidraDec plugin options...";
static show_options_ah_t show_options_ah(&rdgi);
static const action_desc_t desc = ACTION_DESC_LITERAL_PLUGMOD(
optionsActionName,
optionsActionLabel,
&show_options_ah,
rdgi.pm,
nullptr,
NULL,
-1);
if (!register_action(desc)
|| !attach_action_to_menu(
"Options/SourcePaths",
optionsActionName,
SETMENU_APP)
|| !attach_action_to_menu(
"Options/SourcePaths",
"-",
SETMENU_APP))
{
ERROR_MSG("Failed to register Options menu item for GhidraDec plugin!\n");
return true;
}
return false;
}
} // namespace idaplugin