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
This commit is contained in:
Riccardo Schirone 2021-09-23 10:15:03 +02:00 committed by GitHub
parent 48c884c3d6
commit 99070e86b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 64 deletions

View File

@ -66,6 +66,7 @@ ForEachMacros:
- Q_FOREVER - Q_FOREVER
- QBENCHMARK - QBENCHMARK
- QBENCHMARK_ONCE - QBENCHMARK_ONCE
- CutterRzListForeach
IncludeBlocks: Preserve IncludeBlocks: Preserve
IncludeCategories: IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Regex: '^"(llvm|llvm-c|clang|clang-c)/'

View File

@ -64,7 +64,8 @@ void AnalysisTask::runTask()
} }
if (!options.os.isNull()) { 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()) { if (!options.pdbFile.isNull()) {

View File

@ -390,7 +390,7 @@ bool CutterCore::isRedirectableDebugee()
RzList *descs = rz_id_storage_list(core->io->files); RzList *descs = rz_id_storage_list(core->io->files);
RzListIter *it; RzListIter *it;
RzIODesc *desc; RzIODesc *desc;
CutterRzListForeach(descs, it, RzIODesc, desc) { CutterRzListForeach (descs, it, RzIODesc, desc) {
QString URI = QString(desc->uri); QString URI = QString(desc->uri);
if (URI.contains("ptrace") | URI.contains("mach")) { if (URI.contains("ptrace") | URI.contains("mach")) {
return true; return true;
@ -771,7 +771,12 @@ void CutterCore::editBytes(RVA addr, const QString &bytes)
void CutterCore::editBytesEndian(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(); emit stackChanged();
} }
@ -938,7 +943,12 @@ void CutterCore::seekAndShow(QString offset)
void CutterCore::seek(QString thing) 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(); updateSeek();
} }
@ -1365,8 +1375,8 @@ RefDescription CutterCore::formatRefDesc(QJsonObject refItem)
break; break;
} }
if (!refItem["value"].isNull()) { if (!refItem["value"].isNull()) {
appendVar(desc.ref, RzAddressString(refItem["value"].toVariant().toULongLong()), " ", appendVar(desc.ref, RzAddressString(refItem["value"].toVariant().toULongLong()),
""); " ", "");
} }
refItem = refItem["ref"].toObject(); refItem = refItem["ref"].toObject();
} while (!refItem.empty()); } while (!refItem.empty());
@ -1580,8 +1590,7 @@ QVector<Chunk> CutterCore::getHeapChunks(RVA arena_addr)
RzList *chunks = rz_heap_chunks_list(core, m_arena); RzList *chunks = rz_heap_chunks_list(core, m_arena);
RzListIter *iter; RzListIter *iter;
RzHeapChunkListItem *data; RzHeapChunkListItem *data;
CutterRzListForeach(chunks, iter, RzHeapChunkListItem, data) CutterRzListForeach (chunks, iter, RzHeapChunkListItem, data) {
{
Chunk chunk; Chunk chunk;
chunk.offset = data->addr; chunk.offset = data->addr;
chunk.size = (int)data->size; chunk.size = (int)data->size;
@ -1608,8 +1617,7 @@ QVector<Arena> CutterCore::getArenas()
RzList *arenas = rz_heap_arenas_list(core); RzList *arenas = rz_heap_arenas_list(core);
RzListIter *iter; RzListIter *iter;
RzArenaListItem *data; RzArenaListItem *data;
CutterRzListForeach(arenas, iter, RzArenaListItem, data) CutterRzListForeach (arenas, iter, RzArenaListItem, data) {
{
Arena arena; Arena arena;
arena.offset = data->addr; arena.offset = data->addr;
arena.type = QString(data->type); arena.type = QString(data->type);
@ -1670,8 +1678,7 @@ QVector<RzHeapBin *> CutterCore::getHeapBins(ut64 arena_addr)
RzList *tcache_bins = rz_heap_tcache_content(core, arena_addr); RzList *tcache_bins = rz_heap_tcache_content(core, arena_addr);
RzListIter *iter; RzListIter *iter;
RzHeapBin *bin; RzHeapBin *bin;
CutterRzListForeach(tcache_bins, iter, RzHeapBin, bin) CutterRzListForeach (tcache_bins, iter, RzHeapBin, bin) {
{
if (!bin) { if (!bin) {
continue; continue;
} }
@ -1918,7 +1925,7 @@ void CutterCore::attachRemote(const QString &uri)
RzList *descs = rz_id_storage_list(core->io->files); RzList *descs = rz_id_storage_list(core->io->files);
RzListIter *it; RzListIter *it;
RzIODesc *desc; RzIODesc *desc;
CutterRzListForeach(descs, it, RzIODesc, desc) { CutterRzListForeach (descs, it, RzIODesc, desc) {
QString fileUri = QString(desc->uri); QString fileUri = QString(desc->uri);
if (!fileUri.compare(uri)) { if (!fileUri.compare(uri)) {
connected = true; connected = true;
@ -2030,7 +2037,7 @@ void CutterCore::stopDebug()
RzList *descs = rz_id_storage_list(core->io->files); RzList *descs = rz_id_storage_list(core->io->files);
RzListIter *it; RzListIter *it;
RzIODesc *desc; RzIODesc *desc;
CutterRzListForeach(descs, it, RzIODesc, desc) { CutterRzListForeach (descs, it, RzIODesc, desc) {
QString URI = QString(desc->uri); QString URI = QString(desc->uri);
if (URI.contains("ptrace")) { if (URI.contains("ptrace")) {
ptraceFiles += "o-" + QString::number(desc->fd) + ";"; ptraceFiles += "o-" + QString::number(desc->fd) + ";";
@ -2689,7 +2696,9 @@ QList<RVA> CutterCore::getSeekHistory()
RzListIter *it; RzListIter *it;
RzCoreSeekItem *undo; RzCoreSeekItem *undo;
RzList *list = rz_core_seek_list(core); 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; return ret;
} }
@ -2701,7 +2710,9 @@ QStringList CutterCore::getAsmPluginNames()
QStringList ret; QStringList ret;
RzAsmPlugin *ap; RzAsmPlugin *ap;
CutterRzListForeach(core->rasm->plugins, it, RzAsmPlugin, ap) { ret << ap->name; } CutterRzListForeach (core->rasm->plugins, it, RzAsmPlugin, ap) {
ret << ap->name;
}
return ret; return ret;
} }
@ -2713,7 +2724,9 @@ QStringList CutterCore::getAnalysisPluginNames()
QStringList ret; QStringList ret;
RzAnalysisPlugin *ap; RzAnalysisPlugin *ap;
CutterRzListForeach(core->analysis->plugins, it, RzAnalysisPlugin, ap) { ret << ap->name; } CutterRzListForeach (core->analysis->plugins, it, RzAnalysisPlugin, ap) {
ret << ap->name;
}
return ret; return ret;
} }
@ -2724,7 +2737,7 @@ QList<RzBinPluginDescription> CutterCore::getRBinPluginDescriptions(const QStrin
QList<RzBinPluginDescription> ret; QList<RzBinPluginDescription> ret;
RzListIter *it; RzListIter *it;
RzBinPlugin *bp; RzBinPlugin *bp;
CutterRzListForeach(core->bin->plugins, it, RzBinPlugin, bp) { CutterRzListForeach (core->bin->plugins, it, RzBinPlugin, bp) {
RzBinPluginDescription desc; RzBinPluginDescription desc;
desc.name = bp->name ? bp->name : ""; desc.name = bp->name ? bp->name : "";
desc.description = bp->desc ? bp->desc : ""; desc.description = bp->desc ? bp->desc : "";
@ -2733,7 +2746,7 @@ QList<RzBinPluginDescription> CutterCore::getRBinPluginDescriptions(const QStrin
ret.append(desc); ret.append(desc);
} }
RzBinXtrPlugin *bx; RzBinXtrPlugin *bx;
CutterRzListForeach(core->bin->binxtrs, it, RzBinXtrPlugin, bx) { CutterRzListForeach (core->bin->binxtrs, it, RzBinXtrPlugin, bx) {
RzBinPluginDescription desc; RzBinPluginDescription desc;
desc.name = bx->name ? bx->name : ""; desc.name = bx->name ? bx->name : "";
desc.description = bx->desc ? bx->desc : ""; desc.description = bx->desc ? bx->desc : "";
@ -2750,15 +2763,12 @@ QList<RzIOPluginDescription> CutterCore::getRIOPluginDescriptions()
QList<RzIOPluginDescription> ret; QList<RzIOPluginDescription> ret;
RzListIter *it; RzListIter *it;
RzIOPlugin *p; RzIOPlugin *p;
CutterRzListForeach(core->io->plugins, it, RzIOPlugin, p) { CutterRzListForeach (core->io->plugins, it, RzIOPlugin, p) {
RzIOPluginDescription desc; RzIOPluginDescription desc;
desc.name = p->name ? p->name : ""; desc.name = p->name ? p->name : "";
desc.description = p->desc ? p->desc : ""; desc.description = p->desc ? p->desc : "";
desc.license = p->license ? p->license : ""; desc.license = p->license ? p->license : "";
desc.permissions = desc.permissions = QString("r") + (p->write ? "w" : "_") + (p->isdbg ? "d" : "_");
QString("r") +
(p->write ? "w" : "_") +
(p->isdbg ? "d" : "_");
if (p->uris) { if (p->uris) {
desc.uris = QString::fromUtf8(p->uris).split(","); desc.uris = QString::fromUtf8(p->uris).split(",");
} }
@ -2773,7 +2783,7 @@ QList<RzCorePluginDescription> CutterCore::getRCorePluginDescriptions()
QList<RzCorePluginDescription> ret; QList<RzCorePluginDescription> ret;
RzListIter *it; RzListIter *it;
RzCorePlugin *p; RzCorePlugin *p;
CutterRzListForeach(core->plugins, it, RzCorePlugin, p) { CutterRzListForeach (core->plugins, it, RzCorePlugin, p) {
RzCorePluginDescription desc; RzCorePluginDescription desc;
desc.name = p->name ? p->name : ""; desc.name = p->name ? p->name : "";
desc.description = p->desc ? p->desc : ""; desc.description = p->desc ? p->desc : "";
@ -2790,8 +2800,7 @@ QList<RzAsmPluginDescription> CutterCore::getRAsmPluginDescriptions()
QList<RzAsmPluginDescription> ret; QList<RzAsmPluginDescription> ret;
RzAsmPlugin *ap; RzAsmPlugin *ap;
CutterRzListForeach(core->rasm->plugins, it, RzAsmPlugin, ap) CutterRzListForeach (core->rasm->plugins, it, RzAsmPlugin, ap) {
{
RzAsmPluginDescription plugin; RzAsmPluginDescription plugin;
plugin.name = ap->name; plugin.name = ap->name;
@ -2817,8 +2826,7 @@ QList<FunctionDescription> CutterCore::getAllFunctions()
RzListIter *iter; RzListIter *iter;
RzAnalysisFunction *fcn; RzAnalysisFunction *fcn;
CutterRzListForeach(core->analysis->fcns, iter, RzAnalysisFunction, fcn) CutterRzListForeach (core->analysis->fcns, iter, RzAnalysisFunction, fcn) {
{
FunctionDescription function; FunctionDescription function;
function.offset = fcn->addr; function.offset = fcn->addr;
function.linearSize = rz_analysis_function_linear_size(fcn); function.linearSize = rz_analysis_function_linear_size(fcn);
@ -2898,8 +2906,7 @@ QList<SymbolDescription> CutterCore::getAllSymbols()
RzBinSymbol *bs; RzBinSymbol *bs;
if (core && core->bin && core->bin->cur && core->bin->cur->o) { 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); QString type = QString(bs->bind) + " " + QString(bs->type);
SymbolDescription symbol; SymbolDescription symbol;
symbol.vaddr = bs->vaddr; symbol.vaddr = bs->vaddr;
@ -2912,8 +2919,7 @@ QList<SymbolDescription> CutterCore::getAllSymbols()
/* list entrypoints as symbols too */ /* list entrypoints as symbols too */
int n = 0; int n = 0;
RzBinAddr *entry; RzBinAddr *entry;
CutterRzListForeach(core->bin->cur->o->entries, it, RzBinAddr, entry) CutterRzListForeach (core->bin->cur->o->entries, it, RzBinAddr, entry) {
{
SymbolDescription symbol; SymbolDescription symbol;
symbol.vaddr = entry->vaddr; symbol.vaddr = entry->vaddr;
symbol.name = QString("entry") + QString::number(n++); 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()); 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(); CORE_LOCK();
RzAnalysisMethod analysisMeth; RzAnalysisMethod analysisMeth;
@ -3443,7 +3450,7 @@ void CutterCore::setAnalysisMethod(const QString &className, const AnalysisMetho
} }
void CutterCore::renameAnalysisMethod(const QString &className, const QString &oldMethodName, void CutterCore::renameAnalysisMethod(const QString &className, const QString &oldMethodName,
const QString &newMethodName) const QString &newMethodName)
{ {
CORE_LOCK(); CORE_LOCK();
rz_analysis_class_method_rename(core->analysis, className.toUtf8().constData(), 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) void CutterCore::loadPDB(const QString &file)
{ {
cmdRaw("idp " + sanitizeStringForCommand(file)); CORE_LOCK();
rz_core_bin_pdb_load(core, file.toUtf8().constData());
} }
QList<DisassemblyLine> CutterCore::disassembleLines(RVA offset, int lines) QList<DisassemblyLine> CutterCore::disassembleLines(RVA offset, int lines)

View File

@ -55,7 +55,25 @@ void EditVariablesDialog::applyFields()
} }
VariableDescription desc = ui->dropdownLocalVars->currentData().value<VariableDescription>(); VariableDescription desc = ui->dropdownLocalVars->currentData().value<VariableDescription>();
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 // TODO Remove all those replace once rizin command parser is fixed
QString newName = ui->nameEdit->text() QString newName = ui->nameEdit->text()
@ -84,7 +102,8 @@ void EditVariablesDialog::updateFields()
ui->typeComboBox->setCurrentText(desc.type); ui->typeComboBox->setCurrentText(desc.type);
} }
static void addTypeDescriptionsToComboBox(QComboBox *comboBox, QList<TypeDescription> list) { static void addTypeDescriptionsToComboBox(QComboBox *comboBox, QList<TypeDescription> list)
{
for (const TypeDescription &thisType : list) { for (const TypeDescription &thisType : list) {
comboBox->addItem(thisType.type); comboBox->addItem(thisType.type);
} }
@ -97,5 +116,4 @@ void EditVariablesDialog::populateTypesComboBox()
addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllPrimitiveTypes()); addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllPrimitiveTypes());
addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllEnums()); addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllEnums());
addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllTypedefs()); addTypeDescriptionsToComboBox(ui->typeComboBox, Core()->getAllTypedefs());
} }

View File

@ -56,10 +56,14 @@ void LinkTypeDialog::done(int r)
QString type = ui->structureTypeComboBox->currentText(); QString type = ui->structureTypeComboBox->currentText();
if (type == tr("(No Type)")) { if (type == tr("(No Type)")) {
// Delete link // 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 { } else {
// Create link // 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); QDialog::done(r);
@ -84,16 +88,17 @@ QString LinkTypeDialog::findLinkedType(RVA address)
return QString(); return QString();
} }
QString ret = Core()->cmdRaw(QString("tls %1").arg(address)); RzCoreLocked core(Core());
if (ret.isEmpty()) { RzType *link = rz_analysis_type_link_at(core->analysis, address);
// return empty string since the current address is not linked to a type if (!link) {
return QString();
}
RzBaseType *base = rz_type_get_base_type(core->analysis->typedb, link);
if (!base) {
return QString(); return QString();
} }
// Extract the given type from returned data return QString(base->name);
// 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);
} }
void LinkTypeDialog::on_exprLineEdit_textChanged(const QString &text) void LinkTypeDialog::on_exprLineEdit_textChanged(const QString &text)

View File

@ -704,7 +704,8 @@ void HexWidget::w_writeString()
d.setInputMode(QInputDialog::InputMode::TextInput); d.setInputMode(QInputDialog::InputMode::TextInput);
QString str = d.getText(this, tr("Write string"), tr("String:"), QLineEdit::Normal, "", &ok); QString str = d.getText(this, tr("Write string"), tr("String:"), QLineEdit::Normal, "", &ok);
if (ok && !str.isEmpty()) { 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(); refresh();
} }
} }
@ -719,12 +720,13 @@ void HexWidget::w_increaseDecrease()
if (ret == QDialog::Rejected) { if (ret == QDialog::Rejected) {
return; return;
} }
QString mode = d.getMode() == IncrementDecrementDialog::Increase ? "+" : "-"; int64_t value = (int64_t)d.getValue();
Core()->cmdRawAt(QString("w%1%2 %3") uint8_t sz = d.getNBytes();
.arg(QString::number(d.getNBytes())) if (!d.getMode() == IncrementDecrementDialog::Increase) {
.arg(mode) value *= -1;
.arg(QString::number(d.getValue())), }
getLocationAddress()); RzCoreLocked core(Core());
rz_core_write_value_inc_at(core, getLocationAddress(), value, sz);
refresh(); refresh();
} }
@ -741,10 +743,14 @@ void HexWidget::w_writeZeros()
size = static_cast<int>(selection.size()); size = static_cast<int>(selection.size());
} }
QString str = QString::number( int len =
d.getInt(this, tr("Write zeros"), tr("Number of zeros:"), size, 1, 0x7FFFFFFF, 1, &ok)); d.getInt(this, tr("Write zeros"), tr("Number of zeros:"), size, 1, 0x7FFFFFFF, 1, &ok);
if (ok && !str.isEmpty()) { if (ok) {
Core()->cmdRawAt(QString("w0 %1").arg(str), getLocationAddress()); RzCoreLocked core(Core());
uint8_t *buf = (uint8_t *)calloc(len, sizeof(uint8_t));
rz_core_write_at(core, getLocationAddress(), buf, len);
free(buf);
refresh(); refresh();
} }
} }
@ -759,10 +765,9 @@ void HexWidget::w_write64()
if (ret == QDialog::Rejected) { if (ret == QDialog::Rejected) {
return; return;
} }
QString mode = d.getMode() == Base64EnDecodedWriteDialog::Encode ? "e" : "d";
QByteArray str = d.getData(); QByteArray str = d.getData();
if (mode == "d" if (d.getMode() == Base64EnDecodedWriteDialog::Decode
&& (QString(str).contains(QRegularExpression("[^a-zA-Z0-9+/=]")) || str.length() % 4 != 0 && (QString(str).contains(QRegularExpression("[^a-zA-Z0-9+/=]")) || str.length() % 4 != 0
|| str.isEmpty())) { || str.isEmpty())) {
QMessageBox::critical( QMessageBox::critical(
@ -772,9 +777,12 @@ void HexWidget::w_write64()
return; return;
} }
Core()->cmdRawAt(QString("w6%1 %2").arg(mode).arg( RzCoreLocked core(Core());
(mode == "e" ? str.toHex() : str).toStdString().c_str()), if (d.getMode() == Base64EnDecodedWriteDialog::Encode) {
getLocationAddress()); rz_core_write_base64_at(core, getLocationAddress(), str.toHex().constData());
} else {
rz_core_write_base64d_at(core, getLocationAddress(), str.constData());
}
refresh(); refresh();
} }