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

57 progress status for fetching ann results #60

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public boolean isAddToPopup(ActionContext context) {
if (context instanceof ListingActionContext) {
ListingActionContext lac = (ListingActionContext) context;
ProgramLocation location = lac.getLocation();
if (location instanceof FunctionSignatureFieldLocation) {
return true;
}

return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@
import java.awt.Component;
import javax.swing.JCheckBox;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
* GUI for displaying results from a FunctionSimularity request
*/
public class RenameFunctionFromSimilarFunctionsPanel extends JPanel {
private static final long serialVersionUID = -7365592104915627273L;
private static final String PROGRESS_DEFAULT_MSG = "Waiting to Fetch";
private static final String PROGRESS_FETCHING_MSG = "Fetching Results";
private static final String PROGRESS_GOT_RESULTS_MSG = "Done";
private GTable canidateFunctionsTable;
private CanidateFunctionModel cfm = new CanidateFunctionModel();
private Function functionUnderReview;
Expand All @@ -48,13 +60,18 @@ public class RenameFunctionFromSimilarFunctionsPanel extends JPanel {
private JScrollPane canidateFunctionsScrollPanel;
private JPanel actionButtonPanel;
private JPanel parametersPanel;
private JSeparator separator_1;
private JSeparator separator;
private JPanel numResultsPanel;
private JTextField numResultsTf;
private JLabel lblNumResults;
private JPanel debugSymbolsCheckPanel;
private JCheckBox chckbxNewCheckBox;
private JLabel lblParamsPanelTitle;
private Lock lock = new ReentrantLock();
private JProgressBar progressBar;
private JPanel progressPanel;
private JLabel lblProgressStatusText;
private JButton btnRefresh;

public RenameFunctionFromSimilarFunctionsPanel(Function functionUnderReview, PluginTool tool) {
this.functionUnderReview = functionUnderReview;
Expand All @@ -67,38 +84,7 @@ public RenameFunctionFromSimilarFunctionsPanel(Function functionUnderReview, Plu

actionButtonPanel = new JPanel();
add(actionButtonPanel, BorderLayout.WEST);

JButton btnRename = new JButton("Rename");
btnRename.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRename.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {

int tableCursor = canidateFunctionsTable.getSelectedRow();

if (tableCursor != -1) {
int transactionID = currentProgram.startTransaction("Rename function from similar functions");
try {
functionUnderReview.setName((String) canidateFunctionsTable.getValueAt(tableCursor, 0),
SourceType.USER_DEFINED);
currentProgram.endTransaction(transactionID, true);
} catch (DuplicateNameException exc) {
System.err.println("Symbol already exists");
currentProgram.endTransaction(transactionID, false);
Msg.showError(actionButtonPanel, btnRename,
ReaiPluginPackage.WINDOW_PREFIX + "Rename Function Error", exc.getMessage());
} catch (Exception exc) {
currentProgram.endTransaction(transactionID, false);
System.err.println("Unknown Error");
}
}
}
});
actionButtonPanel.setLayout(new BoxLayout(actionButtonPanel, BoxLayout.Y_AXIS));
actionButtonPanel.add(btnRename);

JSeparator separator = new JSeparator();
actionButtonPanel.add(separator);

parametersPanel = new JPanel();
actionButtonPanel.add(parametersPanel);
Expand Down Expand Up @@ -129,23 +115,94 @@ public void mouseClicked(MouseEvent e) {
numResultsPanel.add(numResultsTf);
numResultsTf.setColumns(3);

JButton btnRefresh = new JButton("Refresh");
btnRefresh = new JButton("Fetch Results");
btnRefresh.setEnabled(false);
btnRefresh.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRefresh.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
populateTableResults();
if (!btnRefresh.isEnabled()) {
return;
}
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
populateTableResults();
return null;
}
};
worker.execute();
}
});

separator_1 = new JSeparator();
separator = new JSeparator();
actionButtonPanel.add(separator);

JButton btnRename = new JButton("Rename");
btnRename.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRename.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int tableCursor = canidateFunctionsTable.getSelectedRow();

if (tableCursor != -1) {
int transactionID = currentProgram.startTransaction("Rename function from similar functions");
try {
functionUnderReview.setName((String) canidateFunctionsTable.getValueAt(tableCursor, 0),
SourceType.USER_DEFINED);
currentProgram.endTransaction(transactionID, true);
} catch (DuplicateNameException exc) {
System.err.println("Symbol already exists");
currentProgram.endTransaction(transactionID, false);
Msg.showError(actionButtonPanel, btnRename,
ReaiPluginPackage.WINDOW_PREFIX + "Rename Function Error", exc.getMessage());
} catch (Exception exc) {
currentProgram.endTransaction(transactionID, false);
System.err.println("Unknown Error");
}
}
}
});

progressPanel = new JPanel();
actionButtonPanel.add(progressPanel);
progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.Y_AXIS));

progressBar = new JProgressBar();
progressPanel.add(progressBar);

lblProgressStatusText = new JLabel("Waiting to Fetch");
lblProgressStatusText.setAlignmentX(Component.CENTER_ALIGNMENT);
lblProgressStatusText.setHorizontalAlignment(SwingConstants.CENTER);
progressPanel.add(lblProgressStatusText);

JSeparator separator_1 = new JSeparator();
actionButtonPanel.add(separator_1);
actionButtonPanel.add(btnRename);
actionButtonPanel.add(btnRefresh);

canidateFunctionsScrollPanel = new JScrollPane();
add(canidateFunctionsScrollPanel, BorderLayout.CENTER);

canidateFunctionsTable = new GTable(cfm);
canidateFunctionsTable.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("pressed a key");
char keyChar = e.getKeyChar();
int modifiers = e.getModifiersEx();
// Ctrl+C (or Cmd+C on macOS) was pressed
if ((modifiers & KeyEvent.CTRL_DOWN_MASK) != 0 && keyChar == 'c') {
int tableRowCursor = canidateFunctionsTable.getSelectedRow();
int tableColCursor = canidateFunctionsTable.getSelectedColumn();
String value = (String) canidateFunctionsTable.getValueAt(tableRowCursor, tableColCursor);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
System.out.println("Copied: "+value);
StringSelection selection = new StringSelection(value);
clipboard.setContents(selection, null);
}
}
});
canidateFunctionsScrollPanel.setViewportView(canidateFunctionsTable);

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
Expand All @@ -155,42 +212,58 @@ protected Void doInBackground() throws Exception {
return null;
}
};

worker.execute();
}

private void populateTableResults() {
cfm.clearData();
lock.lock();
btnRefresh.setEnabled(false);
try {
System.out.println("Starting fetch");
lblProgressStatusText.setText(PROGRESS_FETCHING_MSG);
progressBar.setValue(25);
cfm.clearData();

ApiResponse res = apiService.embeddings(currentBinaryHash);

if (res.getStatusCode() > 299) {
Msg.showError(actionButtonPanel, canidateFunctionsScrollPanel,
ReaiPluginPackage.WINDOW_PREFIX + "Function Simularity", res.getJsonObject().get("error"));
return;
}

ApiResponse res = apiService.embeddings(currentBinaryHash);
progressBar.setValue(50);

if (res.getStatusCode() > 299) {
Msg.showError(actionButtonPanel, canidateFunctionsScrollPanel,
ReaiPluginPackage.WINDOW_PREFIX + "Function Simularity", res.getJsonObject().get("error"));
return;
}
Binary bin = new Binary(res.getJsonArray());

Binary bin = new Binary(res.getJsonArray());
FunctionEmbedding fe = bin
.getFunctionEmbedding(Long.parseLong(functionUnderReview.getEntryPoint().toString(), 16));

FunctionEmbedding fe = bin.getFunctionEmbedding(Long.parseLong(functionUnderReview.getEntryPoint().toString(), 16));
if (fe == null) {
Msg.showError(bin, canidateFunctionsScrollPanel,
ReaiPluginPackage.WINDOW_PREFIX + "Find Similar Functions", "No similar functions found");
return;
}

if (fe == null) {
Msg.showError(bin, canidateFunctionsScrollPanel, ReaiPluginPackage.WINDOW_PREFIX + "Find Similar Functions",
"No similar functions found");
return;
}
res = apiService.nearestSymbols(fe.getEmbedding(), currentBinaryHash,
Integer.parseInt(numResultsTf.getText()), null);

res = apiService.nearestSymbols(fe.getEmbedding(), currentBinaryHash, Integer.parseInt(numResultsTf.getText()),
null);
System.out.println(fe.getEmbedding());

System.out.println(fe.getEmbedding());
JSONArray jCanidateFunctions = res.getJsonArray();

JSONArray jCanidateFunctions = res.getJsonArray();
progressBar.setValue(75);

for (int i = 0; i < jCanidateFunctions.length(); i++) {
JSONObject jCanidateFunction = jCanidateFunctions.getJSONObject(i);
cfm.addRow(new String[] { jCanidateFunction.getString("name"), jCanidateFunction.get("distance").toString(),
jCanidateFunction.getString("binary_name") });
for (int i = 0; i < jCanidateFunctions.length(); i++) {
JSONObject jCanidateFunction = jCanidateFunctions.getJSONObject(i);
cfm.addRow(new String[] { jCanidateFunction.getString("name"),
jCanidateFunction.get("distance").toString(), jCanidateFunction.getString("binary_name") });
}
} finally {
progressBar.setValue(100);
lblProgressStatusText.setText(PROGRESS_GOT_RESULTS_MSG);
btnRefresh.setEnabled(true);
lock.unlock();
}
}

Expand All @@ -209,4 +282,16 @@ protected JCheckBox getChckbxNewCheckBox() {
protected JTextField getNumResultsTf() {
return numResultsTf;
}

protected JProgressBar getProgressBar() {
return progressBar;
}

protected JLabel getLblProgressStatusText() {
return lblProgressStatusText;
}

protected JButton getBtnRefresh() {
return btnRefresh;
}
}