From abee33407097da90e79d89163855cb4e73daaed7 Mon Sep 17 00:00:00 2001 From: Pulak Malhotra <56169176+PulakIIIT@users.noreply.github.com> Date: Fri, 30 Jul 2021 16:45:16 +0530 Subject: [PATCH] Added Dialog box for windows heaps and fixed some warnings in sections widget --- src/CMakeLists.txt | 7 ++- src/core/Cutter.cpp | 27 +++++++++ src/core/Cutter.h | 1 + src/core/CutterDescriptions.h | 14 ++++- src/dialogs/WindowsHeapDialog.cpp | 99 +++++++++++++++++++++++++++++++ src/dialogs/WindowsHeapDialog.h | 43 ++++++++++++++ src/dialogs/WindowsHeapDialog.ui | 24 ++++++++ src/widgets/SectionsWidget.h | 11 ++-- src/widgets/WindowsHeapWidget.cpp | 8 +++ src/widgets/WindowsHeapWidget.h | 1 + src/widgets/WindowsHeapWidget.ui | 47 ++++++++------- 11 files changed, 252 insertions(+), 30 deletions(-) create mode 100644 src/dialogs/WindowsHeapDialog.cpp create mode 100644 src/dialogs/WindowsHeapDialog.h create mode 100644 src/dialogs/WindowsHeapDialog.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7d2176f4..c7d1c102 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,6 +148,7 @@ set(SOURCES widgets/HeapBinsGraphView.cpp dialogs/ArenaInfoDialog.cpp widgets/WindowsHeapWidget.cpp + dialogs/WindowsHeapDialog.cpp ) set(HEADER_FILES core/Cutter.h @@ -307,7 +308,8 @@ set(HEADER_FILES dialogs/GlibcHeapBinsDialog.h widgets/HeapBinsGraphView.h dialogs/ArenaInfoDialog.h - widgets/WindowsHeapWidget.h + widgets/WindowsHeapWidget.h + dialogs/WindowsHeapDialog.h ) set(UI_FILES dialogs/AboutDialog.ui @@ -379,7 +381,8 @@ set(UI_FILES widgets/GlibcHeapWidget.ui dialogs/GlibcHeapBinsDialog.ui dialogs/ArenaInfoDialog.ui - widgets/WindowsHeapWidget.ui + widgets/WindowsHeapWidget.ui + dialogs/WindowsHeapDialog.ui ) set(QRC_FILES resources.qrc diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 1f8c184b..891567be 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -1621,6 +1621,33 @@ QVector CutterCore::getHeapBlocks() return blocks_vector; } +QVector CutterCore::getWindowsHeaps() +{ + CORE_LOCK(); + QVector 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(); diff --git a/src/core/Cutter.h b/src/core/Cutter.h index a2536e82..f9a5087e 100644 --- a/src/core/Cutter.h +++ b/src/core/Cutter.h @@ -433,6 +433,7 @@ public: */ bool writeHeapChunk(RzHeapChunkSimple *chunkSimple); QVector getHeapBlocks(); + QVector getWindowsHeaps(); int getArchBits(); void startDebug(); void startEmulation(); diff --git a/src/core/CutterDescriptions.h b/src/core/CutterDescriptions.h index 39c5daf1..451593b3 100644 --- a/src/core/CutterDescriptions.h +++ b/src/core/CutterDescriptions.h @@ -387,12 +387,20 @@ struct HeapBlock { RVA headerAddress; RVA userAddress; - RVA size; - RVA unusedBytes; - RVA granularity; + 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) diff --git a/src/dialogs/WindowsHeapDialog.cpp b/src/dialogs/WindowsHeapDialog.cpp new file mode 100644 index 00000000..6b7548f9 --- /dev/null +++ b/src/dialogs/WindowsHeapDialog.cpp @@ -0,0 +1,99 @@ +#include "WindowsHeapDialog.h" +#include "ui_WindowsHeapDialog.h" +#include +#include + +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() +{ + beginResetModel(); + values.clear(); + values = Core()->getWindowsHeaps(); + endResetModel(); +} \ No newline at end of file diff --git a/src/dialogs/WindowsHeapDialog.h b/src/dialogs/WindowsHeapDialog.h new file mode 100644 index 00000000..b9da0038 --- /dev/null +++ b/src/dialogs/WindowsHeapDialog.h @@ -0,0 +1,43 @@ +#ifndef WINDOWSHEAPDIALOG_H +#define WINDOWSHEAPDIALOG_H + +#include +#include +#include + +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 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 diff --git a/src/dialogs/WindowsHeapDialog.ui b/src/dialogs/WindowsHeapDialog.ui new file mode 100644 index 00000000..a262a396 --- /dev/null +++ b/src/dialogs/WindowsHeapDialog.ui @@ -0,0 +1,24 @@ + + + WindowsHeapDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + + + + diff --git a/src/widgets/SectionsWidget.h b/src/widgets/SectionsWidget.h index 9a7e9638..acf55eea 100644 --- a/src/widgets/SectionsWidget.h +++ b/src/widgets/SectionsWidget.h @@ -51,11 +51,12 @@ public: SectionsModel(QList *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; @@ -78,7 +79,7 @@ class SectionsWidget : public ListDockWidget public: explicit SectionsWidget(MainWindow *main); - ~SectionsWidget(); + ~SectionsWidget() override; private slots: void refreshSections(); diff --git a/src/widgets/WindowsHeapWidget.cpp b/src/widgets/WindowsHeapWidget.cpp index f106565f..7651cd73 100644 --- a/src/widgets/WindowsHeapWidget.cpp +++ b/src/widgets/WindowsHeapWidget.cpp @@ -1,3 +1,4 @@ +#include #include "WindowsHeapWidget.h" #include "ui_WindowsHeapWidget.h" @@ -15,6 +16,7 @@ WindowsHeapWidget::WindowsHeapWidget(MainWindow *main, QWidget *parent) connect(Core(), &CutterCore::refreshAll, this, &WindowsHeapWidget::updateContents); connect(Core(), &CutterCore::debugTaskStateChanged, this, &WindowsHeapWidget::updateContents); + connect(ui->heapButton, &QPushButton::clicked, this, &WindowsHeapWidget::viewHeapInfo); refreshDeferrer = dynamic_cast(parent)->createRefreshDeferrer( [this]() { updateContents(); }); @@ -27,6 +29,12 @@ WindowsHeapWidget::~WindowsHeapWidget() delete ui; } +void WindowsHeapWidget::viewHeapInfo() +{ + WindowsHeapDialog windowsHeapDialog(this); + windowsHeapDialog.exec(); +} + void WindowsHeapWidget::updateContents() { if (!refreshDeferrer->attemptRefresh(nullptr) || Core()->isDebugTaskInProgress()) { diff --git a/src/widgets/WindowsHeapWidget.h b/src/widgets/WindowsHeapWidget.h index 366037b0..a3dba66f 100644 --- a/src/widgets/WindowsHeapWidget.h +++ b/src/widgets/WindowsHeapWidget.h @@ -43,6 +43,7 @@ public: ~WindowsHeapWidget(); private slots: void updateContents(); + void viewHeapInfo(); private: Ui::WindowsHeapWidget *ui; diff --git a/src/widgets/WindowsHeapWidget.ui b/src/widgets/WindowsHeapWidget.ui index 13473eb4..317b19c7 100644 --- a/src/widgets/WindowsHeapWidget.ui +++ b/src/widgets/WindowsHeapWidget.ui @@ -1,24 +1,31 @@ - WindowsHeapWidget - - - - 0 - 0 - 400 - 300 - - - - Form - - - - - - + WindowsHeapWidget + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + + + + + Heaps + - - + + + + +