-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathIaito.cpp
4355 lines (3704 loc) · 116 KB
/
Iaito.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "GuiCorePlugin.cpp"
#include <QCoreApplication>
#include <QDir>
#include <QJsonArray>
#include <QJsonObject>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QStringList>
#include <QVector>
#include <cassert>
#include <memory>
#include "Decompiler.h"
#include "common/AsyncTask.h"
#include "common/BasicInstructionHighlighter.h"
#include "common/Configuration.h"
#include "common/Json.h"
#include "common/R2Shims.h"
#include "common/R2Task.h"
#include "common/TempConfig.h"
#include "core/Iaito.h"
#include "plugins/PluginManager.h"
#include <r_asm.h>
#include <r_cmd.h>
#include <r_core.h>
#if R2_VERSION_NUMBER >= 50809 // reverse compatability
#define BO bo
#else
#define BO o
#endif
Q_GLOBAL_STATIC(IaitoCore, uniqueInstance)
#define R_JSON_KEY(name) static const QString name = QStringLiteral(#name)
#define CORE_LOCK() RCoreLocked core(this)
namespace RJsonKey {
R_JSON_KEY(addr);
R_JSON_KEY(addrs);
R_JSON_KEY(addr_end);
R_JSON_KEY(arrow);
R_JSON_KEY(baddr);
R_JSON_KEY(bind);
R_JSON_KEY(blocks);
R_JSON_KEY(blocksize);
R_JSON_KEY(bytes);
R_JSON_KEY(calltype);
R_JSON_KEY(cc);
R_JSON_KEY(classname);
R_JSON_KEY(code);
R_JSON_KEY(comment);
R_JSON_KEY(comments);
R_JSON_KEY(cost);
R_JSON_KEY(data);
R_JSON_KEY(description);
R_JSON_KEY(ebbs);
R_JSON_KEY(edges);
R_JSON_KEY(enabled);
R_JSON_KEY(entropy);
R_JSON_KEY(fcn_addr);
R_JSON_KEY(fcn_name);
R_JSON_KEY(fields);
R_JSON_KEY(file);
R_JSON_KEY(flags);
R_JSON_KEY(flagname);
R_JSON_KEY(format);
R_JSON_KEY(from);
R_JSON_KEY(functions);
R_JSON_KEY(graph);
R_JSON_KEY(haddr);
R_JSON_KEY(hw);
R_JSON_KEY(in_functions);
R_JSON_KEY(index);
R_JSON_KEY(jump);
R_JSON_KEY(laddr);
R_JSON_KEY(lang);
R_JSON_KEY(len);
R_JSON_KEY(length);
R_JSON_KEY(license);
R_JSON_KEY(methods);
R_JSON_KEY(name);
R_JSON_KEY(realname);
R_JSON_KEY(nargs);
R_JSON_KEY(nbbs);
R_JSON_KEY(nlocals);
R_JSON_KEY(offset);
R_JSON_KEY(opcode);
R_JSON_KEY(opcodes);
R_JSON_KEY(ordinal);
R_JSON_KEY(libname);
R_JSON_KEY(outdegree);
R_JSON_KEY(paddr);
R_JSON_KEY(path);
R_JSON_KEY(perm);
R_JSON_KEY(pid);
R_JSON_KEY(plt);
R_JSON_KEY(prot);
R_JSON_KEY(ref);
R_JSON_KEY(refs);
R_JSON_KEY(reg);
R_JSON_KEY(rwx);
R_JSON_KEY(section);
R_JSON_KEY(sections);
R_JSON_KEY(size);
R_JSON_KEY(stackframe);
R_JSON_KEY(status);
R_JSON_KEY(string);
R_JSON_KEY(strings);
R_JSON_KEY(symbols);
R_JSON_KEY(text);
R_JSON_KEY(to);
R_JSON_KEY(trace);
R_JSON_KEY(type);
R_JSON_KEY(uid);
R_JSON_KEY(vaddr);
R_JSON_KEY(value);
R_JSON_KEY(vsize);
} // namespace RJsonKey
#undef R_JSON_KEY
static void updateOwnedCharPtr(char *&variable, const QString &newValue)
{
auto data = newValue.toUtf8();
r_mem_free(variable);
variable = strdup(data.data());
}
static QString fromOwnedCharPtr(char *str)
{
QString result(str ? str : "");
r_mem_free(str);
return result;
}
RCoreLocked::RCoreLocked(IaitoCore *core)
: core(core)
{
core->coreMutex.lock();
#if R2_VERSION_NUMBER < 50609
assert(core->coreLockDepth >= 0);
core->coreLockDepth++;
if (core->coreLockDepth == 1) {
if (core->coreBed) {
r_cons_sleep_end(core->coreBed);
core->coreBed = nullptr;
}
}
#endif
}
RCoreLocked::~RCoreLocked()
{
core->coreLockDepth--;
#if R2_VERSION_NUMBER < 50609
assert(core->coreLockDepth >= 0);
if (core->coreLockDepth == 0) {
core->coreBed = r_cons_sleep_begin();
}
#endif
core->coreMutex.unlock();
}
RCoreLocked::operator RCore *() const
{
return core->core_;
}
RCore *RCoreLocked::operator->() const
{
return core->core_;
}
static void cutterREventCallback(REvent *, int type, void *user, void *data)
{
auto core = reinterpret_cast<IaitoCore *>(user);
core->handleREvent(type, data);
}
IaitoCore::IaitoCore(QObject *parent)
: QObject(parent)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
, coreMutex(QMutex::Recursive)
#else
, coreMutex()
#endif
{}
IaitoCore *IaitoCore::instance()
{
return uniqueInstance;
}
void IaitoCore::initialize(bool loadPlugins)
{
RCore *kore = iaitoPluginCore();
if (kore != nullptr) {
core_ = kore;
} else {
core_ = r_core_new();
}
#if R2_VERSION_NUMBER < 50609
r_core_task_sync_begin(&core_->tasks);
coreBed = r_cons_sleep_begin();
#endif
CORE_LOCK();
setConfig("dbg.wrap", true);
r_event_hook(core_->anal->ev, R_EVENT_ALL, cutterREventCallback, this);
#if 0
#ifdef APPIMAGE
auto prefix = QDir(QCoreApplication::applicationDirPath());
// Executable is in appdir/bin
prefix.cdUp();
qInfo() << "Setting r2 prefix =" << prefix.absolutePath() << " for AppImage.";
setConfig("dir.prefix", prefix.absolutePath());
auto pluginsDir = prefix;
if (pluginsDir.cd("share/radare2/plugins")) {
qInfo() << "Setting r2 plugins dir =" << pluginsDir.absolutePath();
setConfig("dir.plugins", pluginsDir.absolutePath());
} else {
qInfo() << "r2 plugins dir =" << pluginsDir.absolutePath() << "does not exist!";
}
#endif
#endif
if (loadPlugins) {
QString pluginsDir = QDir(Plugins()->getUserPluginsDirectory()).absolutePath();
auto iaitoPluginsDirectory = pluginsDir.toStdString();
r_lib_opendir (core->lib, iaitoPluginsDirectory.c_str());
} else {
setConfig("cfg.plugins", false);
}
if (getConfigi("cfg.plugins")) {
r_core_loadlibs(this->core_, R_CORE_LOADLIBS_ALL, nullptr);
}
r_lib_open_ptr(this->core_->lib, "uiaito", this, &uiaito_radare_plugin);
// IMPLICIT r_bin_iobind (core_->bin, core_->io);
// Otherwise r2 may ask the user for input and Iaito would freeze
setConfig("scr.interactive", false);
// Initialize graph node highlighter
bbHighlighter = new BasicBlockHighlighter();
// Initialize Async tasks manager
asyncTaskManager = new AsyncTaskManager(this);
}
IaitoCore::~IaitoCore()
{
#if R2_VERSION_NUMBER < 50609
r_cons_sleep_end(coreBed);
r_core_task_sync_end(&core_->tasks);
#endif
RCore *kore = iaitoPluginCore();
if (kore != nullptr) {
// leave qt
QCoreApplication::exit();
} else {
// r_core_free (core_);
r_cons_free();
}
delete bbHighlighter;
}
RCoreLocked IaitoCore::core()
{
return RCoreLocked(this);
}
QDir IaitoCore::getIaitoRCDefaultDirectory() const
{
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
QVector<QString> IaitoCore::getIaitoRCFilePaths(int n) const
{
QVector<QString> result;
auto filename = (n == 0) ? ".iaitorc" : ".iaitorc2";
result.push_back(QFileInfo(QDir::home(), filename).absoluteFilePath());
QStringList locations = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
for (auto &location : locations) {
result.push_back(QFileInfo(QDir(location), filename).absoluteFilePath());
}
// File in config editor is from this path
result.push_back(QFileInfo(getIaitoRCDefaultDirectory(), "rc").absoluteFilePath());
return result;
}
void IaitoCore::loadIaitoRC(int n)
{
CORE_LOCK();
const auto result = getIaitoRCFilePaths(n);
for (auto &cutterRCFilePath : result) {
auto cutterRCFileInfo = QFileInfo(cutterRCFilePath);
if (!cutterRCFileInfo.exists() || !cutterRCFileInfo.isFile()) {
continue;
}
qInfo() << "Loading " << n << " file from " << cutterRCFilePath;
r_core_cmd_file(core, cutterRCFilePath.toUtf8().constData());
}
}
void IaitoCore::loadDefaultIaitoRC()
{
CORE_LOCK();
auto cutterRCFilePath = QFileInfo(getIaitoRCDefaultDirectory(), "rc").absoluteFilePath();
const auto cutterRCFileInfo = QFileInfo(cutterRCFilePath);
if (!cutterRCFileInfo.exists() || !cutterRCFileInfo.isFile()) {
return;
}
qInfo() << "Loading initialization script from " << cutterRCFilePath;
r_core_cmd_file(core, cutterRCFilePath.toUtf8().constData());
}
void IaitoCore::loadSecondaryIaitoRC()
{
CORE_LOCK();
auto cutterRCFilePath = QFileInfo(getIaitoRCDefaultDirectory(), "rc2").absoluteFilePath();
const auto cutterRCFileInfo = QFileInfo(cutterRCFilePath);
if (!cutterRCFileInfo.exists() || !cutterRCFileInfo.isFile()) {
return;
}
qInfo() << "Loading secondary script from " << cutterRCFilePath;
r_core_cmd_file(core, cutterRCFilePath.toUtf8().constData());
}
QList<QString> IaitoCore::sdbList(QString path)
{
CORE_LOCK();
QList<QString> list = QList<QString>();
Sdb *root = sdb_ns_path(core->sdb, path.toUtf8().constData(), 0);
if (root) {
void *vsi;
ls_iter_t *iter;
ls_foreach(root->ns, iter, vsi)
{
SdbNs *nsi = (SdbNs *) vsi;
list << nsi->name;
}
}
return list;
}
using SdbListPtr = std::unique_ptr<SdbList, decltype(&ls_free)>;
static SdbListPtr makeSdbListPtr(SdbList *list)
{
return {list, ls_free};
}
QList<QString> IaitoCore::sdbListKeys(QString path)
{
CORE_LOCK();
QList<QString> list = QList<QString>();
Sdb *root = sdb_ns_path(core->sdb, path.toUtf8().constData(), 0);
if (root) {
void *vsi;
ls_iter_t *iter;
SdbListPtr l = makeSdbListPtr(sdb_foreach_list(root, false));
ls_foreach(l, iter, vsi)
{
SdbKv *nsi = (SdbKv *) vsi;
list << reinterpret_cast<char *>(nsi->base.key);
}
}
return list;
}
QString IaitoCore::sdbGet(QString path, QString key)
{
CORE_LOCK();
Sdb *db = sdb_ns_path(core->sdb, path.toUtf8().constData(), 0);
if (db) {
const char *val = sdb_const_get(db, key.toUtf8().constData(), 0);
if (val && *val)
return val;
}
return QString();
}
bool IaitoCore::sdbSet(QString path, QString key, QString val)
{
CORE_LOCK();
Sdb *db = sdb_ns_path(core->sdb, path.toUtf8().constData(), 1);
return db && sdb_set(db, key.toUtf8().constData(), val.toUtf8().constData(), 0);
}
QString IaitoCore::sanitizeStringForCommand(QString s)
{
static const QRegularExpression regexp(";|@`");
return s.replace(regexp, QStringLiteral("_"));
}
QString IaitoCore::cmdHtml(const char *str)
{
CORE_LOCK();
RVA offset = core->offset;
r_core_cmd0(core, "e scr.html=true;e scr.color=2");
char *res = r_core_cmd_str(core, str);
r_core_cmd0(core, "e scr.html=false;e scr.color=0");
QString o = fromOwnedCharPtr(res);
if (offset != core->offset) {
updateSeek();
}
return o;
}
QString IaitoCore::cmd(const char *str)
{
CORE_LOCK();
RVA offset = core->offset;
char *res = r_core_cmd_str(core, str);
QString o = fromOwnedCharPtr(res);
if (offset != core->offset) {
updateSeek();
}
return o;
}
bool IaitoCore::isRedirectableDebugee()
{
if (!currentlyDebugging || currentlyAttachedToPID != -1) {
return false;
}
// We are only able to redirect locally debugged unix processes
QJsonArray openFilesArray = cmdj("oj").array();
for (QJsonValue value : openFilesArray) {
QJsonObject openFile = value.toObject();
QString URI = openFile["uri"].toString();
if (URI.contains("ptrace") || URI.contains("mach")) {
return true;
}
}
return false;
}
bool IaitoCore::isDebugTaskInProgress()
{
if (!debugTask.isNull()) {
return true;
}
return false;
}
bool IaitoCore::asyncCmdEsil(const char *command, QSharedPointer<R2Task> &task)
{
asyncCmd(command, task);
if (task.isNull()) {
return false;
}
connect(task.data(), &R2Task::finished, task.data(), [this, task]() {
QString res = task.data()->getResult();
if (res.contains(QStringLiteral("[ESIL] Stopped execution in an invalid instruction"))) {
msgBox.showMessage("Stopped when attempted to run an invalid instruction. You can "
"disable this in Preferences");
}
});
return true;
}
bool IaitoCore::asyncCmd(const char *str, QSharedPointer<R2Task> &task)
{
if (!task.isNull()) {
return false;
}
CORE_LOCK();
RVA offset = core->offset;
task = QSharedPointer<R2Task>(new R2Task(str, true));
connect(task.data(), &R2Task::finished, task.data(), [this, offset, task]() {
CORE_LOCK();
if (offset != core->offset) {
updateSeek();
}
});
return true;
}
QString IaitoCore::cmdRawAt(const char *cmd, RVA address)
{
RVA oldOffset = getOffset();
seekSilent(address);
QString res = cmdRaw(cmd);
seekSilent(oldOffset);
return res;
}
bool IaitoCore::cmdRaw0(const QString &s)
{
(void) r_core_cmd0(core_, s.toStdString().c_str());
return core_->rc == 0;
}
QString IaitoCore::cmdRaw(const char *rcmd)
{
QString res;
#if 1
res = cmd(rcmd);
#else
CORE_LOCK();
r_cons_push();
// r_cmd_call does not return the output of the command
r_cmd_call(core->rcmd, cmd);
// we grab the output straight from r_cons
res = r_cons_get_buffer();
// cleaning up
r_cons_pop();
r_cons_echo(NULL);
#endif
return res;
}
QJsonDocument IaitoCore::cmdj(const char *str)
{
CORE_LOCK();
char *res = r_core_cmd_str(core, str);
QJsonDocument doc = parseJson(res, str);
r_mem_free(res);
return doc;
}
QJsonDocument IaitoCore::cmdjAt(const char *str, RVA address)
{
RVA oldOffset = getOffset();
seekSilent(address);
QJsonDocument res = cmdj(str);
seekSilent(oldOffset);
return res;
}
QString IaitoCore::cmdTask(const QString &str)
{
R2Task task(str);
task.startTask();
task.joinTask();
return task.getResult();
}
QJsonDocument IaitoCore::cmdjTask(const QString &str)
{
#if MONOTHREAD
return cmdj(str);
#else
R2Task task(str);
task.startTask();
task.joinTask();
return parseJson(task.getResultRaw(), str);
#endif
}
QJsonDocument IaitoCore::parseJson(const char *res, const char *cmd)
{
QByteArray json(res);
if (json.isEmpty()) {
return QJsonDocument();
}
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(json, &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
if (cmd) {
R_LOG_ERROR(
"Failed to parse JSON for command \"%s\": %s",
cmd,
jsonError.errorString().toLocal8Bit().constData());
} else {
R_LOG_ERROR("Failed to parse JSON: %s", jsonError.errorString().toLocal8Bit().constData());
}
const int MAX_JSON_DUMP_SIZE = 8 * 1024;
if (json.length() > MAX_JSON_DUMP_SIZE) {
int originalSize = json.length();
json.resize(MAX_JSON_DUMP_SIZE);
R_LOG_INFO("%d bytes total: %s", originalSize, json.constData());
} else {
R_LOG_INFO("%s", json.constData());
}
}
return doc;
}
QStringList IaitoCore::autocomplete(const QString &cmd, RLinePromptType promptType, size_t limit)
{
RLineBuffer buf;
int c = snprintf(buf.data, sizeof(buf.data), "%s", cmd.toUtf8().constData());
if (c < 0) {
return {};
}
buf.index = buf.length = std::min((int) (sizeof(buf.data) - 1), c);
RLineCompletion completion;
r_line_completion_init(&completion, limit);
r_core_autocomplete(core(), &completion, &buf, promptType);
QStringList r;
#if R2_VERSION_NUMBER >= 50709
int amount = r_pvector_length(&completion.args);
#else
int amount = r_pvector_len(&completion.args);
#endif
r.reserve(amount);
for (int i = 0; i < amount; i++) {
r.push_back(
QString::fromUtf8(reinterpret_cast<const char *>(r_pvector_at(&completion.args, i))));
}
r_line_completion_fini(&completion);
return r;
}
/**
* @brief IaitoCore::loadFile
* Load initial file. TODO Maybe use the "o" commands?
* @param path File path
* @param baddr Base (RBin) address
* @param mapaddr Map address
* @param perms
* @param va
* @param loadbin Load RBin information
* @param forceBinPlugin
* @return
*/
bool IaitoCore::loadFile(
QString path,
ut64 baddr,
ut64 mapaddr,
int perms,
int va,
bool bincache,
bool loadbin,
const QString &forceBinPlugin)
{
CORE_LOCK();
r_config_set_i(core->config, "io.va", va);
r_config_set_b(core->config, "bin.cache", bincache);
Core()->loadIaitoRC(0);
RIODesc *f = r_core_file_open(core, path.toUtf8().constData(), perms, mapaddr);
if (!f) {
R_LOG_ERROR("r_core_file_open failed");
return false;
}
if (!forceBinPlugin.isNull()) {
r_bin_force_plugin(r_core_get_bin(core), forceBinPlugin.toUtf8().constData());
}
if (loadbin && va) {
if (!r_core_bin_load(core, path.toUtf8().constData(), baddr)) {
R_LOG_ERROR("Cannot find rbin information");
}
#if HAVE_MULTIPLE_RBIN_FILES_INSIDE_SELECT_WHICH_ONE
if (!r_core_file_open(core, path.toUtf8(), R_IO_READ | (rw ? R_IO_WRITE : 0, mapaddr))) {
R_LOG_ERROR("Cannot open file");
} else {
// load RBin information
// XXX only for sub-bins
r_core_bin_load(core, path.toUtf8(), baddr);
r_bin_select_idx(core->bin, NULL, idx);
}
#endif
} else {
// Not loading RBin info coz va = false
}
r_core_bin_export_info(core, R_MODE_SET);
/*
auto iod = core->io ? core->io->desc : NULL;
auto debug = core->file && iod && (core->file->fd == iod->fd) &&
iod->plugin && \ iod->plugin->isdbg;
*/
auto debug = r_config_get_b(core->config, "cfg.debug");
if (!debug && r_flag_get(core->flags, "entry0")) {
r_core_cmd0(core, "s entry0");
}
if (perms & R_PERM_W) {
r_core_cmd0(core, "omfg+w");
}
// run script
// Core()->loadIaitoRC(1);
Core()->loadSecondaryIaitoRC();
fflush(stdout);
return true;
}
bool IaitoCore::tryFile(QString path, bool rw)
{
if (path == "") {
return false;
}
CORE_LOCK();
int flags = rw ? R_PERM_RW : R_PERM_R;
RIODesc *cf = r_core_file_open(core, path.toUtf8().constData(), flags, 0LL);
if (!cf) {
return false;
}
r_core_cmdf(core, "o-%d", cf->fd);
return true;
}
/**
* @brief Maps a file using r2 API
* @param path Path to file
* @param mapaddr Map Address
* @return bool
*/
bool IaitoCore::mapFile(QString path, RVA mapaddr)
{
CORE_LOCK();
RVA addr = mapaddr != RVA_INVALID ? mapaddr : 0;
ut64 baddr = Core()->getFileInfo().object()["bin"].toObject()["baddr"].toVariant().toULongLong();
if (r_core_file_open(core, path.toUtf8().constData(), R_PERM_RX, addr)) {
r_core_bin_load(core, path.toUtf8().constData(), baddr);
} else {
return false;
}
return true;
}
void IaitoCore::renameFunction(const RVA offset, const QString &newName)
{
cmdRaw("afn " + newName + " " + RAddressString(offset));
emit functionRenamed(offset, newName);
}
void IaitoCore::delFunction(RVA addr)
{
cmdRaw("af- " + RAddressString(addr));
emit functionsChanged();
}
void IaitoCore::renameFlag(QString old_name, QString new_name)
{
cmdRaw("fr " + old_name + " " + new_name);
emit flagsChanged();
}
void IaitoCore::renameFunctionVariable(QString newName, QString oldName, RVA functionAddress)
{
CORE_LOCK();
RAnalFunction *function = r_anal_get_function_at(core->anal, functionAddress);
RAnalVar *variable = r_anal_function_get_var_byname(function, oldName.toUtf8().constData());
if (variable) {
r_anal_var_rename(variable, newName.toUtf8().constData(), true);
}
emit refreshCodeViews();
}
void IaitoCore::delFlag(RVA addr)
{
cmdRawAt("f-", addr);
emit flagsChanged();
}
void IaitoCore::delFlag(const QString &name)
{
cmdRaw("f-" + name);
emit flagsChanged();
}
QString IaitoCore::getInstructionBytes(RVA addr)
{
return cmdj("aoj @ " + RAddressString(addr))
.array()
.first()
.toObject()[RJsonKey::bytes]
.toString();
}
QString IaitoCore::getInstructionOpcode(RVA addr)
{
return cmdj("aoj @ " + RAddressString(addr))
.array()
.first()
.toObject()[RJsonKey::opcode]
.toString();
}
void IaitoCore::editInstruction(RVA addr, const QString &inst)
{
cmdRawAt(QStringLiteral("wa %1").arg(inst), addr);
emit instructionChanged(addr);
}
void IaitoCore::nopInstruction(RVA addr)
{
cmdRawAt("wao nop", addr);
emit instructionChanged(addr);
}
void IaitoCore::jmpReverse(RVA addr)
{
cmdRawAt("wao recj", addr);
emit instructionChanged(addr);
}
void IaitoCore::editBytes(RVA addr, const QString &bytes)
{
cmdRawAt(QStringLiteral("wx %1").arg(bytes), addr);
emit instructionChanged(addr);
}
void IaitoCore::editBytesEndian(RVA addr, const QString &bytes)
{
cmdRawAt(QStringLiteral("wv %1").arg(bytes), addr);
emit stackChanged();
}
void IaitoCore::setToCode(RVA addr)
{
cmdRawAt("Cd-", addr);
emit instructionChanged(addr);
}
void IaitoCore::setAsString(RVA addr, int size, StringTypeFormats type)
{
if (RVA_INVALID == addr) {
return;
}
QString command;
switch (type) {
case StringTypeFormats::s_None: {
command = "Cs";
break;
}
case StringTypeFormats::s_ASCII_LATIN1: {
command = "Csa";
break;
}
case StringTypeFormats::s_UTF8: {
command = "Cs8";
break;
}
case StringTypeFormats::s_PASCAL: {
command = "Csp";
break;
}
case StringTypeFormats::s_UTF16: {
command = "Csw";
break;
}
default:
return;
}
seekAndShow(addr);
cmdRawAt(QStringLiteral("%1 %2").arg(command).arg(size), addr);
emit instructionChanged(addr);
}
void IaitoCore::removeString(RVA addr)
{
cmdRawAt("Cs-", addr);
emit instructionChanged(addr);
}
QString IaitoCore::getString(RVA addr)
{
return cmdRawAt("ps", addr);
}
void IaitoCore::setToData(RVA addr, int size, int repeat)
{
if (size <= 0 || repeat <= 0) {
return;
}
cmdRawAt("Cd-", addr);
cmdRawAt(QStringLiteral("Cd %1 %2").arg(size).arg(repeat), addr);
emit instructionChanged(addr);
}
int IaitoCore::sizeofDataMeta(RVA addr)
{
bool ok;
int size = cmdRawAt("Cd.", addr).toInt(&ok);
return (ok ? size : 0);
}
void IaitoCore::setComment(RVA addr, const QString &cmt)
{
cmdRawAt(QStringLiteral("CCu base64:%1").arg(QString(cmt.toLocal8Bit().toBase64())), addr);
emit commentsChanged(addr);
}
void IaitoCore::delComment(RVA addr)
{
cmdRawAt("CC-", addr);
emit commentsChanged(addr);
}
/**
* @brief Gets the comment present at a specific address
* @param addr The address to be checked
* @return String containing comment
*/
QString IaitoCore::getCommentAt(RVA addr)
{
CORE_LOCK();
return r_meta_get_string(core->anal, R_META_TYPE_COMMENT, addr);
}
void IaitoCore::setImmediateBase(const QString &r2BaseName, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
this->cmdRawAt(QStringLiteral("ahi %1").arg(r2BaseName), offset);
emit instructionChanged(offset);
}
void IaitoCore::setCurrentBits(int bits, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
this->cmdRawAt(QStringLiteral("ahb %1").arg(bits), offset);
emit instructionChanged(offset);
}
void IaitoCore::applyStructureOffset(const QString &structureOffset, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
this->cmdRawAt("aht " + structureOffset, offset);
emit instructionChanged(offset);
}
void IaitoCore::seekSilent(ut64 offset)
{
CORE_LOCK();
if (offset == RVA_INVALID) {
return;
}
r_core_seek(core, offset, true);
}
void IaitoCore::seek(ut64 offset)
{
// Slower than using the API, but the API is not complete
// which means we either have to duplicate code from radare2
// here, or refactor radare2 API.
CORE_LOCK();
if (offset == RVA_INVALID) {
return;
}
// use cmd and not cmdRaw to make sure seekChanged is emitted
cmd(QStringLiteral("s %1").arg(offset));
// cmd already does emit seekChanged(core_->offset);
}
void IaitoCore::showMemoryWidget()
{
emit showMemoryWidgetRequested();
}
void IaitoCore::seekAndShow(ut64 offset)
{
seek(offset);
showMemoryWidget();
}
void IaitoCore::seekAndShow(QString offset)
{