From 7cf654ec3016913188b90b09f0827043df0004cc Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 21 Apr 2021 00:19:20 +0200 Subject: [PATCH 1/2] WIP: cache decompiler output --- src/common/R2GhidraCmdDecompiler.cpp | 16 ++++++++++++++-- src/common/R2GhidraCmdDecompiler.h | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/common/R2GhidraCmdDecompiler.cpp b/src/common/R2GhidraCmdDecompiler.cpp index d2c405b4..478a1aa0 100644 --- a/src/common/R2GhidraCmdDecompiler.cpp +++ b/src/common/R2GhidraCmdDecompiler.cpp @@ -16,6 +16,7 @@ R2GhidraCmdDecompiler::R2GhidraCmdDecompiler(QObject *parent) : Decompiler("pdg", "pdg", parent) { task = nullptr; + this->cache = new QHash(); } bool R2GhidraCmdDecompiler::isAvailable() @@ -28,8 +29,16 @@ void R2GhidraCmdDecompiler::decompileAt(RVA addr) if (task) { return; } - task = new R2Task("pdgj @ " + QString::number(addr)); - connect(task, &R2Task::finished, this, [this]() { + QString k = QString::number(addr); + bool is_cached = false; // this->cache->contains (k); + task = new R2Task(is_cached? "?e": "pdgj @ " + k); + connect(task, &R2Task::finished, this, [this, addr, k, is_cached]() { + if (is_cached) { + RCodeMeta *code = r_codemeta_clone (this->cache->value(k)); + delete task; + emit finished(code); + return; + } QJsonObject json = task->getResultJson().object(); delete task; task = nullptr; @@ -65,6 +74,9 @@ void R2GhidraCmdDecompiler::decompileAt(RVA addr) } std::string tmp = codeString.toStdString(); code->code = strdup(tmp.c_str()); + if (!is_cached) { + // this->cache->insert(k, r_codemeta_clone (code)); + } emit finished(code); }); task->startTask(); diff --git a/src/common/R2GhidraCmdDecompiler.h b/src/common/R2GhidraCmdDecompiler.h index f1633b26..921dd1df 100644 --- a/src/common/R2GhidraCmdDecompiler.h +++ b/src/common/R2GhidraCmdDecompiler.h @@ -14,11 +14,11 @@ class R2GhidraCmdDecompiler: public Decompiler private: R2Task *task; + QHash *cache; public: explicit R2GhidraCmdDecompiler(QObject *parent = nullptr); void decompileAt(RVA addr) override; - bool isRunning() override { return task != nullptr; } static bool isAvailable(); From e587bc04f16e7d47a0e9cb1de1e622749c7b637e Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 21 Apr 2021 00:20:49 +0200 Subject: [PATCH 2/2] Fix --- src/common/R2GhidraCmdDecompiler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/R2GhidraCmdDecompiler.cpp b/src/common/R2GhidraCmdDecompiler.cpp index 478a1aa0..a821bfc9 100644 --- a/src/common/R2GhidraCmdDecompiler.cpp +++ b/src/common/R2GhidraCmdDecompiler.cpp @@ -30,7 +30,7 @@ void R2GhidraCmdDecompiler::decompileAt(RVA addr) return; } QString k = QString::number(addr); - bool is_cached = false; // this->cache->contains (k); + bool is_cached = this->cache->contains (k); task = new R2Task(is_cached? "?e": "pdgj @ " + k); connect(task, &R2Task::finished, this, [this, addr, k, is_cached]() { if (is_cached) { @@ -75,7 +75,7 @@ void R2GhidraCmdDecompiler::decompileAt(RVA addr) std::string tmp = codeString.toStdString(); code->code = strdup(tmp.c_str()); if (!is_cached) { - // this->cache->insert(k, r_codemeta_clone (code)); + this->cache->insert(k, r_codemeta_clone (code)); } emit finished(code); });