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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rizin
Submodule rizin updated 405 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
74 changes: 60 additions & 14 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 @@ -3616,22 +3672,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);
int result = rz_type_parse_string(core->analysis->typedb, str, &error_msg);
// TODO fix adding and parsing types
QString error;

if (!parsed) {
if (error_msg) {
error = error_msg;
rz_mem_free(error_msg);
}
return error;
}

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

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

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();
}
43 changes: 43 additions & 0 deletions src/dialogs/WindowsHeapDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef WINDOWSHEAPDIALOG_H
#define WINDOWSHEAPDIALOG_H

#include <QDialog>
#include <ui_WindowsHeapDialog.h>
#include <CutterDescriptions.h>

namespace Ui {
class WindowsHeapDialog;
}

class HeapInfoModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit HeapInfoModel(QObject *parent = nullptr);
enum Column { BaseColumn = 0, AllocatedColumn, CommittedColumn, BlockCountColumn, ColumnCount };
void reload();
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;

private:
QVector<WindowsHeapInfo> values;
};

class WindowsHeapDialog : public QDialog
{
Q_OBJECT

public:
explicit WindowsHeapDialog(QWidget *parent);
~WindowsHeapDialog() override;
private slots:
void updateContents();

private:
Ui::WindowsHeapDialog *ui;
QTableView *viewHeap;
HeapInfoModel *modelHeap = new HeapInfoModel(this);
};
#endif // WINDOWSHEAPDIALOG_H
24 changes: 24 additions & 0 deletions src/dialogs/WindowsHeapDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>WindowsHeapDialog</class>
<widget class="QDialog" name="WindowsHeapDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
5 changes: 5 additions & 0 deletions src/widgets/HeapDockWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "HeapDockWidget.h"
#include "ui_HeapDockWidget.h"
#include "widgets/GlibcHeapWidget.h"
#include "WindowsHeapWidget.h"

HeapDockWidget::HeapDockWidget(MainWindow *main)
: CutterDockWidget(main), ui(new Ui::HeapDockWidget), main(main)
{
ui->setupUi(this);

ui->allocatorSelector->addItem("Glibc Heap");
ui->allocatorSelector->addItem("Windows Heap");
MalhotraPulak marked this conversation as resolved.
Show resolved Hide resolved
ui->verticalLayout->setMargin(0);

connect<void (QComboBox::*)(int)>(ui->allocatorSelector, &QComboBox::currentIndexChanged, this,
Expand Down Expand Up @@ -36,6 +38,9 @@ void HeapDockWidget::onAllocatorSelected(int index)
// change widget depending upon selected allocator
if (index == Glibc) {
currentHeapWidget = new GlibcHeapWidget(main, this);
} else if (index == Windows) {
currentHeapWidget = new WindowsHeapWidget(main, this);
}

ui->verticalLayout->addWidget(currentHeapWidget);
}
2 changes: 1 addition & 1 deletion src/widgets/HeapDockWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private slots:
void onAllocatorSelected(int index);

private:
enum Allocator { Glibc = 0, AllocatorCount };
enum Allocator { Glibc = 0, Windows, AllocatorCount };
Ui::HeapDockWidget *ui;
MainWindow *main;
QWidget* currentHeapWidget = nullptr;
Expand Down
11 changes: 6 additions & 5 deletions src/widgets/SectionsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ class SectionsModel : public AddressableItemModel<QAbstractListModel>

SectionsModel(QList<SectionDescription> *sections, QObject *parent = nullptr);

int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;

QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;

RVA address(const QModelIndex &index) const override;
QString name(const QModelIndex &index) const override;
Expand All @@ -78,7 +79,7 @@ class SectionsWidget : public ListDockWidget

public:
explicit SectionsWidget(MainWindow *main);
~SectionsWidget();
~SectionsWidget() override;

private slots:
void refreshSections();
Expand Down
Loading