From a9ed742cad0cea675f6259c6baf91c22bf666a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Wed, 15 Jan 2020 20:19:24 +0100 Subject: [PATCH] Update radare2 and fix Cutter for it (#2017) --- radare2 | 2 +- src/core/Cutter.cpp | 20 ++++++++++++++------ src/core/Cutter.h | 12 ++++++++++++ src/core/CutterDescriptions.h | 6 ++++-- src/dialogs/EditFunctionDialog.cpp | 11 ----------- src/dialogs/EditFunctionDialog.ui | 16 +++------------- src/menus/DisassemblyContextMenu.cpp | 13 ++++--------- src/widgets/DisassemblerGraphView.cpp | 4 ++-- src/widgets/FunctionsWidget.cpp | 8 ++++---- src/widgets/HexWidget.h | 2 +- 10 files changed, 45 insertions(+), 49 deletions(-) diff --git a/radare2 b/radare2 index 2461780f..14215350 160000 --- a/radare2 +++ b/radare2 @@ -1 +1 @@ -Subproject commit 2461780fcd8140eb9fba6611ec54e468f82609dd +Subproject commit 14215350af35cc8b14710557c248d2e77baf380d diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 1523b0ce..030ccb69 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -996,13 +996,21 @@ QString CutterCore::disassembleSingleInstruction(RVA addr) return cmd("pi 1@" + QString::number(addr)).simplified(); } +RAnalFunction *CutterCore::functionIn(ut64 addr) +{ + CORE_LOCK(); + RList *fcns = r_anal_get_functions_in (core->anal, addr); + RAnalFunction *fcn = !r_list_empty(fcns) ? reinterpret_cast(r_list_first(fcns)) : nullptr; + r_list_free(fcns); + return fcn; +} + RAnalFunction *CutterCore::functionAt(ut64 addr) { CORE_LOCK(); - return r_anal_get_fcn_in(core->anal, addr, 0); + return r_anal_get_function_at(core->anal, addr); } - /** * @brief finds the start address of a function in a given address * @param addr - an address which belongs to a function @@ -1011,7 +1019,7 @@ RAnalFunction *CutterCore::functionAt(ut64 addr) RVA CutterCore::getFunctionStart(RVA addr) { CORE_LOCK(); - RAnalFunction *fcn = Core()->functionAt(addr); + RAnalFunction *fcn = Core()->functionIn(addr); return fcn ? fcn->addr : RVA_INVALID; } @@ -1023,7 +1031,7 @@ RVA CutterCore::getFunctionStart(RVA addr) RVA CutterCore::getFunctionEnd(RVA addr) { CORE_LOCK(); - RAnalFunction *fcn = Core()->functionAt(addr); + RAnalFunction *fcn = Core()->functionIn(addr); return fcn ? fcn->addr : RVA_INVALID; } @@ -1035,7 +1043,7 @@ RVA CutterCore::getFunctionEnd(RVA addr) RVA CutterCore::getLastFunctionInstruction(RVA addr) { CORE_LOCK(); - RAnalFunction *fcn = Core()->functionAt(addr); + RAnalFunction *fcn = Core()->functionIn(addr); if (!fcn) { return RVA_INVALID; } @@ -2343,7 +2351,7 @@ QList CutterCore::getAllFunctions() CutterRListForeach (core->anal->fcns, iter, RAnalFunction, fcn) { FunctionDescription function; function.offset = fcn->addr; - function.size = r_anal_fcn_size(fcn); + function.linearSize = r_anal_function_linear_size(fcn); function.nargs = r_anal_var_count(core->anal, fcn, 'b', 1) + r_anal_var_count(core->anal, fcn, 'r', 1) + r_anal_var_count(core->anal, fcn, 's', 1); diff --git a/src/core/Cutter.h b/src/core/Cutter.h index cb30a056..1324f1d4 100644 --- a/src/core/Cutter.h +++ b/src/core/Cutter.h @@ -111,7 +111,19 @@ public: void renameFunction(const QString &oldName, const QString &newName); void delFunction(RVA addr); void renameFlag(QString old_name, QString new_name); + + /** + * @param addr + * @return a function that contains addr or nullptr + */ + RAnalFunction *functionIn(ut64 addr); + + /** + * @param addr + * @return the function that has its entrypoint at addr or nullptr + */ RAnalFunction *functionAt(ut64 addr); + RVA getFunctionStart(RVA addr); RVA getFunctionEnd(RVA addr); RVA getLastFunctionInstruction(RVA addr); diff --git a/src/core/CutterDescriptions.h b/src/core/CutterDescriptions.h index 59d04ece..8cb35a60 100644 --- a/src/core/CutterDescriptions.h +++ b/src/core/CutterDescriptions.h @@ -13,7 +13,7 @@ struct FunctionDescription { RVA offset; - RVA size; + RVA linearSize; RVA nargs; RVA nbbs; RVA nlocals; @@ -24,7 +24,9 @@ struct FunctionDescription { bool contains(RVA addr) const { - return addr >= offset && addr < offset + size; + // TODO: this is not exactly correct in edge cases. + // r_anal_function_contains() does it right. + return addr >= offset && addr < offset + linearSize; } }; diff --git a/src/dialogs/EditFunctionDialog.cpp b/src/dialogs/EditFunctionDialog.cpp index 477590d4..4a2a6dc4 100644 --- a/src/dialogs/EditFunctionDialog.cpp +++ b/src/dialogs/EditFunctionDialog.cpp @@ -33,17 +33,6 @@ void EditFunctionDialog::setStartAddrText(const QString &startAddr) ui->startLineEdit->setText(startAddr); } -QString EditFunctionDialog::getEndAddrText() -{ - QString ret = ui->endLineEdit->text(); - return ret; -} - -void EditFunctionDialog::setEndAddrText(const QString &endAddr) -{ - ui->endLineEdit->setText(endAddr); -} - QString EditFunctionDialog::getStackSizeText() { QString ret = ui->stackSizeLineEdit->text(); diff --git a/src/dialogs/EditFunctionDialog.ui b/src/dialogs/EditFunctionDialog.ui index 0a363d60..a80ac149 100644 --- a/src/dialogs/EditFunctionDialog.ui +++ b/src/dialogs/EditFunctionDialog.ui @@ -58,33 +58,23 @@ - - - End address - - - - - - - Stack size - + - + Calling convention - + diff --git a/src/menus/DisassemblyContextMenu.cpp b/src/menus/DisassemblyContextMenu.cpp index 9aeb57fb..91c28078 100644 --- a/src/menus/DisassemblyContextMenu.cpp +++ b/src/menus/DisassemblyContextMenu.cpp @@ -450,8 +450,8 @@ void DisassemblyContextMenu::aboutToShowSlot() RCore *core = Core()->core(); - RAnalFunction *fcn = r_anal_get_fcn_at (core->anal, offset, R_ANAL_FCN_TYPE_NULL); - RAnalFunction *in_fcn = Core()->functionAt(offset); + RAnalFunction *fcn = Core()->functionAt(offset); + RAnalFunction *in_fcn = Core()->functionIn(offset); RFlagItem *f = r_flag_get_i (core->flags, offset); actionDeleteFlag.setVisible(f ? true : false); @@ -789,7 +789,7 @@ void DisassemblyContextMenu::on_actionRename_triggered() RenameDialog dialog(mainWindow); - RAnalFunction *fcn = r_anal_get_fcn_at (core->anal, offset, R_ANAL_FCN_TYPE_NULL); + RAnalFunction *fcn = Core()->functionIn (offset); RFlagItem *f = r_flag_get_i (core->flags, offset); if (fcn) { /* Rename function */ @@ -855,7 +855,7 @@ void DisassemblyContextMenu::on_actionRenameUsedHere_triggered() void DisassemblyContextMenu::on_actionSetFunctionVarTypes_triggered() { - RAnalFunction *fcn = Core()->functionAt(offset); + RAnalFunction *fcn = Core()->functionIn(offset); if (!fcn) { QMessageBox::critical(this, tr("Re-type function local vars"), @@ -1001,9 +1001,6 @@ void DisassemblyContextMenu::on_actionEditFunction_triggered() QString startAddrText = "0x" + QString::number(fcn->addr, 16); dialog.setStartAddrText(startAddrText); - QString endAddrText = "0x" + QString::number(fcn->addr + fcn->_size, 16); - dialog.setEndAddrText(endAddrText); - QString stackSizeText; stackSizeText.sprintf("%d", fcn->stack); dialog.setStackSizeText(stackSizeText); @@ -1019,8 +1016,6 @@ void DisassemblyContextMenu::on_actionEditFunction_triggered() Core()->renameFunction(fcn->name, new_name); QString new_start_addr = dialog.getStartAddrText(); fcn->addr = Core()->math(new_start_addr); - QString new_end_addr = dialog.getEndAddrText(); - Core()->cmd("afu " + new_end_addr); QString new_stack_size = dialog.getStackSizeText(); fcn->stack = int(Core()->math(new_stack_size)); Core()->cmd("afc " + dialog.getCallConSelected()); diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index 570d81c4..3ab08919 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -226,7 +226,7 @@ void DisassemblerGraphView::loadCurrentGraph() .set("asm.lines.fcn", false); QJsonArray functions; - RAnalFunction *fcn = Core()->functionAt(seekable->getOffset()); + RAnalFunction *fcn = Core()->functionIn(seekable->getOffset()); if (fcn) { currentFcnAddr = fcn->addr; QJsonDocument functionsDoc = Core()->cmdj("agJ " + RAddressString(fcn->addr)); @@ -1091,7 +1091,7 @@ void DisassemblerGraphView::on_actionExportGraph_triggered() } QString defaultName = "graph"; - if (auto f = Core()->functionAt(currentFcnAddr)) { + if (auto f = Core()->functionIn(currentFcnAddr)) { QString functionName = f->name; // don't confuse image type guessing and make c++ names somewhat usable functionName.replace(QRegularExpression("[.:]"), "_"); diff --git a/src/widgets/FunctionsWidget.cpp b/src/widgets/FunctionsWidget.cpp index 57a69267..7afc8b78 100644 --- a/src/widgets/FunctionsWidget.cpp +++ b/src/widgets/FunctionsWidget.cpp @@ -122,7 +122,7 @@ QVariant FunctionModel::data(const QModelIndex &index, int role) const case 0: return tr("Offset: %1").arg(RAddressString(function.offset)); case 1: - return tr("Size: %1").arg(RSizeString(function.size)); + return tr("Size: %1").arg(RSizeString(function.linearSize)); case 2: return tr("Import: %1").arg(functionIsImport(function.offset) ? tr("true") : tr("false")); case 3: @@ -147,7 +147,7 @@ QVariant FunctionModel::data(const QModelIndex &index, int role) const case NameColumn: return function.name; case SizeColumn: - return QString::number(function.size); + return QString::number(function.linearSize); case OffsetColumn: return RAddressString(function.offset); case NargsColumn: @@ -383,8 +383,8 @@ bool FunctionSortFilterProxyModel::lessThan(const QModelIndex &left, const QMode case FunctionModel::OffsetColumn: return left_function.offset < right_function.offset; case FunctionModel::SizeColumn: - if (left_function.size != right_function.size) - return left_function.size < right_function.size; + if (left_function.linearSize != right_function.linearSize) + return left_function.linearSize < right_function.linearSize; break; case FunctionModel::ImportColumn: { bool left_is_import = left.data(FunctionModel::IsImportRole).toBool(); diff --git a/src/widgets/HexWidget.h b/src/widgets/HexWidget.h index ebb561c8..b7e35381 100644 --- a/src/widgets/HexWidget.h +++ b/src/widgets/HexWidget.h @@ -134,7 +134,7 @@ public: } m_blocks.clear(); uint64_t addr = alignedAddr; - for (int i = 0; i < len / blockSize; ++i, addr += blockSize) { + for (ut64 i = 0; i < len / blockSize; ++i, addr += blockSize) { m_blocks.append(Core()->ioRead(addr, blockSize)); } }