-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
VTMatchOneToManyTableProvider.java
588 lines (500 loc) · 18.2 KB
/
VTMatchOneToManyTableProvider.java
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.feature.vt.gui.provider.onetomany;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import docking.ActionContext;
import docking.action.builder.ActionBuilder;
import docking.widgets.label.GDLabel;
import docking.widgets.table.GTable;
import docking.widgets.table.RowObjectTableModel;
import docking.widgets.table.threaded.ThreadedTableModel;
import generic.theme.GColor;
import generic.theme.GIcon;
import ghidra.app.services.FunctionComparisonService;
import ghidra.feature.vt.api.impl.VTEvent;
import ghidra.feature.vt.api.main.*;
import ghidra.feature.vt.gui.actions.*;
import ghidra.feature.vt.gui.filters.*;
import ghidra.feature.vt.gui.filters.Filter.FilterEditingStatus;
import ghidra.feature.vt.gui.plugin.*;
import ghidra.feature.vt.gui.provider.markuptable.DisplayableListingAddress;
import ghidra.feature.vt.gui.provider.matchtable.MatchTableRenderer;
import ghidra.feature.vt.gui.util.AbstractVTMatchTableModel.StatusTableColumn;
import ghidra.feature.vt.gui.util.MatchInfo;
import ghidra.feature.vt.gui.util.MatchStatusRenderer;
import ghidra.framework.model.*;
import ghidra.framework.options.Options;
import ghidra.framework.plugintool.ComponentProviderAdapter;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.util.HelpLocation;
import ghidra.util.SystemUtilities;
import ghidra.util.layout.HorizontalLayout;
import ghidra.util.table.GhidraTable;
import ghidra.util.table.GhidraThreadedTablePanel;
/**
* The docking window that provides a table of the other tool's function matches
* for the function containing the current cursor location in this tool's
* listing.
*/
public abstract class VTMatchOneToManyTableProvider extends ComponentProviderAdapter
implements FilterDialogModel<VTMatch>, VTControllerListener, VTSubToolManagerListener {
private static final String TITLE_PREFIX = "Version Tracking Matches for ";
private static final Icon ICON = new GIcon("icon.version.tracking.provider.one.to.many");
private static final Icon COMPARISON_ICON = new GIcon("icon.plugin.functioncompare.new");
protected static final Color LOCAL_INFO_FOREGROUND_COLOR = new GColor(
"color.fg.version.tracking.function.match.local.info");
private JComponent component;
private MatchThreadedTablePanel tablePanel;
protected GhidraTable matchesTable;
private ListSelectionListener matchSelectionListener;
protected VTMatchOneToManyTableModel oneToManyTableModel;
private JToggleButton ancillaryFilterButton;
private Set<Filter<VTMatch>> filters = new HashSet<>();
private FilterStatusListener refilterListener = new RefilterListener();
protected final VTController controller;
protected final VTSubToolManager subToolManager;
private Address matchAddress = null;
private JPanel localPanel;
private JLabel label;
private JLabel labelValue;
private JLabel labelType;
private JLabel labelTypeValue;
private JLabel address;
private JLabel addressValue;
private boolean isSource;
private VTMatch latestMatch;
private boolean filteringFrozen;
// a selection we may have to set later, after the table has finished loading
private VTMatch pendingMatchSelection;
public VTMatchOneToManyTableProvider(PluginTool tool, VTController controller,
VTSubToolManager subToolManager,
boolean isSource) {
super(tool, TITLE_PREFIX + (isSource ? "Source" : "Destination"), VTPlugin.OWNER);
this.controller = controller;
this.subToolManager = subToolManager;
this.isSource = isSource;
setWindowGroup(VTPlugin.WINDOW_GROUP);
setIcon(ICON);
component = createComponent();
createActions();
controller.addListener(this);
subToolManager.addListener(this);
addToTool();
HelpLocation helpLocation =
new HelpLocation("VersionTrackingPlugin", "Related Matches Table");
setHelpLocation(helpLocation);
setVisible(true);
}
@Override
public String getTitle() {
return TITLE_PREFIX + (isSource ? "Source" : "Destination");
}
private void createActions() {
addLocalAction(new SetVTMatchFromOneToManyAction(controller, true));
addLocalAction(new ClearMatchAction(controller));
addLocalAction(new AcceptMatchAction(controller));
new ActionBuilder("Compare Functions", getName())
.popupMenuPath("Compare Functions")
.popupMenuIcon(COMPARISON_ICON)
.popupMenuGroup("AAA_VT_Main")
.keyBinding("shift c")
.sharedKeyBinding()
.description("Compares the Function(s) with its remote match")
.helpLocation(new HelpLocation("VersionTrackingPlugin", "Compare_Functions"))
.withContext(VTMatchOneToManyContext.class)
.enabledWhen(this::isValidFunctionComparison)
.onAction(this::compareFunctions)
.buildAndInstallLocal(this);
}
private boolean isValidFunctionComparison(VTMatchOneToManyContext context) {
List<VTMatch> functionMatches = context.getFunctionMatches();
return !functionMatches.isEmpty();
}
private void compareFunctions(VTMatchOneToManyContext c) {
List<VTMatch> selectedMatches = c.getSelectedMatches();
Set<Function> leftFunctions = new HashSet<>();
Set<Function> rightFunctions = new HashSet<>();
for (VTMatch match : selectedMatches) {
MatchInfo matchInfo = controller.getMatchInfo(match);
// Whichever codebrowser we are currently in, is what will be on the left
// side of the compare functions window.
Function leftFunction = matchInfo.getSourceFunction(),
rightFunction = matchInfo.getDestinationFunction();
if (!isSource) {
leftFunction = matchInfo.getDestinationFunction();
rightFunction = matchInfo.getSourceFunction();
}
leftFunctions.add(leftFunction);
rightFunctions.add(rightFunction);
}
// NOTE: in this case the left functions will always be the same function (ie the one in the
// current codebrowser) so leftFunctions will be size one. The rightFunctions will be one or
// more since the src/dst match tables contain all possible matches to the current listing
// function.
FunctionComparisonService service = tool.getService(FunctionComparisonService.class);
service.compareFunctions(leftFunctions, rightFunctions);
}
@Override
public void componentHidden() {
matchesTable.getSelectionModel().clearSelection();
loadLocalInfo(null);
oneToManyTableModel.setAddress(null);
}
@Override
public void componentShown() {
oneToManyTableModel.sessionChanged();
setSelectedMatch(latestMatch);
setAddress(matchAddress);
}
JTable getTable() {
return matchesTable;
}
private JComponent createComponent() {
JPanel localMatchInfoPanel = createLocalInfoPanel();
matchesTable = initializeMatchesTable();
JPanel matchDestinationTablePanel = new JPanel(new BorderLayout());
matchDestinationTablePanel.add(tablePanel, BorderLayout.CENTER);
JPanel parentPanel = new JPanel(new BorderLayout());
parentPanel.add(localMatchInfoPanel, BorderLayout.NORTH);
parentPanel.add(matchDestinationTablePanel, BorderLayout.CENTER);
return parentPanel;
}
protected GhidraTable initializeMatchesTable() {
oneToManyTableModel = getMatchesTableModel();
oneToManyTableModel.addTableModelListener(e -> {
if (matchesTable == null) {
return; // we've been disposed
}
if (pendingMatchSelection != null) {
setSelectedMatch(pendingMatchSelection);
}
});
tablePanel = new MatchThreadedTablePanel(oneToManyTableModel);
final GhidraTable table = tablePanel.getTable();
matchSelectionListener = new ListSelectionListener() {
@Override
@SuppressWarnings("unchecked")
// it's our model, it must be our type
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
// we get out the model here in case it has been wrapped by one of the filters
RowObjectTableModel<VTMatch> model =
(RowObjectTableModel<VTMatch>) table.getModel();
int selectedRow = table.getSelectedRow();
VTMatch match =
(table.getSelectedRowCount() == 1) ? model.getRowObject(selectedRow) : null;
if (!SystemUtilities.isEqual(latestMatch, match) && match != null) {
latestMatch = match;
subToolManager.setMatch(match);
}
notifyContextChanged();
}
};
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(matchSelectionListener);
// setup the renderers
TableColumnModel columnModel = table.getColumnModel();
int statusColumnIndex = oneToManyTableModel.getColumnIndex(StatusTableColumn.class);
TableColumn statusColumn = columnModel.getColumn(statusColumnIndex);
statusColumn.setCellRenderer(new MatchStatusRenderer());
// override the default behavior so we see our columns in their preferred size
Dimension size = table.getPreferredScrollableViewportSize();
Dimension preferredSize = table.getPreferredSize();
// ...account for the scroll bar width
JScrollBar scrollBar = new JScrollBar(Adjustable.VERTICAL);
Dimension scrollBarSize = scrollBar.getMinimumSize();
size.width = preferredSize.width + scrollBarSize.width;
table.setPreferredScrollableViewportSize(size);
return table;
}
protected abstract VTMatchOneToManyTableModel getMatchesTableModel();
private void refilter() {
if (filteringFrozen) {
return;
}
oneToManyTableModel.reFilter();
}
@Override
public void setSelectedMatch(VTMatch match) {
int row = oneToManyTableModel.getRowIndex(match);
if (row < 0) {
pendingMatchSelection = match;
// this happen while reloading. If so, save the match and listen for
// the table data changed and restore the selection at that point
return;
}
pendingMatchSelection = null;
matchesTable.getSelectionModel().setSelectionInterval(row, row);
Rectangle rect = matchesTable.getCellRect(row, 0, false);
matchesTable.scrollRectToVisible(rect);
}
public void repaint() {
if (matchesTable == null) {
return;
}
matchesTable.repaint();
}
public void reload() {
oneToManyTableModel.clearData();
oneToManyTableModel.reload();
}
@Override
public JComponent getComponent() {
return component;
}
@Override
public void disposed() {
if (matchesTable == null) {
return;
}
// must remove the listener first to avoid callback whilst we are disposing
ListSelectionModel selectionModel = matchesTable.getSelectionModel();
selectionModel.removeListSelectionListener(matchSelectionListener);
oneToManyTableModel.dispose();
for (Filter<VTMatch> filter : filters) {
filter.dispose();
}
}
@Override
public ActionContext getActionContext(MouseEvent event) {
List<VTMatch> selectedMatches = getSelectedMatches();
return new VTMatchOneToManyContext(this, selectedMatches);
}
private List<VTMatch> getSelectedMatches() {
List<VTMatch> list = new ArrayList<>();
int[] selectedRows = matchesTable.getSelectedRows();
for (int row : selectedRows) {
VTMatch mySelectedMatch = oneToManyTableModel.getRowObject(row);
list.add(mySelectedMatch);
}
return list;
}
private void notifyContextChanged() {
tool.contextChanged(this);
}
public void setAddress(Address matchAddress) {
boolean sameAsLastAddress = SystemUtilities.isEqual(this.matchAddress, matchAddress);
if (!sameAsLastAddress) {
// Save this address as the match address whether we are showing or not.
this.matchAddress = matchAddress;
}
if (!isVisible() || (controller.getSession() == null)) {
return;
}
if (!sameAsLastAddress) {
// If we're showing and the address changed then clear the selection.
matchesTable.getSelectionModel().clearSelection();
}
loadLocalInfo(matchAddress);
oneToManyTableModel.setAddress(matchAddress);
}
protected JPanel createLocalInfoPanel() {
localPanel = new JPanel(new HorizontalLayout(10));
JPanel labelPanel = new JPanel();
JPanel labelTypePanel = new JPanel();
JPanel addressPanel = new JPanel();
// LABEL,
String labelText = (isSource ? "Source" : "Destination") + " Label: ";
label = new GDLabel(labelText);
// label.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
labelValue = new GDLabel(" ");
labelValue.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
labelPanel.add(label);
labelPanel.add(labelValue);
// LABEL_TYPE,
String labelTypeText = "Label Type: ";
labelType = new GDLabel(labelTypeText);
// labelType.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
labelTypeValue = new GDLabel(" ");
labelTypeValue.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
labelTypePanel.add(labelType);
labelTypePanel.add(labelTypeValue);
// ADDRESS
String addressText = (isSource ? "Source" : "Destination") + " Address: ";
address = new GDLabel(addressText);
// address.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
addressValue = new GDLabel(" ");
addressValue.setForeground(LOCAL_INFO_FOREGROUND_COLOR);
addressPanel.add(address);
addressPanel.add(addressValue);
localPanel.add(labelPanel);
localPanel.add(labelTypePanel);
localPanel.add(addressPanel);
return localPanel;
}
public void loadLocalInfo(Address infoAddress) {
if (infoAddress == null) {
labelValue.setText("");
labelTypeValue.setText("");
addressValue.setText("");
return;
}
VTSession session = controller.getSession();
Program program = (isSource ? session.getSourceProgram() : session.getDestinationProgram());
// LABEL,
SymbolTable symbolTable = program.getSymbolTable();
Symbol symbol = symbolTable.getPrimarySymbol(infoAddress);
String labelValueText;
if (symbol == null) {
labelValueText = "<No Symbol>";
}
else {
labelValueText = symbol.getName();
}
labelValue.setText(labelValueText);
// LABEL_SOURCE,
String labelTypeValueText;
if (symbol == null) {
labelTypeValueText = "<none>";
}
else {
labelTypeValueText = symbol.getSource().getDisplayString();
}
labelTypeValue.setText(labelTypeValueText);
// ADDRESS
DisplayableListingAddress displayableAddress =
new DisplayableListingAddress(program, infoAddress);
addressValue.setText(displayableAddress.getDisplayString());
localPanel.validate();
localPanel.invalidate();
}
@Override
public void matchSelected(MatchInfo matchInfo) {
VTMatch match = matchInfo == null ? null : matchInfo.getMatch();
if (match == latestMatch) {
return;
}
latestMatch = match;
if (!isVisible()) {
return;
}
setSelectedMatch(match);
}
@Override
public void sessionChanged(VTSession session) {
latestMatch = null;
if (!isVisible()) {
return;
}
oneToManyTableModel.sessionChanged();
}
@Override
public void sessionUpdated(DomainObjectChangedEvent ev) {
if (!isVisible()) {
return;
}
// Check event since some may require reload of table if more or fewer matches.
boolean matchesContextChanged = false;
for (int i = 0; i < ev.numRecords(); i++) {
DomainObjectChangeRecord doRecord = ev.getChangeRecord(i);
EventType eventType = doRecord.getEventType();
if (eventType == VTEvent.ASSOCIATION_MARKUP_STATUS_CHANGED ||
eventType == VTEvent.ASSOCIATION_STATUS_CHANGED ||
eventType == VTEvent.MATCH_TAG_CHANGED) {
oneToManyTableModel.refresh();
repaint();
matchesContextChanged = true;
}
else if (eventType == DomainObjectEvent.RESTORED ||
eventType == VTEvent.MATCH_SET_ADDED || eventType == VTEvent.MATCH_ADDED ||
eventType == VTEvent.MATCH_DELETED) {
reload();
repaint();
matchesContextChanged = true;
}
}
if (matchesContextChanged) {
// Update the context so that toolbar actions fix their enablement.
tool.contextChanged(this);
}
}
@Override
public void optionsChanged(Options options) {
// do nothing
}
@Override
public void markupItemSelected(VTMarkupItem markupItem) {
// Do nothing since the one to many match table doesn't need to respond to the
// mark-up that is selected.
}
//==================================================================================================
// FilterDialogModel Methods
//==================================================================================================
@Override
public void addFilter(Filter<VTMatch> filter) {
filter.addFilterStatusListener(refilterListener);
filters.add(filter);
oneToManyTableModel.addFilter(filter);
}
/** Forces a refilter, even though filtering operations may be disabled */
@Override
public void forceRefilter() {
oneToManyTableModel.reFilter();
}
@Override
public void dialogVisibilityChanged(boolean isVisible) {
if (!isVisible) {
ancillaryFilterButton.setSelected(false);
}
filteringFrozen = isVisible; // don't allow any new filtering while this dialog is visible
refilter(); // this will do nothing if we are frozen
}
//==================================================================================================
// Inner Classes
//==================================================================================================
private class RefilterListener implements FilterStatusListener {
@Override
public void filterStatusChanged(FilterEditingStatus status) {
if (status != FilterEditingStatus.ERROR) {
refilter();
}
}
}
private class MatchThreadedTablePanel extends GhidraThreadedTablePanel<VTMatch> {
MatchThreadedTablePanel(ThreadedTableModel<VTMatch, ?> model) {
super(model);
}
@Override
protected GTable createTable(ThreadedTableModel<VTMatch, ?> model) {
return new MatchTable(model);
}
private class MatchTable extends GhidraTable {
MatchTable(RowObjectTableModel<VTMatch> model) {
super(model);
}
private TableCellRenderer renderer = new MatchTableRenderer();
@Override
public TableCellRenderer getCellRenderer(int row, int col) {
// special composite renderer
return renderer;
}
}
}
}