-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCutter.cpp
More file actions
5338 lines (4713 loc) · 148 KB
/
Cutter.cpp
File metadata and controls
5338 lines (4713 loc) · 148 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
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 <QJsonArray>
#include <QJsonObject>
#include <QRegularExpression>
#include <QDir>
#include <QCoreApplication>
#include <QVector>
#include <QStringList>
#include <QStandardPaths>
#include <cassert>
#include <memory>
#include "CutterDescriptions.h"
#include "common/TempConfig.h"
#include "common/BasicInstructionHighlighter.h"
#include "common/Configuration.h"
#include "common/AsyncTask.h"
#include "common/RizinTask.h"
#include "dialogs/MarkDialog.h"
#include "dialogs/RizinTaskDialog.h"
#include "common/Json.h"
#include "core/Cutter.h"
#include "Decompiler.h"
#include <rz_asm.h>
#include <rz_cmd.h>
#include <rz_socket.h>
#include <sdb.h>
static CutterCore *uniqueInstance;
#define RZ_JSON_KEY(name) static const QString name = QStringLiteral(#name)
namespace RJsonKey {
RZ_JSON_KEY(addr);
RZ_JSON_KEY(address);
RZ_JSON_KEY(addrs);
RZ_JSON_KEY(addr_end);
RZ_JSON_KEY(arrow);
RZ_JSON_KEY(baddr);
RZ_JSON_KEY(bind);
RZ_JSON_KEY(blocks);
RZ_JSON_KEY(blocksize);
RZ_JSON_KEY(bytes);
RZ_JSON_KEY(calltype);
RZ_JSON_KEY(cc);
RZ_JSON_KEY(classname);
RZ_JSON_KEY(code);
RZ_JSON_KEY(comment);
RZ_JSON_KEY(comments);
RZ_JSON_KEY(cost);
RZ_JSON_KEY(data);
RZ_JSON_KEY(description);
RZ_JSON_KEY(ebbs);
RZ_JSON_KEY(edges);
RZ_JSON_KEY(enabled);
RZ_JSON_KEY(entropy);
RZ_JSON_KEY(fcn_addr);
RZ_JSON_KEY(fcn_name);
RZ_JSON_KEY(fields);
RZ_JSON_KEY(file);
RZ_JSON_KEY(flag);
RZ_JSON_KEY(flags);
RZ_JSON_KEY(flagname);
RZ_JSON_KEY(format);
RZ_JSON_KEY(from);
RZ_JSON_KEY(functions);
RZ_JSON_KEY(graph);
RZ_JSON_KEY(haddr);
RZ_JSON_KEY(hw);
RZ_JSON_KEY(in_functions);
RZ_JSON_KEY(index);
RZ_JSON_KEY(jump);
RZ_JSON_KEY(laddr);
RZ_JSON_KEY(lang);
RZ_JSON_KEY(len);
RZ_JSON_KEY(length);
RZ_JSON_KEY(license);
RZ_JSON_KEY(methods);
RZ_JSON_KEY(name);
RZ_JSON_KEY(realname);
RZ_JSON_KEY(nargs);
RZ_JSON_KEY(nbbs);
RZ_JSON_KEY(nlocals);
RZ_JSON_KEY(offset);
RZ_JSON_KEY(opcode);
RZ_JSON_KEY(opcodes);
RZ_JSON_KEY(ordinal);
RZ_JSON_KEY(libname);
RZ_JSON_KEY(outdegree);
RZ_JSON_KEY(paddr);
RZ_JSON_KEY(path);
RZ_JSON_KEY(perm);
RZ_JSON_KEY(pid);
RZ_JSON_KEY(plt);
RZ_JSON_KEY(prot);
RZ_JSON_KEY(ref);
RZ_JSON_KEY(refs);
RZ_JSON_KEY(reg);
RZ_JSON_KEY(rwx);
RZ_JSON_KEY(section);
RZ_JSON_KEY(sections);
RZ_JSON_KEY(size);
RZ_JSON_KEY(stackframe);
RZ_JSON_KEY(status);
RZ_JSON_KEY(string);
RZ_JSON_KEY(strings);
RZ_JSON_KEY(symbols);
RZ_JSON_KEY(text);
RZ_JSON_KEY(to);
RZ_JSON_KEY(trace);
RZ_JSON_KEY(type);
RZ_JSON_KEY(uid);
RZ_JSON_KEY(vaddr);
RZ_JSON_KEY(value);
RZ_JSON_KEY(vsize);
}
#undef RZ_JSON_KEY
static void updateOwnedCharPtr(char *&variable, const QString &newValue)
{
auto data = newValue.toUtf8();
RZ_FREE(variable)
variable = strdup(data.data());
}
static bool reg_sync(RzCore *core, RzRegisterType type, bool write)
{
if (rz_core_is_debug(core)) {
return rz_debug_reg_sync(core->dbg, type, write);
}
return true;
}
RzCoreLocked::RzCoreLocked(CutterCore *core) : core(core)
{
core->coreMutex.lock();
assert(core->coreLockDepth >= 0);
core->coreLockDepth++;
if (core->coreLockDepth == 1) {
assert(core->coreBed);
rz_cons_sleep_end(core->coreBed);
core->coreBed = nullptr;
}
}
RzCoreLocked::~RzCoreLocked()
{
assert(core->coreLockDepth > 0);
core->coreLockDepth--;
if (core->coreLockDepth == 0) {
core->coreBed = rz_cons_sleep_begin();
}
core->coreMutex.unlock();
}
RzCoreLocked::operator RzCore *() &
{
return core->core_;
}
RzCore *RzCoreLocked::operator->() &
{
return core->core_;
}
#define CORE_LOCK() RzCoreLocked core(this)
static void cutterREventCallback(RzEvent *, int type, void *user, void *data)
{
auto core = reinterpret_cast<CutterCore *>(user);
core->handleREvent(type, data);
}
CutterCore::CutterCore(QObject *parent)
: QObject(parent)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
,
coreMutex(QMutex::Recursive)
#endif
{
if (uniqueInstance) {
throw std::logic_error("Only one instance of CutterCore must exist");
}
uniqueInstance = this;
}
CutterCore *CutterCore::instance()
{
return uniqueInstance;
}
void CutterCore::initialize(bool loadPlugins)
{
rz_cons_new(); // initialize console
core_ = rz_core_new();
#if defined(MACOS_RZ_BUNDLED)
auto app_path = QDir(QCoreApplication::applicationDirPath());
app_path.cdUp();
app_path.cd("Resources");
qInfo() << "Setting Rizin prefix =" << app_path.absolutePath()
<< " for macOS Application Bundle.";
rz_path_set_prefix(core_->sys_path, app_path.absolutePath().toUtf8().constData());
#endif
char **env = rz_sys_get_environ();
core_->io->envprofile = rz_run_get_environ_profile(env);
rz_core_task_sync_begin(&core_->tasks);
coreBed = rz_cons_sleep_begin();
CORE_LOCK();
rz_event_hook(core_->ev, RZ_EVENT_ALL, cutterREventCallback, this);
if (loadPlugins) {
setConfig("cfg.plugins", true);
rz_core_loadlibs(this->core_, RZ_CORE_LOADLIBS_ALL);
} else {
setConfig("cfg.plugins", false);
}
// IMPLICIT rz_bin_iobind (core_->bin, core_->io);
// Otherwise Rizin may ask the user for input and Cutter would freeze
setConfig("scr.interactive", false);
// Temporary workaround for https://github.com/rizinorg/rizin/issues/2741
// Otherwise sometimes disassembly is truncated.
// The blocksize here is a rather arbitrary value larger than the default 0x100.
rz_core_block_size(core, 0x400);
// Initialize graph node highlighter
bbHighlighter = new BasicBlockHighlighter();
// Initialize Async tasks manager
asyncTaskManager = new AsyncTaskManager(this);
}
CutterCore::~CutterCore()
{
delete bbHighlighter;
rz_cons_sleep_end(coreBed);
rz_core_task_sync_end(&core_->tasks);
rz_core_free(this->core_);
rz_cons_free();
assert(uniqueInstance == this);
uniqueInstance = nullptr;
}
RzCoreLocked CutterCore::lock()
{
return RzCoreLocked(this);
}
RzCoreLocked CutterCore::core()
{
return lock();
}
QDir CutterCore::getCutterRCDefaultDirectory() const
{
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
QVector<QString> CutterCore::getCutterRCFilePaths() const
{
QVector<QString> result;
result.push_back(QFileInfo(QDir::home(), ".cutterrc").absoluteFilePath());
QStringList locations = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation);
for (auto &location : locations) {
result.push_back(QFileInfo(QDir(location), ".cutterrc").absoluteFilePath());
}
result.push_back(QFileInfo(getCutterRCDefaultDirectory(), "rc")
.absoluteFilePath()); // File in config editor is from this path
return result;
}
void CutterCore::loadCutterRC()
{
CORE_LOCK();
const auto result = getCutterRCFilePaths();
for (auto &cutterRCFilePath : result) {
auto cutterRCFileInfo = QFileInfo(cutterRCFilePath);
if (!cutterRCFileInfo.exists() || !cutterRCFileInfo.isFile()) {
continue;
}
qInfo() << tr("Loading initialization file from ") << cutterRCFilePath;
rz_core_cmd_file(core, cutterRCFilePath.toUtf8().constData());
rz_cons_flush();
}
}
void CutterCore::loadDefaultCutterRC()
{
CORE_LOCK();
auto cutterRCFilePath = QFileInfo(getCutterRCDefaultDirectory(), "rc").absoluteFilePath();
const auto cutterRCFileInfo = QFileInfo(cutterRCFilePath);
if (!cutterRCFileInfo.exists() || !cutterRCFileInfo.isFile()) {
return;
}
qInfo() << tr("Loading initialization file from ") << cutterRCFilePath;
rz_core_cmd_file(core, cutterRCFilePath.toUtf8().constData());
rz_cons_flush();
}
QList<QString> CutterCore::sdbList(QString path)
{
CORE_LOCK();
QList<QString> list = QList<QString>();
Sdb *root = sdb_ns_path(core->sdb, path.toUtf8().constData(), 0);
if (root && root->ns) {
for (const auto &nsi : CutterRzList<SdbNs>(root->ns)) {
list << nsi->name;
}
}
return list;
}
using PVectorPtr = std::unique_ptr<RzPVector, decltype(&rz_pvector_free)>;
static PVectorPtr makePVectorPtr(RzPVector *vec)
{
return { vec, rz_pvector_free };
}
static bool foreach_keys_cb(void *user, const SdbKv *kv)
{
auto list = reinterpret_cast<QList<QString> *>(user);
*list << kv->base.key;
return true;
}
QList<QString> CutterCore::sdbListKeys(QString path)
{
CORE_LOCK();
QList<QString> list = QList<QString>();
Sdb *root = sdb_ns_path(core->sdb, path.toUtf8().constData(), 0);
if (root) {
sdb_foreach(root, foreach_keys_cb, &list);
}
return list;
}
QString CutterCore::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());
if (val && *val)
return val;
}
return QString();
}
bool CutterCore::sdbSet(QString path, QString key, QString val)
{
CORE_LOCK();
Sdb *db = sdb_ns_path(core->sdb, path.toUtf8().constData(), 1);
if (!db)
return false;
return sdb_set(db, key.toUtf8().constData(), val.toUtf8().constData());
}
QString CutterCore::sanitizeStringForCommand(QString s)
{
static const QRegularExpression regexp(";|@");
return s.replace(regexp, QStringLiteral("_"));
}
QString CutterCore::cmd(const char *str)
{
CORE_LOCK();
RVA offset = core->offset;
char *res = rz_core_cmd_str(core, str);
QString o = fromOwnedCharPtr(res);
if (offset != core->offset) {
updateSeek();
}
return o;
}
QString CutterCore::getFunctionExecOut(const std::function<bool(RzCore *)> &fcn, const RVA addr)
{
CORE_LOCK();
RVA offset = core->offset;
seekSilent(addr);
QString o = {};
rz_cons_push();
bool is_pipe = core->is_pipe;
core->is_pipe = true;
if (!fcn(core)) {
core->is_pipe = is_pipe;
rz_cons_pop();
goto clean_return;
}
core->is_pipe = is_pipe;
rz_cons_filter();
o = rz_cons_get_buffer();
rz_cons_pop();
rz_cons_echo(NULL);
clean_return:
if (offset != core->offset) {
seekSilent(offset);
}
return o;
}
bool CutterCore::isRedirectableDebugee()
{
if (!currentlyDebugging || currentlyAttachedToPID != -1) {
return false;
}
// We are only able to redirect locally debugged unix processes
RzCoreLocked core(Core());
RzList *descs = rz_id_storage_list(core->io->files);
RzListIter *it;
RzIODesc *desc;
CutterRzListForeach (descs, it, RzIODesc, desc) {
QString URI = QString(desc->uri);
if (URI.contains("ptrace") || URI.contains("mach")) {
return true;
}
}
return false;
}
bool CutterCore::isDebugTaskInProgress()
{
if (!debugTask.isNull()) {
return true;
}
return false;
}
void CutterCore::setProfileDirectives(const QString &directives)
{
QString file = getConfig("dbg.profile");
if (file.isEmpty()) {
char *temp_path = rz_file_temp("rz-run");
file = QString::fromUtf8(temp_path);
free(temp_path);
setConfig("dbg.profile", file);
}
QByteArray fileNameBytes = file.toUtf8();
QByteArray directiveBytes = directives.toUtf8();
const char *pathPtr = fileNameBytes.constData();
rz_file_dump(pathPtr, (const ut8 *)directiveBytes.data(), (int)directiveBytes.size(), 0);
rz_file_dump(pathPtr, (const ut8 *)"\n", 1, 1);
}
void CutterCore::setRegisterProfile(const QString &profile)
{
CORE_LOCK();
rz_reg_set_profile_string(core->dbg->reg, profile.toUtf8().constData());
emit registersChanged();
}
QString CutterCore::convertGDBProfile(const QString &profilePath)
{
return QString::fromUtf8(rz_reg_parse_gdb_profile(profilePath.toUtf8().constData()));
}
QString CutterCore::getRegisterProfile()
{
CORE_LOCK();
RzReg *reg = core->dbg->reg;
if (reg && reg->reg_profile_str) {
return QString::fromUtf8(reg->reg_profile_str);
}
return QString();
}
bool CutterCore::asyncTask(std::function<void *(RzCore *)> fcn, QSharedPointer<RizinTask> &task)
{
if (!task.isNull()) {
return false;
}
CORE_LOCK();
RVA offset = core->offset;
task = QSharedPointer<RizinTask>(new RizinFunctionTask(std::move(fcn), true));
connect(task.data(), &RizinTask::finished, task.data(), [this, offset, task]() {
CORE_LOCK();
if (offset != core->offset) {
updateSeek();
}
});
return true;
}
void CutterCore::functionTask(std::function<void *(RzCore *)> fcn)
{
auto task = std::unique_ptr<RizinTask>(new RizinFunctionTask(std::move(fcn), true));
task->startTask();
task->joinTask();
}
QString CutterCore::cmdRawAt(const char *cmd, RVA address)
{
QString res;
RVA oldOffset = getOffset();
seekSilent(address);
res = cmdRaw(cmd);
seekSilent(oldOffset);
return res;
}
QString CutterCore::cmdRaw(const char *cmd)
{
QString res;
CORE_LOCK();
return rz_core_cmd_str(core, cmd);
}
CutterJson CutterCore::cmdj(const char *str)
{
char *res;
{
CORE_LOCK();
res = rz_core_cmd_str(core, str);
}
return parseJson("cmdj", res, str);
}
QString CutterCore::cmdTask(const QString &str)
{
RizinCmdTask task(str);
task.startTask();
task.joinTask();
return task.getResult();
}
CutterJson CutterCore::parseJson(const char *name, char *res, const char *cmd)
{
if (RZ_STR_ISEMPTY(res)) {
return CutterJson();
}
RzJson *doc = rz_json_parse(res);
if (!doc) {
if (cmd) {
RZ_LOG_ERROR("%s: Failed to parse JSON for command \"%s\"\n%s\n", name, cmd, res);
} else {
RZ_LOG_ERROR("%s: Failed to parse JSON %s\n", name, res);
}
RZ_LOG_ERROR("%s: %s\n", name, res);
}
return CutterJson(doc, QSharedPointer<CutterJsonOwner>::create(doc, res));
}
QStringList CutterCore::autocomplete(const QString &cmd, RzLinePromptType promptType)
{
RzLineBuffer 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);
CORE_LOCK();
RzLineNSCompletionResult *compr = rz_core_autocomplete_rzshell(core, &buf, promptType);
QStringList r;
auto optslen = rz_pvector_len(&compr->options);
r.reserve(optslen);
for (size_t i = 0; i < optslen; i++) {
r.push_back(QString::fromUtf8(
reinterpret_cast<const char *>(rz_pvector_at(&compr->options, i))));
}
rz_line_ns_completion_result_free(compr);
return r;
}
/**
* @brief CutterCore::loadFile
* Load initial file.
* @param path File path
* @param baddr Base (RzBin) address
* @param mapaddr Map address
* @param perms
* @param va
* @param loadbin Load RzBin information
* @param forceBinPlugin
* @return
*/
bool CutterCore::loadFile(QString path, ut64 baddr, ut64 mapaddr, int perms, int va, bool loadbin,
const QString &forceBinPlugin)
{
CORE_LOCK();
RzCoreFile *f;
rz_config_set_i(core->config, "io.va", va);
f = rz_core_file_open(core, path.toUtf8().constData(), perms, mapaddr);
if (!f) {
eprintf("rz_core_file_open failed\n");
return false;
}
if (!forceBinPlugin.isNull()) {
rz_bin_force_plugin(rz_core_get_bin(core), forceBinPlugin.toUtf8().constData());
}
if (loadbin && va) {
if (!rz_core_bin_load(core, path.toUtf8().constData(), baddr)) {
eprintf("CANNOT GET RBIN INFO\n");
}
#if HAVE_MULTIPLE_RBIN_FILES_INSIDE_SELECT_WHICH_ONE
if (!rz_core_file_open(core, path.toUtf8(), RZ_IO_READ | (rw ? RZ_IO_WRITE : 0, mapaddr))) {
eprintf("Cannot open file\n");
} else {
// load RzBin information
// XXX only for sub-bins
rz_core_bin_load(core, path.toUtf8(), baddr);
rz_bin_select_idx(core->bin, NULL, idx);
}
#endif
}
auto iod = core->io ? core->io->desc : NULL;
auto debug =
core->file && iod && (core->file->fd == iod->fd) && iod->plugin && iod->plugin->isdbg;
if (!debug && rz_flag_get(core->flags, "entry0")) {
ut64 addr = rz_num_math(core->num, "entry0");
rz_core_seek_and_save(core, addr, true);
}
if (perms & RZ_PERM_W) {
RzPVector *maps = rz_io_maps(core->io);
for (auto map : CutterPVector<RzIOMap>(maps)) {
map->perm |= RZ_PERM_W;
}
}
rz_cons_flush();
fflush(stdout);
return true;
}
bool CutterCore::tryFile(QString path, bool rw)
{
if (path.isEmpty()) {
// opening no file is always possible
return true;
}
CORE_LOCK();
// clear info from previous fails
rz_cons_break_clear();
RzCoreFile *cf;
int flags = RZ_PERM_R;
if (rw)
flags = RZ_PERM_RW;
cf = rz_core_file_open(core, path.toUtf8().constData(), flags, 0LL);
if (!cf) {
return false;
}
rz_core_file_close(cf);
return true;
}
/**
* @brief Maps a file using Rizin API
* @param path Path to file
* @param mapaddr Map Address
* @return bool
*/
bool CutterCore::mapFile(QString path, RVA mapaddr)
{
CORE_LOCK();
RVA addr = mapaddr != RVA_INVALID ? mapaddr : 0;
ut64 baddr = rz_bin_get_baddr(core->bin);
if (rz_core_file_open(core, path.toUtf8().constData(), RZ_PERM_RX, addr)) {
rz_core_bin_load(core, path.toUtf8().constData(), baddr);
} else {
return false;
}
return true;
}
void CutterCore::renameFunction(const RVA offset, const QString &newName)
{
CORE_LOCK();
rz_core_analysis_function_rename(core, offset, newName.toStdString().c_str());
emit functionRenamed(offset, newName);
}
void CutterCore::delFunction(RVA addr)
{
CORE_LOCK();
rz_core_analysis_undefine(core, addr);
emit functionsChanged();
}
void CutterCore::renameFlag(QString old_name, QString new_name)
{
CORE_LOCK();
RzFlagItem *flag = rz_flag_get(core->flags, old_name.toStdString().c_str());
if (!flag)
return;
rz_flag_rename(core->flags, flag, new_name.toStdString().c_str());
emit flagsChanged();
}
void CutterCore::renameFunctionVariable(QString newName, QString oldName, RVA functionAddress)
{
CORE_LOCK();
RzAnalysisFunction *function = rz_analysis_get_function_at(core->analysis, functionAddress);
RzAnalysisVar *variable =
rz_analysis_function_get_var_byname(function, oldName.toUtf8().constData());
if (variable) {
rz_analysis_var_rename(variable, newName.toUtf8().constData(), true);
}
emit refreshCodeViews();
}
void CutterCore::delFlag(RVA addr)
{
CORE_LOCK();
rz_flag_unset_off(core->flags, addr);
emit flagsChanged();
}
void CutterCore::delFlag(const QString &name)
{
CORE_LOCK();
rz_flag_unset_name(core->flags, name.toStdString().c_str());
emit flagsChanged();
}
CutterRzIter<RzCoreDecodedBytes> CutterCore::getRzCoreDecodedBytesSingle(RVA addr)
{
CORE_LOCK();
ut8 buf[128];
rz_io_read_at_mapped(core->io, addr, buf, sizeof(buf));
// Warning! only safe to use with stack buffer, due to instruction count being 1
auto result = CutterRzIter<RzCoreDecodedBytes>(
rz_core_analysis_bytes(core, addr, buf, sizeof(buf), 1));
return result;
}
QString CutterCore::getInstructionBytes(RVA addr)
{
auto cdb = getRzCoreDecodedBytesSingle(addr);
return cdb ? cdb->bytes : "";
}
QString CutterCore::getInstructionOpcode(RVA addr)
{
auto cdb = getRzCoreDecodedBytesSingle(addr);
return cdb ? cdb->opcode : "";
}
void CutterCore::editInstruction(RVA addr, const QString &inst, bool fillWithNops)
{
CORE_LOCK();
if (fillWithNops) {
rz_core_write_assembly_fill(core, addr, inst.trimmed().toStdString().c_str());
} else {
rz_core_write_assembly(core, addr, inst.trimmed().toStdString().c_str());
}
emit instructionChanged(addr);
}
void CutterCore::nopInstruction(RVA addr)
{
CORE_LOCK();
{
auto seek = seekTemp(addr);
rz_core_hack(core, "nop");
}
emit instructionChanged(addr);
}
void CutterCore::jmpReverse(RVA addr)
{
CORE_LOCK();
{
auto seek = seekTemp(addr);
rz_core_hack(core, "recj");
}
emit instructionChanged(addr);
}
void CutterCore::editBytes(RVA addr, const QString &bytes)
{
CORE_LOCK();
rz_core_write_hexpair(core, addr, bytes.toUtf8().constData());
emit instructionChanged(addr);
}
void CutterCore::editBytesEndian(RVA addr, const QString &bytes)
{
CORE_LOCK();
ut64 value = rz_num_math(core->num, bytes.toUtf8().constData());
if (core->num->nc.errors) {
return;
}
rz_core_write_value_at(core, addr, value, 0);
emit stackChanged();
}
void CutterCore::setToCode(RVA addr)
{
CORE_LOCK();
rz_meta_del(core->analysis, RZ_META_TYPE_STRING, core->offset, 1);
rz_meta_del(core->analysis, RZ_META_TYPE_DATA, core->offset, 1);
emit instructionChanged(addr);
}
void CutterCore::setAsString(RVA addr, int size, StringTypeFormats type)
{
if (RVA_INVALID == addr) {
return;
}
RzStrEnc encoding;
switch (type) {
case StringTypeFormats::None: {
encoding = RZ_STRING_ENC_GUESS;
break;
}
case StringTypeFormats::ASCII_LATIN1: {
encoding = RZ_STRING_ENC_8BIT;
break;
}
case StringTypeFormats::UTF8: {
encoding = RZ_STRING_ENC_UTF8;
break;
}
default:
return;
}
CORE_LOCK();
seekAndShow(addr);
rz_core_meta_string_add(core, addr, size, encoding, nullptr);
emit instructionChanged(addr);
}
void CutterCore::removeString(RVA addr)
{
CORE_LOCK();
rz_meta_del(core->analysis, RZ_META_TYPE_STRING, addr, 1);
emit instructionChanged(addr);
}
QString CutterCore::getString(RVA addr)
{
CORE_LOCK();
return getString(addr, core->blocksize,
rz_str_guess_encoding_from_buffer(core->block, core->blocksize));
}
QString CutterCore::getString(RVA addr, uint64_t len, RzStrEnc encoding, bool escape_nl)
{
CORE_LOCK();
RzStrStringifyOpt opt = {};
opt.buffer = core->block;
opt.length = len;
opt.encoding = encoding;
opt.escape_nl = escape_nl;
auto seek = seekTemp(addr);
return fromOwnedCharPtr(rz_str_stringify_raw_buffer(&opt, NULL));
}
QString CutterCore::getMetaString(RVA addr)
{
CORE_LOCK();
return rz_meta_get_string(core->analysis, RZ_META_TYPE_STRING, addr);
}
void CutterCore::setToData(RVA addr, int size, int repeat)
{
if (size <= 0 || repeat <= 0) {
return;
}
CORE_LOCK();
RVA address = addr;
for (int i = 0; i < repeat; ++i, address += size) {
rz_meta_set(core->analysis, RZ_META_TYPE_DATA, address, size, nullptr);
}
emit instructionChanged(addr);
}
int CutterCore::sizeofDataMeta(RVA addr)
{
ut64 size = 0;
CORE_LOCK();
rz_meta_get_at(core->analysis, addr, RZ_META_TYPE_DATA, &size);
return (int)size;
}
void CutterCore::setComment(RVA addr, const QString &cmt)
{
CORE_LOCK();
rz_meta_set_string(core->analysis, RZ_META_TYPE_COMMENT, addr, cmt.toStdString().c_str());
emit commentsChanged(addr);
}
void CutterCore::delComment(RVA addr)
{
CORE_LOCK();
rz_meta_del(core->analysis, RZ_META_TYPE_COMMENT, addr, 1);
emit commentsChanged(addr);
}
/**
* @brief Gets the comment present at a specific address
* @param addr The address to be checked
* @return String containing comment
*/
QString CutterCore::getCommentAt(RVA addr)
{
CORE_LOCK();
return rz_meta_get_string(core->analysis, RZ_META_TYPE_COMMENT, addr);
}
void CutterCore::setImmediateBase(const QString &rzBaseName, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
CORE_LOCK();
int base = (int)rz_num_base_of_string(core->num, rzBaseName.toUtf8().constData());
rz_analysis_hint_set_immbase(core->analysis, offset, base);
emit instructionChanged(offset);
}
void CutterCore::setCurrentBits(int bits, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
CORE_LOCK();
rz_analysis_hint_set_bits(core->analysis, offset, bits);
emit instructionChanged(offset);
}
void CutterCore::applyStructureOffset(const QString &structureOffset, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
{
CORE_LOCK();
auto seek = seekTemp(offset);
rz_core_analysis_hint_set_offset(core, structureOffset.toUtf8().constData());
}
emit instructionChanged(offset);
}
void CutterCore::seekSilent(ut64 offset)
{
CORE_LOCK();
if (offset == RVA_INVALID) {
return;
}
rz_core_seek(core, offset, true);
}
void CutterCore::seek(ut64 offset)
{
// Slower than using the API, but the API is not complete
// which means we either have to duplicate code from rizin
// here, or refactor rizin API.
CORE_LOCK();
if (offset == RVA_INVALID) {
return;
}
RVA o_offset = core->offset;
rz_core_seek_and_save(core, offset, true);
if (o_offset != core->offset) {
updateSeek();