summaryrefslogtreecommitdiff
path: root/plugins/warp/ui/matches.cpp
blob: 2af1738341e4435849664f2847fd4214a42c6d83 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "matches.h"
#include "theme.h"
#include "warp.h"
#include "shared/misc.h"

#include <QClipboard>
#include <QFormLayout>
#include <thread>
#include <QGridLayout>
#include <QHeaderView>
#include <QtConcurrent/QtConcurrent>

WarpCurrentFunctionWidget::WarpCurrentFunctionWidget(QWidget* parent) : QWidget(parent)
{
	// We must explicitly support no current function.
	m_current = nullptr;

	m_logger = new BinaryNinja::Logger("WARP UI");

	// Create the QT stuff
	QGridLayout* layout = new QGridLayout(this);
	layout->setContentsMargins(2, 2, 2, 2);
	layout->setSpacing(2);
	auto newPalette = palette();
	newPalette.setColor(QPalette::Window, getThemeColor(SidebarWidgetBackgroundColor));
	setAutoFillBackground(true);
	setPalette(newPalette);

	// TODO: Split horizontally if the widget is displayed in a sidebar that is vertically challenged.
	m_splitter = new QSplitter(Qt::Vertical);
	m_splitter->setContentsMargins(0, 0, 0, 0);

	// Wrap the table and the spinner so that we can overlay the spinner on the table.
	QWidget* tableWrapper = new QWidget(m_splitter);
	QGridLayout* wrapperLayout = new QGridLayout(tableWrapper);
	wrapperLayout->setContentsMargins(0, 0, 0, 0);

	// Add a widget to display the matches.
	m_tableWidget = new WarpFunctionTableWidget(tableWrapper);
	m_tableWidget->setContentsMargins(0, 0, 0, 0);

	// Spinner for when we are fetching functions over the network.
	m_spinner = new QProgressBar(tableWrapper);
	m_spinner->setRange(0, 0);
	m_spinner->setTextVisible(false);
	m_spinner->setFixedHeight(6);
	m_spinner->hide();

	// The table has no alignment, so it expands to fill the entire cell.
	wrapperLayout->addWidget(m_tableWidget, 0, 0);
	wrapperLayout->addWidget(m_spinner, 0, 0, Qt::AlignBottom);

	m_splitter->addWidget(tableWrapper);

	// Add a widget to display the info about the selected function match.
	m_infoWidget = new WarpFunctionInfoWidget(this);
	m_infoWidget->setContentsMargins(0, 0, 0, 0);
	m_splitter->addWidget(m_infoWidget);

	layout->addWidget(m_splitter, 1, 0, 1, 5);
	setLayout(layout);

	m_tableWidget->RegisterContextMenuAction("Apply", [this](WarpFunctionItem* item, std::optional<uint64_t>) {
		if (item == nullptr)
			return;
		Warp::Ref<Warp::Function> selectedFunction = item->GetFunction();
		if (!selectedFunction)
			return;
		selectedFunction->Apply(*m_current);
		// Update analysis so that the selected function shows.
		m_current->GetView()->UpdateAnalysis();
		// So it shows visually as selected.
		m_tableWidget->GetModel()->SetMatchedFunction(selectedFunction);
	});
	// If the selected function is the current match, let the user remove the match.
	m_tableWidget->RegisterContextMenuAction(
		"Remove Match",
		[this](WarpFunctionItem*, std::optional<uint64_t>) {
			WarpRemoveMatchDialog dlg(this, m_current);
			if (dlg.execute())
				m_tableWidget->GetModel()->SetMatchedFunction(nullptr);
		},
		[this](WarpFunctionItem* item, std::optional<uint64_t>) {
			if (item == nullptr)
				return false;
			Warp::Ref<Warp::Function> selectedFunction = item->GetFunction();
			if (!selectedFunction)
				return false;
			Warp::Ref<Warp::Function> matchedFunction = m_tableWidget->GetModel()->GetMatchedFunction();
			if (!matchedFunction)
				return false;
			return BNWARPFunctionsEqual(selectedFunction->m_object, matchedFunction->m_object);
		});
	m_tableWidget->RegisterContextMenuAction(
		"Search for Source", [this](WarpFunctionItem* item, std::optional<uint64_t>) {
			// Apply the source as the filter.
			if (const auto source = item->GetSource(); source)
				m_tableWidget->setFilter(source->ToString(), NoFilterOption);
		});

	connect(m_tableWidget->GetTableView(), &QTableView::clicked, this, [this](const QModelIndex& index) {
		if (m_current == nullptr)
			return;
		if (!index.isValid())
			return;
		const QModelIndex sourceIndex = m_tableWidget->GetProxyModel()->mapToSource(index);
		if (!sourceIndex.isValid())
			return;
		auto selectedItem = m_tableWidget->GetModel()->GetItem(sourceIndex);
		// Access the first column in the row
		if (!selectedItem)
			return;
		m_infoWidget->SetFunction(selectedItem->GetFunction());
		m_infoWidget->UpdateInfo();
	});

	connect(m_tableWidget->GetTableView(), &QTableView::doubleClicked, this, [=, this](const QModelIndex& index) {
		if (m_current == nullptr)
			return;
		// Get the selected row for the given index.
		if (!index.isValid())
			return;
		const QModelIndex sourceIndex = m_tableWidget->GetProxyModel()->mapToSource(index);
		if (!sourceIndex.isValid())
			return;
		auto selectedItem = m_tableWidget->GetModel()->GetItem(sourceIndex);
		// Access the first column in the row
		if (!selectedItem)
			return;
		Warp::Ref<Warp::Function> selectedFunction = selectedItem->GetFunction();

		// Actually apply the newly selected function.
		selectedFunction->Apply(*m_current);

		// Update analysis so that the selected function shows.
		m_current->GetView()->UpdateAnalysis();

		// So it shows visually as selected.
		m_tableWidget->GetModel()->SetMatchedFunction(selectedFunction);
	});
}

void WarpCurrentFunctionWidget::SetFetcher(std::shared_ptr<WarpFetcher> fetcher)
{
	m_fetcher = fetcher;
}

void WarpCurrentFunctionWidget::SetCurrentFunction(FunctionRef current)
{
	if (m_current == current)
		return;
	m_current = current;
	m_infoWidget->SetAnalysisFunction(m_current);

	// If we have a fetcher, we should also let it know to try and fetch the possible functions from the containers.
	if (current && m_fetcher)
	{
		m_fetcher->AddPendingFunction(current);
		if (!m_fetcher->m_requestInProgress.exchange(true))
		{
			// Don't block the UI thread when fetching, also unlike other cases where we reach for QtConcurrent::run we
			// do not use a watcher to tie the future to the widget's lifetime, this is because network requests can
			// take a long time, and we want to avoid blocking the UI thread in the widget destructor. Instead of
			// ensuring the widget is alive, we just use a weak pointer that can tell us when it (self) has been
			// destructed.
			auto future = QtConcurrent::run([self = QPointer(this), current, fetcher = m_fetcher]() {
				if (!self)
					return;
				QMetaObject::invokeMethod(
					self,
					[self] {
						if (self)
							self->m_spinner->show();
					},
					Qt::QueuedConnection);
				BinaryNinja::Ref bgTask = new BinaryNinja::BackgroundTask("Fetching WARP Functions...", true);
				const auto allowedTags = GetAllowedTagsFromView(current->GetView());
				fetcher->FetchPendingFunctions(allowedTags);
				bgTask->Finish();
				QMetaObject::invokeMethod(
					self,
					[self] {
						if (self)
							self->m_spinner->hide();
					},
					Qt::QueuedConnection);
			});
		}
	}

	UpdateMatches();
}

void WarpCurrentFunctionWidget::UpdateMatches()
{
	if (!m_current)
		return;
	const auto guid = Warp::GetAnalysisFunctionGUID(*m_current);
	if (!guid.has_value())
		return;

	// Set the matched function for highlighting.
	Warp::Ref<Warp::Function> matchedFunction = Warp::Function::GetMatched(*m_current);
	m_tableWidget->GetModel()->SetMatchedFunction(matchedFunction);

	// We swapped functions, reset the info widget to the default state with new analysis function.
	m_infoWidget->SetFunction(matchedFunction);
	m_infoWidget->UpdateInfo();

	Warp::Ref<Warp::Target> target = Warp::Target::FromPlatform(*m_current->GetPlatform());

	// Add all the possible matches for the current function to the model.
	QVector<WarpFunctionItem*> matches;
	bool matchedFuncAdded = false;
	// TODO: When we add in the networked container we need to update this stuff on a separate thread and show a spinny
	// thing.
	for (const auto& container : Warp::Container::All())
	{
		for (const auto& source : container->GetSourcesWithFunctionGUID(*target, guid.value()))
		{
			for (const auto& function : container->GetFunctionsWithGUID(*target, source, guid.value()))
			{
				// TODO: This does not work.
				if (matchedFunction && BNWARPFunctionsEqual(function->m_object, matchedFunction->m_object))
					matchedFuncAdded = true;
				auto item = new WarpFunctionItem(function, m_current);
				item->SetContainer(container);
				item->SetSource(source);
				matches.emplace_back(item);
			}
		}
	}

	// Add the matched function unconditionally, assuming it has not been found in a container.
	// NOTE: This happens when you load from a database for example.
	if (matchedFunction && !matchedFuncAdded)
	{
		auto item = new WarpFunctionItem(matchedFunction, m_current);
		matches.emplace_back(item);
	}

	m_tableWidget->SetFunctions(matches);
}