From 923a7e888c3600f94d6d317fe9d652af31618204 Mon Sep 17 00:00:00 2001 From: xarkes Date: Sat, 5 May 2018 15:20:14 +0200 Subject: [PATCH] A bit of AnalThread cleaning --- src/AnalThread.cpp | 72 ++++++++++++++--------------------- src/AnalThread.h | 1 - src/Cutter.cpp | 46 ++++------------------ src/Cutter.h | 11 ++---- src/dialogs/OptionsDialog.ui | 22 ++++++----- src/widgets/HexdumpWidget.cpp | 6 +-- 6 files changed, 55 insertions(+), 103 deletions(-) diff --git a/src/AnalThread.cpp b/src/AnalThread.cpp index 2c3ed2f4..9d3b9561 100644 --- a/src/AnalThread.cpp +++ b/src/AnalThread.cpp @@ -1,15 +1,15 @@ -#include #include "Cutter.h" #include "AnalThread.h" #include "MainWindow.h" #include "dialogs/OptionsDialog.h" #include +#include +#include AnalThread::AnalThread(OptionsDialog *parent) : QThread(parent), level(2), main(nullptr), - core(Core()), interrupted(false) { } @@ -46,41 +46,23 @@ void AnalThread::run() { const auto optionsDialog = dynamic_cast(parent()); const auto &ui = optionsDialog->ui; - int va = ui->vaCheckBox->isChecked(); - ut64 loadaddr = 0LL; - ut64 mapaddr = 0LL; - + bool va = ui->vaCheckBox->isChecked(); + ut64 binLoadAddr = Core()->math(ui->entry_loadOffset->text()); // Where the bin header is located in the file (-B) + ut64 mapAddr = Core()->math(ui->entry_mapOffset->text()); // Where to map the file once loaded (-m) interrupted = false; + emit updateProgress(tr("Loading binary...")); - // - // Advanced Options - // - - core->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(), + // Set the CPU details (handle auto) + Core()->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(), optionsDialog->getSelectedBits()); + // Binary opening permissions (read/write/execute) int perms = R_IO_READ | R_IO_EXEC; if (ui->writeCheckBox->isChecked()) perms |= R_IO_WRITE; + + // Check if we must load and parse binary header (ELF, PE, ...) bool loadBinInfo = !ui->binCheckBox->isChecked(); - - if (loadBinInfo) { - if (!va) { - va = 2; - loadaddr = UT64_MAX; - r_config_set_i(core->core()->config, "bin.laddr", loadaddr); - mapaddr = 0; - } - } else { - Core()->setConfig("file.info", "false"); - va = false; - loadaddr = mapaddr = 0; - } - - emit updateProgress(tr("Loading binary")); - // options dialog should show the list of archs inside the given fatbin - int binidx = 0; // index of subbin - QString forceBinPlugin = nullptr; QVariant forceBinPluginData = ui->formatComboBox->currentData(); if (!forceBinPluginData.isNull()) { @@ -88,36 +70,40 @@ void AnalThread::run() forceBinPlugin = pluginDesc.name; } - core->setConfig("bin.demangle", ui->demangleCheckBox->isChecked()); + // Demangle (must be before file Core()->loadFile) + Core()->setConfig("bin.demangle", ui->demangleCheckBox->isChecked()); + // Do not reload the file if already loaded QJsonArray openedFiles = Core()->getOpenedFiles(); if (!openedFiles.size()) { - core->loadFile(main->getFilename(), loadaddr, mapaddr, perms, va, binidx, loadBinInfo, + Core()->loadFile(main->getFilename(), binLoadAddr, mapAddr, perms, va, loadBinInfo, forceBinPlugin); } - emit updateProgress("Analysis in progress."); + // Set asm OS configuration QString os = optionsDialog->getSelectedOS(); if (!os.isNull()) { - core->cmd("e asm.os=" + os); + Core()->cmd("e asm.os=" + os); } + // Load PDB and/or scripts if (ui->pdbCheckBox->isChecked()) { - core->loadPDB(ui->pdbLineEdit->text()); + Core()->loadPDB(ui->pdbLineEdit->text()); } - if (ui->scriptCheckBox->isChecked()) { - core->loadScript(ui->scriptLineEdit->text()); + Core()->loadScript(ui->scriptLineEdit->text()); } + // Set various options if (optionsDialog->getSelectedEndianness() != OptionsDialog::Endianness::Auto) { - core->setEndianness(optionsDialog->getSelectedEndianness() == OptionsDialog::Endianness::Big); + Core()->setEndianness(optionsDialog->getSelectedEndianness() == OptionsDialog::Endianness::Big); } + Core()->setBBSize(optionsDialog->getSelectedBBSize()); + // Use prj.simple as default as long as regular projects are broken + Core()->setConfig("prj.simple", true); - core->setBBSize(optionsDialog->getSelectedBBSize()); - - // use prj.simple as default as long as regular projects are broken - core->setConfig("prj.simple", true); - - core->analyze(this->level, this->advanced); + // Start analysis + emit updateProgress(tr("Analysis in progress...")); + Core()->analyze(this->level, this->advanced); + emit updateProgress(tr("Analysis complete!")); } diff --git a/src/AnalThread.h b/src/AnalThread.h index 6218ae70..3494ef22 100644 --- a/src/AnalThread.h +++ b/src/AnalThread.h @@ -34,7 +34,6 @@ private: int level; QList advanced; MainWindow *main; - CutterCore *core; bool interrupted; }; diff --git a/src/Cutter.cpp b/src/Cutter.cpp index 53793f29..1863f545 100644 --- a/src/Cutter.cpp +++ b/src/Cutter.cpp @@ -71,9 +71,6 @@ CutterCore::CutterCore(QObject *parent) : # endif setConfig("dir.prefix", prefix.absolutePath()); #endif - - - default_bits = 0; } @@ -209,16 +206,12 @@ QJsonDocument CutterCore::cmdj(const QString &str) return doc; } -bool CutterCore::loadFile(QString path, uint64_t loadaddr, uint64_t mapaddr, int perms, int va, - int idx, bool loadbin, const QString &forceBinPlugin) +bool CutterCore::loadFile(QString path, ut64 baddr, ut64 mapaddr, int perms, int va, + bool loadbin, const QString &forceBinPlugin) { - Q_UNUSED(loadaddr); - Q_UNUSED(idx); - CORE_LOCK(); RCoreFile *f; - if (va == 0 || va == 2) - r_config_set_i(core_->config, "io.va", va); + r_config_set_i(core_->config, "io.va", va); f = r_core_file_open(core_, path.toUtf8().constData(), perms, mapaddr); if (!f) { @@ -230,15 +223,9 @@ bool CutterCore::loadFile(QString path, uint64_t loadaddr, uint64_t mapaddr, int r_bin_force_plugin(r_core_get_bin(core_), forceBinPlugin.toUtf8().constData()); } - if (loadbin) { - if (va == 1) { - if (!r_core_bin_load(core_, path.toUtf8().constData(), UT64_MAX)) { - eprintf("CANNOT GET RBIN INFO\n"); - } - } else { - if (!r_core_bin_load(core_, path.toUtf8().constData(), UT64_MAX)) { - eprintf("CANNOT GET RBIN INFO\n"); - } + if (loadbin && va) { + if (!r_core_bin_load(core_, path.toUtf8().constData(), baddr)) { + eprintf("CANNOT GET RBIN INFO\n"); } #if HAVE_MULTIPLE_RBIN_FILES_INSIDE_SELECT_WHICH_ONE @@ -247,7 +234,7 @@ bool CutterCore::loadFile(QString path, uint64_t loadaddr, uint64_t mapaddr, int } else { // load RBin information // XXX only for sub-bins - r_core_bin_load(core, path.toUtf8(), loadaddr); + r_core_bin_load(core, path.toUtf8(), baddr); r_bin_select_idx(core_->bin, NULL, idx); } #endif @@ -267,8 +254,6 @@ bool CutterCore::loadFile(QString path, uint64_t loadaddr, uint64_t mapaddr, int r_core_cmd0 (core_, "omfg+w"); } - setDefaultCPU(); - r_core_hash_load(core_, path.toUtf8().constData()); fflush(stdout); return true; @@ -541,16 +526,11 @@ void CutterCore::setConfig(const QString &k, const QVariant &v) } } -void CutterCore::setCPU(QString arch, QString cpu, int bits, bool temporary) +void CutterCore::setCPU(QString arch, QString cpu, int bits) { setConfig("asm.arch", arch); setConfig("asm.cpu", cpu); setConfig("asm.bits", bits); - if (!temporary) { - default_arch = arch; - default_cpu = cpu; - default_bits = bits; - } } void CutterCore::setEndianness(bool big) @@ -563,16 +543,6 @@ void CutterCore::setBBSize(int size) setConfig("anal.bb.maxsize", size); } -void CutterCore::setDefaultCPU() -{ - if (!default_arch.isEmpty()) - setConfig("asm.arch", default_arch); - if (!default_cpu.isEmpty()) - setConfig("asm.cpu", default_cpu); - if (default_bits) - setConfig("asm.bits", QString::number(default_bits)); -} - QString CutterCore::assemble(const QString &code) { CORE_LOCK(); diff --git a/src/Cutter.h b/src/Cutter.h index 595e74fd..e1dccd8a 100644 --- a/src/Cutter.h +++ b/src/Cutter.h @@ -325,8 +325,8 @@ public: void setImmediateBase(const QString &r2BaseName, RVA offset = RVA_INVALID); void setCurrentBits(int bits, RVA offset = RVA_INVALID); - bool loadFile(QString path, uint64_t loadaddr = 0LL, uint64_t mapaddr = 0LL, int perms = R_IO_READ, - int va = 0, int idx = 0, bool loadbin = false, const QString &forceBinPlugin = nullptr); + bool loadFile(QString path, ut64 baddr = 0LL, ut64 mapaddr = 0LL, int perms = R_IO_READ, + int va = 0, bool loadbin = false, const QString &forceBinPlugin = nullptr); bool tryFile(QString path, bool rw); void analyze(int level, QList advanced); @@ -371,8 +371,7 @@ public: QString assemble(const QString &code); QString disassemble(const QString &hex); QString disassembleSingleInstruction(RVA addr); - void setDefaultCPU(); - void setCPU(QString arch, QString cpu, int bits, bool temporary = false); + void setCPU(QString arch, QString cpu, int bits); void setEndianness(bool big); void setBBSize(int size); @@ -500,10 +499,6 @@ signals: public slots: private: - QString default_arch; - QString default_cpu; - int default_bits; - MemoryWidgetType memoryWidgetPriority; QString notes; diff --git a/src/dialogs/OptionsDialog.ui b/src/dialogs/OptionsDialog.ui index b75cb77f..01959d90 100644 --- a/src/dialogs/OptionsDialog.ui +++ b/src/dialogs/OptionsDialog.ui @@ -220,9 +220,9 @@ 0 - -636 + -672 564 - 831 + 867 @@ -791,7 +791,7 @@ - Load offset: + Load bin offset (-B) @@ -801,7 +801,7 @@ - 0 + false @@ -809,6 +809,9 @@ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + 1024 + @@ -826,14 +829,14 @@ - Map offset: + Map offset (-m) - 0 + false @@ -841,6 +844,9 @@ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + 0x40000 + @@ -871,7 +877,7 @@ - pdb file + PDB File path @@ -885,7 +891,6 @@ - @@ -928,7 +933,6 @@ - diff --git a/src/widgets/HexdumpWidget.cpp b/src/widgets/HexdumpWidget.cpp index 77b03ca1..6e5bf617 100644 --- a/src/widgets/HexdumpWidget.cpp +++ b/src/widgets/HexdumpWidget.cpp @@ -19,8 +19,6 @@ HexdumpWidget::HexdumpWidget(MainWindow *main, QAction *action) : { ui->setupUi(this); - //this->on_actionSettings_menu_1_triggered(); - // Setup hex highlight //connect(ui->hexHexText, SIGNAL(cursorPositionChanged()), this, SLOT(highlightHexCurrentLine())); //highlightHexCurrentLine(); @@ -297,8 +295,8 @@ void HexdumpWidget::refresh(RVA addr) // TODO: Figure out how to calculate a sane value for this bufferLines = qhelpers::getMaxFullyDisplayedLines(ui->hexHexText); - int loadLines = bufferLines * 3; // total lines to load - int curAddrLineOffset = bufferLines; // line number where seek should be + ut64 loadLines = bufferLines * 3; // total lines to load + ut64 curAddrLineOffset = bufferLines; // line number where seek should be if (addr < curAddrLineOffset * cols) { curAddrLineOffset = static_cast(addr / cols);