Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows heap widget #2723

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rizin
Submodule rizin updated 467 files
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ set(SOURCES
dialogs/GlibcHeapBinsDialog.cpp
widgets/HeapBinsGraphView.cpp
dialogs/ArenaInfoDialog.cpp
widgets/WindowsHeapWidget.cpp
dialogs/WindowsHeapDialog.cpp
)
set(HEADER_FILES
core/Cutter.h
Expand Down Expand Up @@ -306,6 +308,8 @@ set(HEADER_FILES
dialogs/GlibcHeapBinsDialog.h
widgets/HeapBinsGraphView.h
dialogs/ArenaInfoDialog.h
widgets/WindowsHeapWidget.h
dialogs/WindowsHeapDialog.h
)
set(UI_FILES
dialogs/AboutDialog.ui
Expand Down Expand Up @@ -377,6 +381,8 @@ set(UI_FILES
widgets/GlibcHeapWidget.ui
dialogs/GlibcHeapBinsDialog.ui
dialogs/ArenaInfoDialog.ui
widgets/WindowsHeapWidget.ui
dialogs/WindowsHeapDialog.ui
)
set(QRC_FILES
resources.qrc
Expand Down
146 changes: 109 additions & 37 deletions src/core/Cutter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,62 @@ QVector<Chunk> CutterCore::getHeapChunks(RVA arena_addr)
return chunks_vector;
}

QVector<HeapBlock> CutterCore::getHeapBlocks()
{
CORE_LOCK();
QVector<HeapBlock> blocks_vector;
RzList *blocks = rz_heap_windows_blocks_list(core);
if (!blocks || !rz_list_length(blocks)) {
rz_list_free(blocks);
return blocks_vector;
}

RzListIter *iter;
RzWindowsHeapBlock *data;
CutterRListForeach(blocks, iter, RzWindowsHeapBlock, data)
{
HeapBlock block;
block.headerAddress = data->headerAddress;
block.userAddress = data->userAddress;
block.granularity = data->granularity;
block.unusedBytes = data->unusedBytes;
block.size = data->size;
block.type = QString(data->type);

blocks_vector.append(block);
}

rz_list_free(blocks);
return blocks_vector;
}

QVector<WindowsHeapInfo> CutterCore::getWindowsHeaps()
{
CORE_LOCK();
QVector<WindowsHeapInfo> heaps_vector;
RzList *heaps = rz_heap_windows_heap_list(core);
if (!heaps || !rz_list_length(heaps)) {
rz_list_free(heaps);
return heaps_vector;
}

RzListIter *iter;
RzWindowsHeapInfo *data;
CutterRListForeach(heaps, iter, RzWindowsHeapInfo, data)
{
WindowsHeapInfo block;
block.base = data->base;
block.blockCount = data->blockCount;
block.allocated = data->allocated;
block.committed = data->committed;

heaps_vector.append(block);
}

rz_list_free(heaps);
return heaps_vector;
}

int CutterCore::getArchBits()
{
CORE_LOCK();
Expand Down Expand Up @@ -3506,15 +3562,30 @@ QList<VTableDescription> CutterCore::getAllVTables()

QList<TypeDescription> CutterCore::getAllTypes()
{
QList<TypeDescription> types;
CORE_LOCK();
QList<TypeDescription> types_desc;
RzList *types = rz_type_db_get_base_types(core->analysis->typedb);
RzListIter *it;
RzBaseType *btype;

types.append(getAllPrimitiveTypes());
types.append(getAllUnions());
types.append(getAllStructs());
types.append(getAllEnums());
types.append(getAllTypedefs());
CutterRListForeach(types, it, RzBaseType, btype)
{
TypeDescription typeDescription;
typeDescription.type = btype->name;
if (btype->kind == RZ_BASE_TYPE_KIND_STRUCT) {
typeDescription.category = "Struct";
} else if (btype->kind == RZ_BASE_TYPE_KIND_ENUM) {
typeDescription.category = "Enum";
} else if (btype->kind == RZ_BASE_TYPE_KIND_ATOMIC) {
typeDescription.category = "Primitive";
} else if (btype->kind == RZ_BASE_TYPE_KIND_TYPEDEF) {
typeDescription.category = "Typedef";
}
typeDescription.size = btype->size;
types_desc << typeDescription;
}

return types;
return types_desc;
}

QList<TypeDescription> CutterCore::getAllPrimitiveTypes()
Expand All @@ -3529,9 +3600,8 @@ QList<TypeDescription> CutterCore::getAllPrimitiveTypes()
TypeDescription exp;

exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.format = typeObject[RJsonKey::format].toString();
exp.category = tr("Primitive");
exp.size = (int)typeObject[RJsonKey::size].toVariant().toULongLong();
exp.category = "Primitive";
primitiveTypes << exp;
}

Expand All @@ -3549,8 +3619,7 @@ QList<TypeDescription> CutterCore::getAllUnions()

TypeDescription exp;

exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.type = typeObject[RJsonKey::name].toString();
exp.category = "Union";
unions << exp;
}
Expand All @@ -3569,8 +3638,8 @@ QList<TypeDescription> CutterCore::getAllStructs()

TypeDescription exp;

exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.type = typeObject[RJsonKey::name].toString();
exp.size = 0;
exp.category = "Struct";
structs << exp;
}
Expand All @@ -3584,9 +3653,12 @@ QList<TypeDescription> CutterCore::getAllEnums()
QList<TypeDescription> enums;

QJsonObject typesObject = cmdj("tej").object();
for (QString key : typesObject.keys()) {
for (const QJsonValue value : typesObject.keys()) {
QJsonObject typeObject = value.toObject();

TypeDescription exp;
exp.type = key;

exp.type = typeObject[RJsonKey::name].toString();
exp.size = 0;
exp.category = "Enum";
enums << exp;
Expand All @@ -3601,9 +3673,12 @@ QList<TypeDescription> CutterCore::getAllTypedefs()
QList<TypeDescription> typeDefs;

QJsonObject typesObject = cmdj("ttj").object();
for (QString key : typesObject.keys()) {
for (const QJsonValue value : typesObject.keys()) {
QJsonObject typeObject = value.toObject();

TypeDescription exp;
exp.type = key;

exp.type = typeObject[RJsonKey::name].toString();
exp.size = 0;
exp.category = "Typedef";
typeDefs << exp;
Expand All @@ -3616,22 +3691,12 @@ QString CutterCore::addTypes(const char *str)
{
CORE_LOCK();
char *error_msg = nullptr;
char *parsed = rz_type_parse_c_string(core->analysis->typedb, str, &error_msg);
QString error;

if (!parsed) {
if (error_msg) {
error = error_msg;
rz_mem_free(error_msg);
}
return error;
}
int result = rz_type_parse_string(core->analysis->typedb, str, &error_msg);

rz_type_db_save_parsed_type(core->analysis->typedb, parsed);
rz_mem_free(parsed);
QString error;

if (error_msg) {
error = error_msg;
if (result && error_msg) {
error = QString(error_msg);
rz_mem_free(error_msg);
}

Expand All @@ -3645,16 +3710,23 @@ QString CutterCore::getTypeAsC(QString name, QString category)
if (name.isEmpty() || category.isEmpty()) {
return output;
}
QString typeName = sanitizeStringForCommand(name);

const char *name_char = name.toStdString().c_str();
RzBaseType *rzBaseType;

if (category == "Struct") {
output = cmdRaw(QString("tsc %1").arg(typeName));
rzBaseType = rz_type_db_get_struct(core->analysis->typedb, name_char);
} else if (category == "Union") {
output = cmdRaw(QString("tuc %1").arg(typeName));
rzBaseType = rz_type_db_get_union(core->analysis->typedb, name_char);
} else if (category == "Enum") {
output = cmdRaw(QString("tec %1").arg(typeName));
rzBaseType = rz_type_db_get_enum(core->analysis->typedb, name_char);
} else if (category == "Typedef") {
output = cmdRaw(QString("ttc %1").arg(typeName));
rzBaseType = rz_type_db_get_typedef(core->analysis->typedb, name_char);
}

char *output_char = rz_type_db_base_type_as_string(core->analysis->typedb, rzBaseType);

output = QString(output_char);
return output;
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/Cutter.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ class CUTTER_EXPORT CutterCore : public QObject
* @return true if the write succeeded else false
*/
bool writeHeapChunk(RzHeapChunkSimple *chunkSimple);
QVector<HeapBlock> getHeapBlocks();
QVector<WindowsHeapInfo> getWindowsHeaps();
int getArchBits();
void startDebug();
void startEmulation();
Expand Down
18 changes: 18 additions & 0 deletions src/core/CutterDescriptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,24 @@ struct Arena
ut64 max_system_mem;
};

struct HeapBlock
{
RVA headerAddress;
RVA userAddress;
ut64 size;
ut64 unusedBytes;
ut64 granularity;
QString type;
};

struct WindowsHeapInfo
{
RVA base;
ut64 committed;
ut64 allocated;
ut64 blockCount;
};

Q_DECLARE_METATYPE(FunctionDescription)
Q_DECLARE_METATYPE(ImportDescription)
Q_DECLARE_METATYPE(ExportDescription)
Expand Down
99 changes: 99 additions & 0 deletions src/dialogs/WindowsHeapDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "WindowsHeapDialog.h"
#include "ui_WindowsHeapDialog.h"
#include <Cutter.h>
#include <Configuration.h>

WindowsHeapDialog::WindowsHeapDialog(QWidget *parent)
: QDialog(parent), ui(new Ui::WindowsHeapDialog)
{
ui->setupUi(this);

viewHeap = ui->tableView;
viewHeap->setFont(Config()->getFont());
viewHeap->setModel(modelHeap);
viewHeap->verticalHeader()->hide();
// change the scroll mode to ScrollPerPixel
viewHeap->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
viewHeap->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

updateContents();
}

WindowsHeapDialog::~WindowsHeapDialog()
{
delete ui;
}

void WindowsHeapDialog::updateContents()
{
modelHeap->reload();
viewHeap->resizeColumnsToContents();
}

HeapInfoModel::HeapInfoModel(QObject *parent) : QAbstractTableModel(parent) {}

QVariant HeapInfoModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= values.count())
return QVariant();

const auto &item = values.at(index.row());

switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case BaseColumn:
return RAddressString(item.base);
case AllocatedColumn:
return item.allocated;
case CommittedColumn:
return item.committed;
case BlockCountColumn:
return item.blockCount;
default:
return QVariant();
}
default:
return QVariant();
}
}

QVariant HeapInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation);
switch (role) {
case Qt::DisplayRole:
switch (section) {
case BaseColumn:
return tr("Base Address");
case AllocatedColumn:
return tr("Allocated");
case CommittedColumn:
return tr("Committed");
case BlockCountColumn:
return tr("Block Count");
default:
return QVariant();
}
default:
return QVariant();
}
}

int HeapInfoModel::columnCount(const QModelIndex &) const
{
return ColumnCount;
}

int HeapInfoModel::rowCount(const QModelIndex &) const
{
return this->values.size();
}

void HeapInfoModel::reload()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going through this freezes Cutter, the call to getWindowsHeaps should be done in an AsyncTask similar to Strings/FunctionsWidget.
Also worth trying to optimize rizin's side, listing an empty heap should take 20s+.

{
beginResetModel();
values.clear();
values = Core()->getWindowsHeaps();
endResetModel();
}