1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*
Copyright (C) 2006 - 2019 Evan Teran
evan.teran@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DialogResults.h"
#include "IAnalyzer.h"
#include "ISymbolManager.h"
#include "MemoryRegions.h"
#include "ResultsModel.h"
#include "edb.h"
#ifdef ENABLE_GRAPH
#include "GraphEdge.h"
#include "GraphNode.h"
#include "GraphWidget.h"
#endif
#include <QDialog>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QSortFilterProxyModel>
namespace FunctionFinderPlugin {
/**
* @brief DialogResults::DialogResults
* @param parent
* @param f
*/
DialogResults::DialogResults(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f) {
ui.setupUi(this);
ui.tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
resultsModel_ = new ResultsModel(this);
filterModel_ = new QSortFilterProxyModel(this);
filterModel_->setFilterKeyColumn(5);
filterModel_->setSourceModel(resultsModel_);
connect(ui.textFilter, &QLineEdit::textChanged, filterModel_, &QSortFilterProxyModel::setFilterFixedString);
ui.tableView->setModel(filterModel_);
buttonGraph_ = new QPushButton(QIcon::fromTheme("distribute-graph"), tr("Graph Selected Function"));
#if defined(ENABLE_GRAPH)
connect(buttonGraph_, &QPushButton::clicked, this, [this]() {
// this code is not very pretty...
// but it works!
qDebug("[FunctionFinder] Constructing Graph...");
const QItemSelectionModel *const selModel = ui.tableView->selectionModel();
const QModelIndexList sel = selModel->selectedRows();
if (sel.size() == 1) {
const QModelIndex index = filterModel_->mapToSource(sel[0]);
if (auto item = static_cast<ResultsModel::Result *>(index.internalPointer())) {
const edb::address_t addr = item->startAddress;
if (IAnalyzer *const analyzer = edb::v1::analyzer()) {
const IAnalyzer::FunctionMap &functions = analyzer->functions();
auto it = functions.find(addr);
if (it != functions.end()) {
Function f = *it;
auto graph = new GraphWidget(nullptr);
graph->setAttribute(Qt::WA_DeleteOnClose);
QMap<edb::address_t, GraphNode *> nodes;
// first create all of the nodes
for (const auto &pair : f) {
const BasicBlock &bb = pair.second;
auto node = new GraphNode(graph, bb.toString(), Qt::lightGray);
nodes.insert(bb.firstAddress(), node);
}
// then connect them!
for (const auto &pair : f) {
const BasicBlock &bb = pair.second;
if (!bb.empty()) {
auto term = bb.back();
auto &inst = *term;
if (is_unconditional_jump(inst)) {
Q_ASSERT(inst.operandCount() >= 1);
const auto op = inst[0];
// TODO: we need some heuristic for detecting when this is
// a call/ret -> jmp optimization
if (is_immediate(op)) {
const edb::address_t ea = op->imm;
auto from = nodes.find(bb.firstAddress());
auto to = nodes.find(ea);
if (to != nodes.end() && from != nodes.end()) {
new GraphEdge(from.value(), to.value(), Qt::black);
}
}
} else if (is_conditional_jump(inst)) {
Q_ASSERT(inst.operandCount() == 1);
const auto op = inst[0];
if (is_immediate(op)) {
auto from = nodes.find(bb.firstAddress());
auto to_taken = nodes.find(op->imm);
if (to_taken != nodes.end() && from != nodes.end()) {
new GraphEdge(from.value(), to_taken.value(), Qt::green);
}
auto to_skipped = nodes.find(inst.rva() + inst.byteSize());
if (to_taken != nodes.end() && from != nodes.end()) {
new GraphEdge(from.value(), to_skipped.value(), Qt::red);
}
}
} else if (is_terminator(inst)) {
}
}
}
graph->layout();
graph->show();
}
}
}
}
});
#endif
ui.buttonBox->addButton(buttonGraph_, QDialogButtonBox::ActionRole);
#ifdef ENABLE_GRAPH
buttonGraph_->setEnabled(true);
#else
buttonGraph_->setEnabled(false);
#endif
}
/**
* @brief DialogResults::on_tableView_doubleClicked
* @param index
*/
void DialogResults::on_tableView_doubleClicked(const QModelIndex &index) {
if (index.isValid()) {
const QModelIndex realIndex = filterModel_->mapToSource(index);
if (auto item = static_cast<ResultsModel::Result *>(realIndex.internalPointer())) {
edb::v1::jump_to_address(item->startAddress);
}
}
}
/**
* @brief DialogResults::addResult
* @param function
*/
void DialogResults::addResult(const Function &function) {
ResultsModel::Result result;
// entry point
result.startAddress = function.entryAddress();
// upper bound of the function
result.endAddress = function.endAddress();
result.size = function.endAddress() - function.entryAddress() + 1;
// reference count
result.score = function.referenceCount();
// type
result.type = function.type();
QString symbol_name = edb::v1::symbol_manager().findAddressName(function.entryAddress());
if (!symbol_name.isEmpty()) {
result.symbol = symbol_name;
}
resultsModel_->addResult(result);
}
/**
* @brief DialogResults::resultCount
* @return
*/
int DialogResults::resultCount() const {
return resultsModel_->rowCount();
}
}
|