From 99070e86b950b9862dd3f8bc820de71f15ee2b0d Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Thu, 23 Sep 2021 10:15:03 +0200 Subject: [PATCH] Use new Rizin API instead of cmdRaw/cmdRawAt (#2770) * Use new Rizin API instead of cmdRaw/cmdRawAt * add CutterRzListForeach as a foreach-keyword for clang-format --- _clang-format | 1 + src/common/AnalysisTask.cpp | 3 +- src/core/Cutter.cpp | 78 ++++++++++++++++------------- src/dialogs/EditVariablesDialog.cpp | 24 +++++++-- src/dialogs/LinkTypeDialog.cpp | 23 +++++---- src/widgets/HexWidget.cpp | 40 +++++++++------ 6 files changed, 105 insertions(+), 64 deletions(-) diff --git a/_clang-format b/_clang-format index 65079d08..67f84f00 100644 --- a/_clang-format +++ b/_clang-format @@ -66,6 +66,7 @@ ForEachMacros: - Q_FOREVER - QBENCHMARK - QBENCHMARK_ONCE + - CutterRzListForeach IncludeBlocks: Preserve IncludeCategories: - Regex: '^"(llvm|llvm-c|clang|clang-c)/' diff --git a/src/common/AnalysisTask.cpp b/src/common/AnalysisTask.cpp index 5752a470..75e90395 100644 --- a/src/common/AnalysisTask.cpp +++ b/src/common/AnalysisTask.cpp @@ -64,7 +64,8 @@ void AnalysisTask::runTask() } if (!options.os.isNull()) { - Core()->cmdRaw("e asm.os=" + options.os); + RzCoreLocked core(Core()); + rz_config_set(core->config, "asm.os", options.os.toUtf8().constData()); } if (!options.pdbFile.isNull()) { diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 6b09954a..a2d81fb7 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -390,7 +390,7 @@ bool CutterCore::isRedirectableDebugee() RzList *descs = rz_id_storage_list(core->io->files); RzListIter *it; RzIODesc *desc; - CutterRzListForeach(descs, it, RzIODesc, desc) { + CutterRzListForeach (descs, it, RzIODesc, desc) { QString URI = QString(desc->uri); if (URI.contains("ptrace") | URI.contains("mach")) { return true; @@ -771,7 +771,12 @@ void CutterCore::editBytes(RVA addr, const QString &bytes) void CutterCore::editBytesEndian(RVA addr, const QString &bytes) { - cmdRawAt(QString("wv %1").arg(bytes), addr); + 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(); } @@ -938,7 +943,12 @@ void CutterCore::seekAndShow(QString offset) void CutterCore::seek(QString thing) { - cmdRaw(QString("s %1").arg(thing)); + CORE_LOCK(); + ut64 addr = rz_num_math(core->num, thing.toUtf8().constData()); + if (core->num->nc.errors) { + return; + } + rz_core_seek_and_save(core, addr, true); updateSeek(); } @@ -1365,8 +1375,8 @@ RefDescription CutterCore::formatRefDesc(QJsonObject refItem) break; } if (!refItem["value"].isNull()) { - appendVar(desc.ref, RzAddressString(refItem["value"].toVariant().toULongLong()), " ", - ""); + appendVar(desc.ref, RzAddressString(refItem["value"].toVariant().toULongLong()), + " ", ""); } refItem = refItem["ref"].toObject(); } while (!refItem.empty()); @@ -1580,8 +1590,7 @@ QVector CutterCore::getHeapChunks(RVA arena_addr) RzList *chunks = rz_heap_chunks_list(core, m_arena); RzListIter *iter; RzHeapChunkListItem *data; - CutterRzListForeach(chunks, iter, RzHeapChunkListItem, data) - { + CutterRzListForeach (chunks, iter, RzHeapChunkListItem, data) { Chunk chunk; chunk.offset = data->addr; chunk.size = (int)data->size; @@ -1608,8 +1617,7 @@ QVector CutterCore::getArenas() RzList *arenas = rz_heap_arenas_list(core); RzListIter *iter; RzArenaListItem *data; - CutterRzListForeach(arenas, iter, RzArenaListItem, data) - { + CutterRzListForeach (arenas, iter, RzArenaListItem, data) { Arena arena; arena.offset = data->addr; arena.type = QString(data->type); @@ -1670,8 +1678,7 @@ QVector CutterCore::getHeapBins(ut64 arena_addr) RzList *tcache_bins = rz_heap_tcache_content(core, arena_addr); RzListIter *iter; RzHeapBin *bin; - CutterRzListForeach(tcache_bins, iter, RzHeapBin, bin) - { + CutterRzListForeach (tcache_bins, iter, RzHeapBin, bin) { if (!bin) { continue; } @@ -1918,7 +1925,7 @@ void CutterCore::attachRemote(const QString &uri) RzList *descs = rz_id_storage_list(core->io->files); RzListIter *it; RzIODesc *desc; - CutterRzListForeach(descs, it, RzIODesc, desc) { + CutterRzListForeach (descs, it, RzIODesc, desc) { QString fileUri = QString(desc->uri); if (!fileUri.compare(uri)) { connected = true; @@ -2030,7 +2037,7 @@ void CutterCore::stopDebug() RzList *descs = rz_id_storage_list(core->io->files); RzListIter *it; RzIODesc *desc; - CutterRzListForeach(descs, it, RzIODesc, desc) { + CutterRzListForeach (descs, it, RzIODesc, desc) { QString URI = QString(desc->uri); if (URI.contains("ptrace")) { ptraceFiles += "o-" + QString::number(desc->fd) + ";"; @@ -2689,7 +2696,9 @@ QList CutterCore::getSeekHistory() RzListIter *it; RzCoreSeekItem *undo; RzList *list = rz_core_seek_list(core); - CutterRzListForeach(list, it, RzCoreSeekItem, undo) { ret << undo->offset; } + CutterRzListForeach (list, it, RzCoreSeekItem, undo) { + ret << undo->offset; + } return ret; } @@ -2701,7 +2710,9 @@ QStringList CutterCore::getAsmPluginNames() QStringList ret; RzAsmPlugin *ap; - CutterRzListForeach(core->rasm->plugins, it, RzAsmPlugin, ap) { ret << ap->name; } + CutterRzListForeach (core->rasm->plugins, it, RzAsmPlugin, ap) { + ret << ap->name; + } return ret; } @@ -2713,7 +2724,9 @@ QStringList CutterCore::getAnalysisPluginNames() QStringList ret; RzAnalysisPlugin *ap; - CutterRzListForeach(core->analysis->plugins, it, RzAnalysisPlugin, ap) { ret << ap->name; } + CutterRzListForeach (core->analysis->plugins, it, RzAnalysisPlugin, ap) { + ret << ap->name; + } return ret; } @@ -2724,7 +2737,7 @@ QList CutterCore::getRBinPluginDescriptions(const QStrin QList ret; RzListIter *it; RzBinPlugin *bp; - CutterRzListForeach(core->bin->plugins, it, RzBinPlugin, bp) { + CutterRzListForeach (core->bin->plugins, it, RzBinPlugin, bp) { RzBinPluginDescription desc; desc.name = bp->name ? bp->name : ""; desc.description = bp->desc ? bp->desc : ""; @@ -2733,7 +2746,7 @@ QList CutterCore::getRBinPluginDescriptions(const QStrin ret.append(desc); } RzBinXtrPlugin *bx; - CutterRzListForeach(core->bin->binxtrs, it, RzBinXtrPlugin, bx) { + CutterRzListForeach (core->bin->binxtrs, it, RzBinXtrPlugin, bx) { RzBinPluginDescription desc; desc.name = bx->name ? bx->name : ""; desc.description = bx->desc ? bx->desc : ""; @@ -2750,15 +2763,12 @@ QList CutterCore::getRIOPluginDescriptions() QList ret; RzListIter *it; RzIOPlugin *p; - CutterRzListForeach(core->io->plugins, it, RzIOPlugin, p) { + CutterRzListForeach (core->io->plugins, it, RzIOPlugin, p) { RzIOPluginDescription desc; desc.name = p->name ? p->name : ""; desc.description = p->desc ? p->desc : ""; desc.license = p->license ? p->license : ""; - desc.permissions = - QString("r") + - (p->write ? "w" : "_") + - (p->isdbg ? "d" : "_"); + desc.permissions = QString("r") + (p->write ? "w" : "_") + (p->isdbg ? "d" : "_"); if (p->uris) { desc.uris = QString::fromUtf8(p->uris).split(","); } @@ -2773,7 +2783,7 @@ QList CutterCore::getRCorePluginDescriptions() QList ret; RzListIter *it; RzCorePlugin *p; - CutterRzListForeach(core->plugins, it, RzCorePlugin, p) { + CutterRzListForeach (core->plugins, it, RzCorePlugin, p) { RzCorePluginDescription desc; desc.name = p->name ? p->name : ""; desc.description = p->desc ? p->desc : ""; @@ -2790,8 +2800,7 @@ QList CutterCore::getRAsmPluginDescriptions() QList ret; RzAsmPlugin *ap; - CutterRzListForeach(core->rasm->plugins, it, RzAsmPlugin, ap) - { + CutterRzListForeach (core->rasm->plugins, it, RzAsmPlugin, ap) { RzAsmPluginDescription plugin; plugin.name = ap->name; @@ -2817,8 +2826,7 @@ QList CutterCore::getAllFunctions() RzListIter *iter; RzAnalysisFunction *fcn; - CutterRzListForeach(core->analysis->fcns, iter, RzAnalysisFunction, fcn) - { + CutterRzListForeach (core->analysis->fcns, iter, RzAnalysisFunction, fcn) { FunctionDescription function; function.offset = fcn->addr; function.linearSize = rz_analysis_function_linear_size(fcn); @@ -2898,8 +2906,7 @@ QList CutterCore::getAllSymbols() RzBinSymbol *bs; if (core && core->bin && core->bin->cur && core->bin->cur->o) { - CutterRzListForeach(core->bin->cur->o->symbols, it, RzBinSymbol, bs) - { + CutterRzListForeach (core->bin->cur->o->symbols, it, RzBinSymbol, bs) { QString type = QString(bs->bind) + " " + QString(bs->type); SymbolDescription symbol; symbol.vaddr = bs->vaddr; @@ -2912,8 +2919,7 @@ QList CutterCore::getAllSymbols() /* list entrypoints as symbols too */ int n = 0; RzBinAddr *entry; - CutterRzListForeach(core->bin->cur->o->entries, it, RzBinAddr, entry) - { + CutterRzListForeach (core->bin->cur->o->entries, it, RzBinAddr, entry) { SymbolDescription symbol; symbol.vaddr = entry->vaddr; symbol.name = QString("entry") + QString::number(n++); @@ -3415,7 +3421,8 @@ void CutterCore::deleteClass(const QString &cls) rz_analysis_class_delete(core->analysis, cls.toUtf8().constData()); } -bool CutterCore::getAnalysisMethod(const QString &cls, const QString &meth, AnalysisMethodDescription *desc) +bool CutterCore::getAnalysisMethod(const QString &cls, const QString &meth, + AnalysisMethodDescription *desc) { CORE_LOCK(); RzAnalysisMethod analysisMeth; @@ -3443,7 +3450,7 @@ void CutterCore::setAnalysisMethod(const QString &className, const AnalysisMetho } void CutterCore::renameAnalysisMethod(const QString &className, const QString &oldMethodName, - const QString &newMethodName) + const QString &newMethodName) { CORE_LOCK(); rz_analysis_class_method_rename(core->analysis, className.toUtf8().constData(), @@ -3855,7 +3862,8 @@ void CutterCore::triggerFunctionRenamed(const RVA offset, const QString &newName void CutterCore::loadPDB(const QString &file) { - cmdRaw("idp " + sanitizeStringForCommand(file)); + CORE_LOCK(); + rz_core_bin_pdb_load(core, file.toUtf8().constData()); } QList CutterCore::disassembleLines(RVA offset, int lines) diff --git a/src/dialogs/EditVariablesDialog.cpp b/src/dialogs/EditVariablesDialog.cpp index 08966071..5525048f 100644 --- a/src/dialogs/EditVariablesDialog.cpp +++ b/src/dialogs/EditVariablesDialog.cpp @@ -55,7 +55,25 @@ void EditVariablesDialog::applyFields() } VariableDescription desc = ui->dropdownLocalVars->currentData().value(); - Core()->cmdRaw(QString("afvt %1 %2").arg(desc.name).arg(ui->typeComboBox->currentText())); + RzCoreLocked core(Core()); + RzAnalysisFunction *fcn = Core()->functionIn(core->offset); + if (!fcn) { + return; + } + + RzAnalysisVar *v = rz_analysis_function_get_var_byname(fcn, desc.name.toUtf8().constData()); + if (!v) { + return; + } + + char *error_msg = NULL; + RzType *v_type = rz_type_parse_string_single( + core->analysis->typedb->parser, ui->typeComboBox->currentText().toUtf8().constData(), + &error_msg); + if (!v_type || error_msg) { + return; + } + rz_analysis_var_set_type(v, v_type); // TODO Remove all those replace once rizin command parser is fixed QString newName = ui->nameEdit->text() @@ -84,7 +102,8 @@ void EditVariablesDialog::updateFields() ui->typeComboBox->setCurrentText(desc.type); } -static void addTypeDescriptionsToComboBox(QComboBox *comboBox, QList list) { +static void addTypeDescriptionsToComboBox(QComboBox *comboBox, QList list) +{ for (const TypeDescription &thisType : list) { comboBox->addItem(thisType.type); } @@ -97,5 +116,4 @@ void EditVariablesDialog::populateTypesComboBox() addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllPrimitiveTypes()); addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllEnums()); addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllTypedefs()); - } diff --git a/src/dialogs/LinkTypeDialog.cpp b/src/dialogs/LinkTypeDialog.cpp index fc1c95ea..6d2a9110 100644 --- a/src/dialogs/LinkTypeDialog.cpp +++ b/src/dialogs/LinkTypeDialog.cpp @@ -56,10 +56,14 @@ void LinkTypeDialog::done(int r) QString type = ui->structureTypeComboBox->currentText(); if (type == tr("(No Type)")) { // Delete link - Core()->cmdRaw("tl- " + address); + RzCoreLocked core(Core()); + ut64 addr = rz_num_math(core->num, address.toUtf8().constData()); + rz_analysis_type_unlink(core->analysis, addr); } else { // Create link - Core()->cmdRaw(QString("tl %1 = %2").arg(type).arg(address)); + RzCoreLocked core(Core()); + ut64 addr = rz_num_math(core->num, address.toUtf8().constData()); + rz_core_types_link(core, type.toUtf8().constData(), addr); } QDialog::done(r); @@ -84,16 +88,17 @@ QString LinkTypeDialog::findLinkedType(RVA address) return QString(); } - QString ret = Core()->cmdRaw(QString("tls %1").arg(address)); - if (ret.isEmpty()) { - // return empty string since the current address is not linked to a type + RzCoreLocked core(Core()); + RzType *link = rz_analysis_type_link_at(core->analysis, address); + if (!link) { + return QString(); + } + RzBaseType *base = rz_type_get_base_type(core->analysis->typedb, link); + if (!base) { return QString(); } - // Extract the given type from returned data - // TODO: Implement "tlsj" in Rizin or some other function to directly get linked type - QString s = ret.section(QLatin1Char('\n'), 0, 0); - return s.mid(1, s.size() - 2); + return QString(base->name); } void LinkTypeDialog::on_exprLineEdit_textChanged(const QString &text) diff --git a/src/widgets/HexWidget.cpp b/src/widgets/HexWidget.cpp index 914cb6c3..784528a9 100644 --- a/src/widgets/HexWidget.cpp +++ b/src/widgets/HexWidget.cpp @@ -704,7 +704,8 @@ void HexWidget::w_writeString() d.setInputMode(QInputDialog::InputMode::TextInput); QString str = d.getText(this, tr("Write string"), tr("String:"), QLineEdit::Normal, "", &ok); if (ok && !str.isEmpty()) { - Core()->cmdRawAt(QString("w %1").arg(str), getLocationAddress()); + RzCoreLocked core(Core()); + rz_core_write_string_at(core, getLocationAddress(), str.toUtf8().constData()); refresh(); } } @@ -719,12 +720,13 @@ void HexWidget::w_increaseDecrease() if (ret == QDialog::Rejected) { return; } - QString mode = d.getMode() == IncrementDecrementDialog::Increase ? "+" : "-"; - Core()->cmdRawAt(QString("w%1%2 %3") - .arg(QString::number(d.getNBytes())) - .arg(mode) - .arg(QString::number(d.getValue())), - getLocationAddress()); + int64_t value = (int64_t)d.getValue(); + uint8_t sz = d.getNBytes(); + if (!d.getMode() == IncrementDecrementDialog::Increase) { + value *= -1; + } + RzCoreLocked core(Core()); + rz_core_write_value_inc_at(core, getLocationAddress(), value, sz); refresh(); } @@ -741,10 +743,14 @@ void HexWidget::w_writeZeros() size = static_cast(selection.size()); } - QString str = QString::number( - d.getInt(this, tr("Write zeros"), tr("Number of zeros:"), size, 1, 0x7FFFFFFF, 1, &ok)); - if (ok && !str.isEmpty()) { - Core()->cmdRawAt(QString("w0 %1").arg(str), getLocationAddress()); + int len = + d.getInt(this, tr("Write zeros"), tr("Number of zeros:"), size, 1, 0x7FFFFFFF, 1, &ok); + if (ok) { + RzCoreLocked core(Core()); + uint8_t *buf = (uint8_t *)calloc(len, sizeof(uint8_t)); + rz_core_write_at(core, getLocationAddress(), buf, len); + free(buf); + refresh(); } } @@ -759,10 +765,9 @@ void HexWidget::w_write64() if (ret == QDialog::Rejected) { return; } - QString mode = d.getMode() == Base64EnDecodedWriteDialog::Encode ? "e" : "d"; QByteArray str = d.getData(); - if (mode == "d" + if (d.getMode() == Base64EnDecodedWriteDialog::Decode && (QString(str).contains(QRegularExpression("[^a-zA-Z0-9+/=]")) || str.length() % 4 != 0 || str.isEmpty())) { QMessageBox::critical( @@ -772,9 +777,12 @@ void HexWidget::w_write64() return; } - Core()->cmdRawAt(QString("w6%1 %2").arg(mode).arg( - (mode == "e" ? str.toHex() : str).toStdString().c_str()), - getLocationAddress()); + RzCoreLocked core(Core()); + if (d.getMode() == Base64EnDecodedWriteDialog::Encode) { + rz_core_write_base64_at(core, getLocationAddress(), str.toHex().constData()); + } else { + rz_core_write_base64d_at(core, getLocationAddress(), str.constData()); + } refresh(); }