From e6c46c7f6b796f14a75cf5156e6166a333605641 Mon Sep 17 00:00:00 2001 From: NIRMAL MANOJ C Date: Sun, 21 Jun 2020 11:22:21 +0530 Subject: [PATCH 1/2] Shifted changes to this branch --- cutter-plugin/R2RetDec.cpp | 54 +++++-------------------------------- include/r2plugin/r2info.h | 2 +- include/r2plugin/r2retdec.h | 9 +++++++ src/core_plugin.cpp | 22 ++++++++++++--- src/r2info.cpp | 10 ++++--- 5 files changed, 40 insertions(+), 57 deletions(-) create mode 100644 include/r2plugin/r2retdec.h diff --git a/cutter-plugin/R2RetDec.cpp b/cutter-plugin/R2RetDec.cpp index e46e8e5..c913114 100644 --- a/cutter-plugin/R2RetDec.cpp +++ b/cutter-plugin/R2RetDec.cpp @@ -7,7 +7,7 @@ */ #include "R2RetDec.h" - +#include "../include/r2plugin/r2retdec.h" #include #include @@ -22,51 +22,9 @@ R2RetDec::R2RetDec(QObject *parent) void R2RetDec::decompileAt(RVA addr) { - if(task) - return; - - AnnotatedCode code = {}; - - task = new R2Task ("pdzj @ " + QString::number(addr)); - - connect(task, &R2Task::finished, this, [this]() { - AnnotatedCode code = {}; - QString s; - - QJsonObject json = task->getResultJson().object(); - delete task; - task = nullptr; - if(json.isEmpty()) - { - code.code = tr("Failed to parse JSON from r2retdec"); - emit finished(code); - return; - } - - auto root = json; - code.code = root["code"].toString(); - - for(QJsonValueRef annotationValue : root["annotations"].toArray()) - { - QJsonObject annotationObject = annotationValue.toObject(); - CodeAnnotation annotation = {}; - annotation.start = (size_t)annotationObject["start"].toVariant().toULongLong(); - annotation.end = (size_t)annotationObject["end"].toVariant().toULongLong(); - if(annotationObject["type"].toString() == "offset") - { - annotation.type = CodeAnnotation::Type::Offset; - annotation.offset.offset = annotationObject["offset"].toVariant().toULongLong(); - } - else - continue; - code.annotations.push_back(annotation); - } - - for(QJsonValueRef error : json["errors"].toArray()) - code.code += "// " + error.toString() + "\n"; - - emit finished(code); - }); - task->startTask(); - + RAnnotatedCode *code = r2retdec_decompile_annotated_code(Core()->core(), addr); + if(code == nullptr){ + code = r_annotated_code_new(strdup("RetDec Decompiler Error: No function at this offset")); + } + emit finished(code); } diff --git a/include/r2plugin/r2info.h b/include/r2plugin/r2info.h index 69beb3c..7ef8ded 100644 --- a/include/r2plugin/r2info.h +++ b/include/r2plugin/r2info.h @@ -29,7 +29,7 @@ class R2InfoProvider { public: std::string fetchFilePath() const; - common::Function fetchCurrentFunction() const; + common::Function fetchCurrentFunction(ut64 addr) const; void fetchFunctionsAndGlobals(config::Config &rdconfig) const; diff --git a/include/r2plugin/r2retdec.h b/include/r2plugin/r2retdec.h new file mode 100644 index 0000000..95a532d --- /dev/null +++ b/include/r2plugin/r2retdec.h @@ -0,0 +1,9 @@ +#ifndef R2RETDEC_H +#define R2RETDEC_H + +#include +#include + +RAnnotatedCode* r2retdec_decompile_annotated_code(RCore *core, ut64 addr); + +#endif //R2RETDEC_H diff --git a/src/core_plugin.cpp b/src/core_plugin.cpp index 04d3bbd..dc8e170 100644 --- a/src/core_plugin.cpp +++ b/src/core_plugin.cpp @@ -17,6 +17,7 @@ #include #include "r2plugin/cmd_exec.h" +#include "r2plugin/r2retdec.h" #include "r2plugin/r2cgen.h" #include "r2plugin/r2info.h" #include "r2plugin/r2utils.h" @@ -181,10 +182,12 @@ fs::path getOutDirPath() * @brief Main decompilation method. Uses RetDec to decompile input binary. * * Decompiles binary on input by configuring and calling RetDec decompiler script. - * + * Decompiles the binary given by the offset passed addr. + * * @param binInfo Provides informations gathered from r2 console. + * @param addr Decompiles the function at this offset. */ -RAnnotatedCode* decompile(const R2InfoProvider &binInfo) +RAnnotatedCode* decompile(const R2InfoProvider &binInfo, ut64 addr) { try { R2CGenerator outgen; @@ -198,7 +201,7 @@ RAnnotatedCode* decompile(const R2InfoProvider &binInfo) binInfo.fetchFunctionsAndGlobals(config); config.generateJsonFile(); - auto fnc = binInfo.fetchCurrentFunction(); + auto fnc = binInfo.fetchCurrentFunction(addr); auto decpath = outDir/"rd_dec.json"; auto outpath = outDir/"rd_out.log"; @@ -275,7 +278,7 @@ static void _cmd(RCore &core, const char &input) std::lock_guard lock (mutex); R2InfoProvider binInfo(core); - auto code = decompile(binInfo); + auto code = decompile(binInfo, core.offset); if (code == nullptr) { return; } @@ -283,6 +286,17 @@ static void _cmd(RCore &core, const char &input) outputFunction(code); } +/** + * This function is to get RAnnotatedCode to pass it to Cutter's decompiler widget. + */ +RAnnotatedCode* r2retdec_decompile_annotated_code(RCore *core, ut64 addr){ + static std::mutex mutex; + std::lock_guard lock (mutex); + + R2InfoProvider binInfo(*core); + return decompile(binInfo, addr); +} + /** * R2 console registration method. This method is called * after each command typed into r2. If the function wants diff --git a/src/r2info.cpp b/src/r2info.cpp index eefead4..01860d5 100644 --- a/src/r2info.cpp +++ b/src/r2info.cpp @@ -51,14 +51,16 @@ std::string R2InfoProvider::fetchFilePath() const } /** - * @brief Fetches the currently seeked function in Radare2 console. + * @brief Fetches the function at the address passed as parameter. + * + * @param addr Analyzes the function at the given address. */ -Function R2InfoProvider::fetchCurrentFunction() const +Function R2InfoProvider::fetchCurrentFunction(ut64 addr) const { - RAnalFunction *cf = r_anal_get_fcn_in(_r2core.anal, _r2core.offset, R_ANAL_FCN_TYPE_NULL); + RAnalFunction *cf = r_anal_get_fcn_in(_r2core.anal, addr, R_ANAL_FCN_TYPE_NULL); if (cf == nullptr) { std::ostringstream errMsg; - errMsg << "no function at offset 0x" << std::hex << _r2core.offset; + errMsg << "no function at offset 0x" << std::hex << addr; throw DecompilationError(errMsg.str()); } From 16e7afc42303ad6ecdb80b15cd9e5608412d3d5e Mon Sep 17 00:00:00 2001 From: NIRMAL MANOJ C Date: Sun, 21 Jun 2020 17:50:29 +0530 Subject: [PATCH 2/2] update CMakeLists.txt --- cutter-plugin/CMakeLists.txt | 1 + cutter-plugin/R2RetDec.cpp | 2 +- deps/cutter/CMakeLists.txt | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cutter-plugin/CMakeLists.txt b/cutter-plugin/CMakeLists.txt index 620b9ac..814592c 100644 --- a/cutter-plugin/CMakeLists.txt +++ b/cutter-plugin/CMakeLists.txt @@ -16,6 +16,7 @@ find_package(Qt5 REQUIRED COMPONENTS Widgets) add_library(r2retdec_cutter SHARED ${SOURCE}) target_link_libraries(r2retdec_cutter Qt5::Widgets) target_link_libraries(r2retdec_cutter Radare2::libr) +target_link_libraries(r2retdec_cutter core_retdec) #find_package(Cutter REQUIRED) target_link_libraries(r2retdec_cutter Cutter::Cutter) diff --git a/cutter-plugin/R2RetDec.cpp b/cutter-plugin/R2RetDec.cpp index c913114..59e10ac 100644 --- a/cutter-plugin/R2RetDec.cpp +++ b/cutter-plugin/R2RetDec.cpp @@ -7,7 +7,7 @@ */ #include "R2RetDec.h" -#include "../include/r2plugin/r2retdec.h" +#include "r2plugin/r2retdec.h" #include #include diff --git a/deps/cutter/CMakeLists.txt b/deps/cutter/CMakeLists.txt index 851796f..a72fc6f 100644 --- a/deps/cutter/CMakeLists.txt +++ b/deps/cutter/CMakeLists.txt @@ -4,7 +4,7 @@ include(FetchContent) FetchContent_Declare(cutter GIT_REPOSITORY https://github.com/radareorg/cutter - GIT_TAG v1.10.3 + GIT_TAG master ) FetchContent_GetProperties(cutter)