Merge remote-tracking branch 'origin/GT-3227-dragonmacher-equate-manager-transactions'

This commit is contained in:
Ryan Kurtz 2019-10-18 08:47:44 -04:00
commit 792ad17cdb
6 changed files with 306 additions and 576 deletions

View file

@ -15,13 +15,20 @@
*/
package ghidra.app.plugin.core.equate;
import java.util.*;
import ghidra.framework.cmd.BackgroundCommand;
import ghidra.framework.model.DomainObject;
import ghidra.program.database.symbol.EquateManager;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.Enum;
import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.*;
import ghidra.program.model.scalar.Scalar;
import ghidra.program.model.symbol.Equate;
import ghidra.program.model.symbol.EquateTable;
import ghidra.util.exception.CancelledException;
import ghidra.util.Msg;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
public class CreateEnumEquateCommand extends BackgroundCommand {
@ -30,6 +37,7 @@ public class CreateEnumEquateCommand extends BackgroundCommand {
private Enum enoom;
private Program program;
private boolean shouldDoOnSubOps;
private EquateTable equateTable;
/**
* Constructor
@ -37,33 +45,123 @@ public class CreateEnumEquateCommand extends BackgroundCommand {
* @param program The program to use
* @param addresses The addresses to apply an enum to
* @param enoom The enum to apply equates with
* @param shouldIncludeTypes True if the equate name should include the enum name.
* @param shouldDoOnSubOps True if the enum should also be applied to the sub-operands.
* @param shouldDoOnSubOps true if the enum should also be applied to the sub-operands.
*/
public CreateEnumEquateCommand(Program program, AddressSetView addresses, Enum enoom,
boolean shouldDoOnSubOps) {
this.program = program;
this.addresses = addresses;
this.enoom = enoom;
this.program = Objects.requireNonNull(program);
this.addresses = Objects.requireNonNull(addresses);
this.enoom = Objects.requireNonNull(enoom);
this.shouldDoOnSubOps = shouldDoOnSubOps;
}
@Override
public boolean applyTo(DomainObject obj, TaskMonitor monitor) {
EquateTable et = program.getEquateTable();
try {
et.applyEnum(addresses, enoom, monitor, shouldDoOnSubOps);
}
catch (CancelledException e) {
return false;
}
return true;
}
@Override
public String getName() {
return "Create Enum Equate Command";
}
@Override
public boolean applyTo(DomainObject obj, TaskMonitor monitor) {
equateTable = program.getEquateTable();
try {
applyEnum(monitor);
}
catch (CancelledException e) {
return false;
}
return true;
}
private void applyEnum(TaskMonitor monitor) throws CancelledException {
Listing listing = program.getListing();
InstructionIterator it = listing.getInstructions(addresses, true);
monitor.initialize(addresses.getNumAddresses());
while (it.hasNext()) {
monitor.checkCanceled();
Instruction instruction = it.next();
processsEquates(instruction);
monitor.incrementProgress(instruction.getLength());
}
}
private void processsEquates(Instruction instruction) {
for (int opIndex = 0; opIndex < instruction.getNumOperands(); opIndex++) {
if (!shouldDoOnSubOps) {
// Only apply equates to scalars that are not contained in sub operands.
Scalar scalar = instruction.getScalar(opIndex);
maybeCreateEquateOnScalar(instruction, opIndex, scalar);
}
else {
// Apply equates to scalars in the sub operands as well.
List<?> subOperands = instruction.getDefaultOperandRepresentationList(opIndex);
for (Object subOp : subOperands) {
maybeCreateEquateOnScalar(instruction, opIndex, subOp);
}
}
}
}
private void maybeCreateEquateOnScalar(Instruction instruction, int opIndex,
Object operandRepresentation) {
if (!(operandRepresentation instanceof Scalar)) {
return;
}
Scalar scalar = (Scalar) operandRepresentation;
int enoomLength = enoom.getLength();
boolean anyValuesMatch = Arrays.stream(enoom.getValues()).anyMatch(enumValue -> {
return scalar.equals(new Scalar(enoomLength * 8, enumValue, scalar.isSigned()));
});
if (!anyValuesMatch) {
return;
}
if (program.getDataTypeManager().findDataTypeForID(enoom.getUniversalID()) == null) {
enoom = (Enum) program.getDataTypeManager().addDataType(enoom, null);
}
Address addr = instruction.getAddress();
removeUnusedEquates(opIndex, scalar, addr);
long value = scalar.getValue();
String equateName = EquateManager.formatNameForEquate(enoom.getUniversalID(), value);
Equate equate = getOrCreateEquate(equateName, value);
equate.addReference(addr, opIndex);
}
private void removeUnusedEquates(int opIndex, Scalar scalar, Address addr) {
Equate existingEquate = equateTable.getEquate(addr, opIndex, scalar.getValue());
if (existingEquate != null) {
if (existingEquate.getReferenceCount() <= 1) {
equateTable.removeEquate(existingEquate.getName());
}
}
}
private Equate getOrCreateEquate(String name, long value) {
Equate equate = equateTable.getEquate(name);
if (equate != null) {
return equate;
}
try {
equate = equateTable.createEquate(name, value);
}
catch (DuplicateNameException | InvalidInputException e) {
// These should not happen:
// Duplicate will not happen since we checked for the existence first; Invalid
// can't happen since we built the name ourselves (we are assuming)
Msg.error(this, "Unexpected error creating equate", e); // just in case
}
return equate;
}
}

View file

@ -30,7 +30,6 @@ import ghidra.app.plugin.PluginCategoryNames;
import ghidra.app.util.bean.SetEquateDialog;
import ghidra.app.util.bean.SetEquateDialog.SelectionType;
import ghidra.app.util.datatype.ApplyEnumDialog;
import ghidra.app.util.datatype.DataTypeSelectionDialog;
import ghidra.framework.cmd.BackgroundCommand;
import ghidra.framework.cmd.CompoundBackgroundCommand;
import ghidra.framework.plugintool.*;
@ -74,23 +73,14 @@ public class EquatePlugin extends Plugin {
private SetEquateDialog setEquateDialog;
private ApplyEnumDialog applyEnumDialog;
/**
* Constructor.
*
* @param tool
*/
public EquatePlugin(PluginTool tool) {
super(tool);
createActions();
}
/**
/*
* Returns the GUI for the {@link SetEquateDialog}. Note that this function will close
* any instance of the dialog that is currently open and construct a new one.
*
* @param context
* @param scalar
* @return
*/
private SetEquateDialog createEquateDialog(ListingActionContext context, Scalar scalar) {
if (setEquateDialog != null) {
@ -105,11 +95,9 @@ public class EquatePlugin extends Plugin {
return setEquateDialog;
}
/**
/*
* Returns the GUI for the {@link DataTypeSelectionDialog}. Note that this function will
* close any instance of the dialog that is currently open and construct a new one.
* @param context
* @return
*/
private ApplyEnumDialog applyEnumDialog(ListingActionContext context) {
DataTypeManager dtm = context.getProgram().getDataTypeManager();
@ -122,11 +110,6 @@ public class EquatePlugin extends Plugin {
return applyEnumDialog;
}
/**
* Destroys the {@link SetEquateDialog}.
*
* @param dialog
*/
private void dispose(SetEquateDialog dialog) {
if (setEquateDialog == dialog) {
setEquateDialog.dispose();
@ -146,7 +129,7 @@ public class EquatePlugin extends Plugin {
* array or composite (CreateEquateCmd does not currently support such data cases)</li>
* </ul>
* Currently markup of equates is not supported within composite or array data
* @param context
* @param context the action context
* @return true if current location satisfies the above constraints
*/
protected boolean isEquatePermitted(ListingActionContext context) {
@ -182,7 +165,7 @@ public class EquatePlugin extends Plugin {
* ultimately create the background tasks that will create the proper equate(s) for
* the selected addresses.
*
* @param context
* @param context the action context
*/
private void setEquate(ListingActionContext context) {
@ -252,7 +235,7 @@ public class EquatePlugin extends Plugin {
/**
* Called in response to the user selecting the Apply Enum action from the popup menu. This
* action will apply enum values to scalars in a selection.
* @param context
* @param context the action context
*/
private void applyEnum(ListingActionContext context) {
applyEnumDialog = applyEnumDialog(context);
@ -281,7 +264,7 @@ public class EquatePlugin extends Plugin {
/**
* Called in response to the user activating rename action from the context menu.
*
* @param context
* @param context the action context
*/
private void renameEquate(ListingActionContext context) {
@ -348,12 +331,6 @@ public class EquatePlugin extends Plugin {
dispose(setEquateDialog);
}
/**
* Returns the equate for the given context listing.
*
* @param context
* @return
*/
private Equate getEquate(ListingActionContext context) {
EquateTable equateTable = context.getProgram().getEquateTable();
Scalar s = getScalar(context);
@ -363,15 +340,6 @@ public class EquatePlugin extends Plugin {
return equateTable.getEquate(context.getAddress(), getOperandIndex(context), s.getValue());
}
/**
* Renames all equates matching the given old equate name, within the address space identified
* by the iterator.
*
* @param context
* @param oldEquate
* @param newEquateName
* @param iter
*/
private void renameEquate(ListingActionContext context, Equate oldEquate, String newEquateName,
CodeUnitIterator iter) {
@ -476,14 +444,6 @@ public class EquatePlugin extends Plugin {
}
}
/**
* Returns the list of operands for the given {@link Instruction} that match the given
* {@link Equate}.
*
* @param instruction
* @param equate
* @return an array of matches, in the format: [operand][operand-obj]
*/
private List<Integer> getInstructionMatches(Program program, Instruction instruction,
Equate equate) {
@ -521,15 +481,6 @@ public class EquatePlugin extends Plugin {
return matches;
}
/**
* Returns true if the given {@link Data} object is a scalar value that has an equate
* that matches the one passed-in.
*
* @param data
* @param context
* @param equate
* @return
*/
private boolean isDataMatch(Data data, ListingActionContext context, Equate equate) {
if (!data.isDefined()) {
@ -560,12 +511,10 @@ public class EquatePlugin extends Plugin {
return false;
}
/**
/*
* Removes equates within the selected region, or a single equate if there's
* no selection. The user will be prompted for confirmation before
* removing multiple equates in a selection.
*
* @param context
*/
private void removeSelectedEquates(ListingActionContext context) {
@ -606,7 +555,8 @@ public class EquatePlugin extends Plugin {
}
/**
* Get the instruction at the location provided by the ProgramLocationProvider
* Get the instruction at the location provided by the context
* @param context the action context
* @return code unit containing current location if found (component data unsupported)
*/
CodeUnit getCodeUnit(ListingActionContext context) {
@ -619,7 +569,7 @@ public class EquatePlugin extends Plugin {
/**
* Get the operand index at the location
*
* @param context the action context
* @return 0-3 for a good operand location, -1 otherwise
*/
int getOperandIndex(ListingActionContext context) {
@ -630,11 +580,6 @@ public class EquatePlugin extends Plugin {
return -1;
}
/**
*
* @param context
* @return
*/
int getSubOperandIndex(ListingActionContext context) {
ProgramLocation location = context.getLocation();
if (location instanceof OperandFieldLocation) {
@ -649,10 +594,6 @@ public class EquatePlugin extends Plugin {
return scalar;
}
////////////////////////////////////////////////////////////////
// *** private methods ***
////////////////////////////////////////////////////////////////
private Scalar getScalar(CodeUnit cu, ListingActionContext context) {
int opIndex = getOperandIndex(context);
int subOpIndex = getSubOperandIndex(context);
@ -664,7 +605,7 @@ public class EquatePlugin extends Plugin {
* Get scalar value associated with the specified code unit,
* opIndex and subOpindex. NOTE: this method does not support
* composite or array data (null will always be returned).
* @param cu cpde unit
* @param cu code unit
* @param opIndex operand index
* @param subOpIndex sub-operand index
* @return scalar value or null

View file

@ -38,16 +38,13 @@ import ghidra.program.model.data.*;
import ghidra.program.model.data.Enum;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv;
import ghidra.util.InvalidNameException;
/**
* Tests for the make enum from a selection of enums action
* Tests for the 'make enum from a selection' action
*/
public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegrationTest {
private static final String PROGRAM_FILENAME = "notepad";
private static final int TASK_TIMEOUT = 2000;
//private Program program;
private PluginTool tool;
private ProgramDB program;
private TestEnv env;
@ -57,10 +54,6 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
private ArchiveRootNode archiveRootNode;
private ArchiveNode programNode;
public CreateEnumFromSelectionTest() {
super();
}
@Before
public void setUp() throws Exception {
env = new TestEnv();
@ -93,16 +86,6 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
return builder.getProgram();
}
// @Override
// protected void tearDown() throws Exception {
// executeOnSwingWithoutBlocking(new Runnable() {
// public void run() {
// ProgramManager pm = tool.getService(ProgramManager.class);
// pm.closeProgram();
//
// }
// });
// }
@After
public void tearDown() throws Exception {
@ -116,8 +99,7 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
});
// this handles the save changes dialog and potential analysis dialogs
closeAllWindowsAndFrames();
env.release(program);
closeAllWindows();
env.dispose();
}
@ -174,7 +156,7 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
}
});
Window window = waitForWindow(tool.getToolFrame(), "Name new ENUM", TASK_TIMEOUT);
Window window = waitForWindow("Name new ENUM");
assertNotNull(window);
final JTextField tf = findComponent(window, JTextField.class);
@ -274,7 +256,7 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
}
});
Window window = waitForWindow(tool.getToolFrame(), "Name new ENUM", TASK_TIMEOUT);
Window window = waitForWindow("Name new ENUM");
assertNotNull(window);
final JTextField tf = findComponent(window, JTextField.class);
@ -283,7 +265,7 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
tf.setText("myNewEnum");
pressButtonByText(window, "OK");
Window window2 = waitForWindow(tool.getToolFrame(), "Duplicate ENUM Name", TASK_TIMEOUT);
Window window2 = waitForWindow("Duplicate ENUM Name");
assertNotNull(window2);
final JTextField tf2 = findComponent(window2, JTextField.class);
@ -334,27 +316,6 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
}
private DataTypeNode createEnum(CategoryNode categoryNode, String newEnumName, int size) {
Category category = categoryNode.getCategory();
DataTypeManager dataTypeManager = category.getDataTypeManager();
int id = dataTypeManager.startTransaction("new category");
Enum newEnum = new EnumDataType(newEnumName, size);
category.addDataType(newEnum, null);
dataTypeManager.endTransaction(id, true);
waitForTree();
return getDataTypeNode(categoryNode, newEnumName);
}
private DataTypeNode getDataTypeNode(GTreeNode parent, String dataTypeName) {
List<GTreeNode> children = parent.getChildren();
for (GTreeNode node : children) {
if (node instanceof DataTypeNode && node.getName().equals(dataTypeName)) {
return (DataTypeNode) node;
}
}
return null;
}
private void expandNode(GTreeNode node) {
tree.expandPath(node);
waitForTree();
@ -373,37 +334,4 @@ public class CreateEnumFromSelectionTest extends AbstractGhidraHeadedIntegration
private void waitForTree() {
waitForTree(tree);
}
private void waitForProgram() throws Exception {
program.flushEvents();
waitForTasks();
waitForPostedSwingRunnables();
}
private CategoryNode createCategory(CategoryNode categoryNode, String newCategoryName) {
Category category = categoryNode.getCategory();
DataTypeManager dataTypeManager = category.getDataTypeManager();
int id = dataTypeManager.startTransaction("new category");
try {
category.createCategory(newCategoryName);
}
catch (InvalidNameException e) {
// shouldn't happen
}
dataTypeManager.endTransaction(id, true);
waitForTree();
CategoryNode node = getCategoryNode(categoryNode, newCategoryName);
return node;
}
private CategoryNode getCategoryNode(GTreeNode parent, String categoryName) {
List<GTreeNode> children = parent.getChildren();
for (GTreeNode node : children) {
if (node instanceof CategoryNode && node.getName().equals(categoryName)) {
return (CategoryNode) node;
}
}
return null;
}
}

View file

@ -30,38 +30,21 @@ import ghidra.program.model.symbol.*;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.util.exception.DuplicateNameException;
import ghidra.util.exception.InvalidInputException;
import ghidra.util.task.TaskMonitorAdapter;
import ghidra.util.task.TaskMonitor;
/**
* Test the equate manager for the database implementation.
*
*
*/
public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
private ProgramDB program;
private AddressSpace space;
private EquateTable equateTable;
private int transactionID;
/**
* Constructor for EquateManagerTest.
* @param name
*/
public EquateManagerTest() {
super();
}
/*
* @see TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
program = createDefaultProgram("Test", ProgramBuilder._TOY, this);
space = program.getAddressFactory().getDefaultAddressSpace();
Memory memory = program.getMemory();
transactionID = program.startTransaction("Test");
memory.createInitializedBlock("test", addr(0), 5000, (byte) 0,
TaskMonitorAdapter.DUMMY_MONITOR, false);
memory.createInitializedBlock("test", addr(0), 5000, (byte) 0, TaskMonitor.DUMMY, false);
equateTable = program.getEquateTable();
@ -73,7 +56,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
program.release(this);
}
@Test
@Test
public void testCreateEquate() throws Exception {
Equate equate = equateTable.createEquate("Test", 100);
assertNotNull(equate);
@ -82,7 +65,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertNotNull(equate);
}
@Test
@Test
public void testCreateEquateDuplicate() throws Exception {
equateTable.createEquate("Test", 100);
equateTable.createEquate("Test-2", 1000);
@ -91,26 +74,29 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
Assert.fail("Should have gotten duplicate name exception");
}
catch (DuplicateNameException e) {
// expected
}
}
@Test
@Test
public void testBadNameForCreate() throws Exception {
try {
equateTable.createEquate("", 100);
Assert.fail("Should have gotten invalid input exception");
}
catch (InvalidInputException e) {
// expected
}
try {
equateTable.createEquate(null, 100);
Assert.fail("Should have gotten invalid input exception");
}
catch (InvalidInputException e) {
// expected
}
}
@Test
@Test
public void testBasicEquate() throws Exception {
Equate equate = equateTable.createEquate("Test", 100);
assertEquals("Test", equate.getName());
@ -118,7 +104,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(100, equate.getValue());
}
@Test
@Test
public void testAddReferenceOnEquate() throws Exception {
Equate eqTest = equateTable.createEquate("Test", 100);
eqTest.addReference(addr(100), 0);
@ -150,7 +136,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
// assertEquals(1, eqTest3.getReferenceCount());
}
@Test
@Test
public void testRemoveReference() throws Exception {
Equate eq = equateTable.createEquate("Test", 1);
@ -185,7 +171,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(7, refs.length);
}
@Test
@Test
public void testEquateIterator() throws Exception {
for (int i = 10; i < 20; i++) {
@ -203,7 +189,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
}
@Test
@Test
public void testEquateAddressIterator() throws Exception {
Equate eq = equateTable.createEquate("Test", 1);
@ -228,7 +214,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(7, count);// should filter out duplicate ref addrs
}
@Test
@Test
public void testEquateAddressIteratorStart() throws Exception {
Equate eq = equateTable.createEquate("Test", 1);
@ -247,7 +233,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(2, count);// should filter out duplicate ref addrs
}
@Test
@Test
public void testEquateAddressIteratorSet() throws Exception {
Equate eq = equateTable.createEquate("Test", 1);
@ -287,7 +273,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
}
@Test
@Test
public void testGetEquatesByValue() throws Exception {
// now create equates with the same value
for (int i = 0; i < 10; i++) {
@ -299,7 +285,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(12, equates.size());
}
@Test
@Test
public void testGetEquateByName() throws Exception {
equateTable.createEquate("Test", 500);
equateTable.createEquate("Test2", 1500);
@ -315,7 +301,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertNull(equateTable.getEquate("foo"));
}
@Test
@Test
public void testRemoveEquate() throws Exception {
equateTable.createEquate("Test", 500);
equateTable.createEquate("Test2", 1500);
@ -325,7 +311,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertNotNull(equateTable.getEquate("Test2"));
}
@Test
@Test
public void testRemoveEquateWithRefs() throws Exception {
Equate eq = equateTable.createEquate("Test", 1);
@ -344,7 +330,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertNotNull(equateTable.getEquate("Test2"));
}
@Test
@Test
public void testRemoveEquatesInRange() throws Exception {
Equate eq = equateTable.createEquate("Test1", 100);
equateTable.createEquate("Test2", 200);
@ -370,7 +356,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
}
assertEquals(6, count);
equateTable.deleteAddressRange(addr(50), addr(500), TaskMonitorAdapter.DUMMY_MONITOR);
equateTable.deleteAddressRange(addr(50), addr(500), TaskMonitor.DUMMY);
assertNull(equateTable.getEquate("Test1"));
assertNotNull(equateTable.getEquate("Test4"));
assertEquals(0, equateTable.getEquates(addr(100), 0).size());
@ -386,7 +372,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(5, count);
}
@Test
@Test
public void testRenameEquate() throws Exception {
Equate eq = equateTable.createEquate("Test1", 100);
equateTable.createEquate("Test2", 200);
@ -407,7 +393,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals(eq, temp);
}
@Test
@Test
public void testRenameEquateDuplicate() throws Exception {
Equate eq = equateTable.createEquate("Test1", 100);
equateTable.createEquate("Test2", 200);
@ -419,6 +405,7 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
Assert.fail("Should have gotten DuplicateNameException");
}
catch (DuplicateNameException e) {
// expected
}
eq = equateTable.createEquate("foo", 100);
eq.renameEquate("foo");// nothing should happen
@ -427,10 +414,11 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
Assert.fail("Should have gotten DuplicateNameException");
}
catch (DuplicateNameException e) {
// expected
}
}
@Test
@Test
public void testRenameEquateBadName() throws Exception {
Equate eq = equateTable.createEquate("Test1", 100);
try {
@ -438,12 +426,14 @@ public class EquateManagerTest extends AbstractGhidraHeadedIntegrationTest {
Assert.fail("Empty string should be invalid!");
}
catch (InvalidInputException e) {
// expected
}
try {
eq.renameEquate(null);
Assert.fail("Empty string should be invalid!");
}
catch (InvalidInputException e) {
// expected
}
}

View file

@ -17,9 +17,6 @@ package ghidra.program.database.symbol;
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import db.*;
import db.util.ErrorHandler;
@ -27,22 +24,17 @@ import ghidra.program.database.*;
import ghidra.program.database.map.AddressKeyAddressIterator;
import ghidra.program.database.map.AddressMap;
import ghidra.program.model.address.*;
import ghidra.program.model.data.DataTypeManager;
import ghidra.program.model.data.Enum;
import ghidra.program.model.listing.*;
import ghidra.program.model.scalar.Scalar;
import ghidra.program.model.symbol.Equate;
import ghidra.program.model.symbol.EquateTable;
import ghidra.program.util.ChangeManager;
import ghidra.program.util.EquateInfo;
import ghidra.util.*;
import ghidra.util.Lock;
import ghidra.util.UniversalID;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
/**
* Implementation for the Equate Table.
*
*
* Implementation of the Equate Table
*/
public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
@ -60,14 +52,12 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
/**
* Constructor
* @param handle database handle
* @param addrMap map that converts addresses to longs and longs to
* addresses
* @param addrMap map that converts addresses to longs and longs to addresses
* @param openMode one of ProgramDB.CREATE, UPDATE, UPGRADE, or READ_ONLY
* @param lock the program synchronization lock
* @param monitor the progress monitor used when upgrading.
* @throws VersionException if the database version doesn't match the current version.
* @throws IOException if a database error occurs.
* @throws CancelledException if the user cancels the upgrade.
*/
public EquateManager(DBHandle handle, AddressMap addrMap, int openMode, Lock lock,
TaskMonitor monitor) throws VersionException, IOException {
@ -103,149 +93,22 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* @see ghidra.program.database.ManagerDB#setProgram(ghidra.program.database.ProgramDB)
*/
@Override
public void setProgram(ProgramDB program) {
this.program = program;
}
/**
* @see ghidra.program.database.ManagerDB#programReady(int, int, ghidra.util.task.TaskMonitor)
*/
@Override
public void programReady(int openMode, int currentRevision, TaskMonitor monitor)
throws IOException, CancelledException {
// Nothing to do
}
/**
* @see db.util.ErrorHandler#dbError(java.io.IOException)
*/
@Override
public void dbError(IOException e) {
program.dbError(e);
}
@Override
public void applyEnum(AddressSetView addresses, Enum enoom, TaskMonitor monitor,
boolean shouldDoOnSubOps) throws CancelledException {
if (addresses == null) {
throw new IllegalArgumentException("Can't apply Enum over null addresses");
}
if (enoom == null) {
throw new IllegalArgumentException("Data Type is null");
}
Consumer<Instruction> applyEquates = instruction -> {
if (monitor.isCancelled()) {
return;
}
for (int opIndex = 0; opIndex < instruction.getNumOperands(); opIndex++) {
if (!shouldDoOnSubOps) {
// Only apply equates to scalars that are not contained in sub operands.
Scalar scalar = instruction.getScalar(opIndex);
maybeCreateEquateOnScalar(enoom, instruction, opIndex, scalar);
}
else {
// Apply equates to scalars in the sub operands as well.
List<?> subOperands = instruction.getDefaultOperandRepresentationList(opIndex);
for (Object subOp : subOperands) {
maybeCreateEquateOnScalar(enoom, instruction, opIndex, subOp);
}
}
}
};
Listing listing = program.getListing();
InstructionIterator it = listing.getInstructions(addresses, true);
Stream<Instruction> instructions = StreamSupport.stream(it.spliterator(), false);
DataTypeManager dtm = program.getDataTypeManager();
try {
lock.acquire();
int id = dtm.startTransaction("Apply Enum");
instructions.forEach(applyEquates);
dtm.endTransaction(id, true);
}
finally {
lock.release();
}
}
private void maybeCreateEquateOnScalar(Enum enoom, Instruction instruction, int opIndex,
Object operandRepresentation) {
if (!(operandRepresentation instanceof Scalar)) {
return;
}
Scalar scalar = (Scalar) operandRepresentation;
int enoomLength = enoom.getLength();
boolean anyValuesMatch = Arrays.stream(enoom.getValues()).anyMatch(enumValue -> {
return scalar.equals(new Scalar(enoomLength * 8, enumValue, scalar.isSigned()));
});
if (!anyValuesMatch) {
return;
}
if (program.getDataTypeManager().findDataTypeForID(enoom.getUniversalID()) == null) {
int transactionID = program.startTransaction("Set Equate Dialog");
try {
enoom = (Enum) program.getDataTypeManager().addDataType(enoom, null);
}
finally {
program.endTransaction(transactionID, true);
}
}
Address addr = instruction.getAddress();
removeUnusedEquates(opIndex, scalar, addr);
long value = scalar.getValue();
String equateName = EquateManager.formatNameForEquate(enoom.getUniversalID(), value);
Equate equate = getOrCreateEquate(equateName, value);
equate.addReference(addr, opIndex);
}
private void removeUnusedEquates(int opIndex, Scalar scalar, Address addr) {
Equate existingEquate = getEquate(addr, opIndex, scalar.getValue());
if (existingEquate != null) {
if (existingEquate.getReferenceCount() <= 1) {
removeEquate(existingEquate.getName());
}
}
}
private Equate getOrCreateEquate(String name, long value) {
Equate equate = getEquate(name);
if (equate != null) {
return equate;
}
try {
equate = createEquate(name, value);
}
catch (DuplicateNameException | InvalidInputException e) {
// These should not happen:
// Duplicate will not happen since we checked for the existence first; Invalid
// can't happen since we built the name ourselves (we are assuming)
Msg.error(this, "Unexpected error creating equate", e); // just in case
}
return equate;
}
/**
* @see ghidra.program.model.symbol.EquateTable#createEquate(java.lang.String, long)
*/
@Override
public Equate createEquate(String name, long value)
throws DuplicateNameException, InvalidInputException {
@ -271,9 +134,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return null;
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquate(ghidra.program.model.address.Address, int, long)
*/
@Override
public Equate getEquate(Address reference, int opIndex, long scalarValue) {
lock.acquire();
@ -302,9 +162,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return null;
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquates(ghidra.program.model.address.Address, int)
*/
@Override
public List<Equate> getEquates(Address reference, int opIndex) {
List<Equate> ret = new LinkedList<>();
@ -331,9 +188,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return ret;
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquates(ghidra.program.model.address.Address)
*/
@Override
public List<Equate> getEquates(Address reference) {
List<Equate> ret = new LinkedList<>();
@ -358,9 +212,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return ret;
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquate(java.lang.String)
*/
@Override
public Equate getEquate(String name) {
lock.acquire();
@ -369,6 +220,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return getEquateDB(equateID);
}
catch (NotFoundException e) {
// just return null below
}
catch (IOException e) {
program.dbError(e);
@ -379,9 +231,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return null;
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquateAddresses()
*/
@Override
public AddressIterator getEquateAddresses() {
@ -395,9 +244,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return new EmptyAddressIterator();
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquateAddresses(ghidra.program.model.address.Address)
*/
@Override
public AddressIterator getEquateAddresses(Address startAddr) {
try {
@ -421,9 +267,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return new EmptyAddressIterator();
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquateAddresses(ghidra.program.model.address.AddressSetView)
*/
@Override
public AddressIterator getEquateAddresses(AddressSetView set) {
try {
@ -436,9 +279,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return new EmptyAddressIterator();
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquates()
*/
@Override
public Iterator<Equate> getEquates() {
try {
@ -451,9 +291,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return new EquateIterator(null);
}
/**
* @see ghidra.program.model.symbol.EquateTable#getEquates(long)
*/
@Override
public List<Equate> getEquates(long value) {
ArrayList<Equate> list = new ArrayList<>();
@ -473,9 +310,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return list;
}
/**
* @see ghidra.program.database.ManagerDB#deleteAddressRange(ghidra.program.model.address.Address, ghidra.program.model.address.Address, ghidra.util.task.TaskMonitor)
*/
@Override
public void deleteAddressRange(Address startAddr, Address endAddr, TaskMonitor monitor)
throws CancelledException {
@ -516,9 +350,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* @see ghidra.program.model.symbol.EquateTable#removeEquate(java.lang.String)
*/
@Override
public boolean removeEquate(String name) {
if (name == null) {
@ -562,30 +393,18 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* Return the address map.
*/
AddressMap getAddressMap() {
return addrMap;
}
/**
* Get the database adapter for equate table.
*/
EquateDBAdapter getEquateDatabaseAdapter() {
return equateAdapter;
}
/**
* Get the database adapter for the equate references table.
*/
EquateRefDBAdapter getRefDatabaseAdapter() {
return refAdapter;
}
/**
* Add a reference for an equate at the given operand position.
*/
void addReference(long equateID, Address address, int opIndex, long dynamicHash)
throws IOException {
lock.acquire();
@ -622,9 +441,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* Get the references for the given equate ID.
*/
EquateRefDB[] getReferences(long equateID) throws IOException {
long[] keys = refAdapter.getRecordKeysForEquateID(equateID);
EquateRefDB[] refs = new EquateRefDB[keys.length];
@ -634,19 +450,10 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
return refs;
}
/**
* Get the number of references for the given equate ID.
*/
int getReferenceCount(long equateID) throws IOException {
return getReferences(equateID).length;
}
/**
* Remove the reference.
* @param equateDB equate
* @param refAddr ref address to remove
* @param opIndex operand index
*/
void removeReference(EquateDB equateDB, Address refAddr, short opIndex) throws IOException {
long[] keys = refAdapter.getRecordKeysForEquateID(equateDB.getKey());
@ -659,13 +466,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* Remove the reference.
* @param equateDB equate
* @param dynamicHash hash value
* @param refAddr ref address to remove
* @param opIndex operand index
*/
void removeReference(EquateDB equateDB, long dynamicHash, Address refAddr) throws IOException {
long[] keys = refAdapter.getRecordKeysForEquateID(equateDB.getKey());
@ -678,10 +478,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* Verify that the name is not null and not the empty string.
* @throws InvalidInputException if the name is null or is empty
*/
void validateName(String name) throws InvalidInputException {
if (name == null) {
throw new InvalidInputException("Name is null");
@ -693,7 +489,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
/**
* Send notification that the equate name changed.
* Send notification that the equate name changed
* @param oldName old name
* @param newName new name
*/
@ -784,66 +580,10 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
null);
}
//////////////////////////////////////////////////////////////////////
private class EquateIterator implements Iterator<Equate> {
private RecordIterator iter;
private EquateIterator(RecordIterator iter) {
this.iter = iter;
}
/**
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
if (iter != null) {
try {
return iter.hasNext();
}
catch (IOException e) {
program.dbError(e);
}
}
return false;
}
/**
* @see java.util.Iterator#next()
*/
@Override
public Equate next() {
if (iter != null) {
try {
Record record = iter.next();
if (record != null) {
return getEquateDB(record.getKey());
}
}
catch (IOException e) {
program.dbError(e);
}
}
return null;
}
/**
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
throw new UnsupportedOperationException("remove is not supported.");
}
}
Lock getLock() {
return lock;
}
/**
* @see ghidra.program.database.ManagerDB#invalidateCache(boolean)
*/
@Override
public void invalidateCache(boolean all) {
lock.acquire();
@ -856,9 +596,6 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
}
/**
* @see ghidra.program.database.ManagerDB#moveAddressRange(ghidra.program.model.address.Address, ghidra.program.model.address.Address, long, ghidra.util.task.TaskMonitor)
*/
@Override
public void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)
throws CancelledException {
@ -879,7 +616,7 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
* Formats a string to the equate format given the enum UUID and the value for the equate. The
* formatted strings are used when setting equates from datatypes so that information can be
* stored with an equate to point back to that datatype.
* @param dtID The enums data type UUID
* @param dtID The enum's data type UUID
* @param equateValue The value intended for the equate
* @return The formatted equate name
*/
@ -922,4 +659,51 @@ public class EquateManager implements EquateTable, ErrorHandler, ManagerDB {
}
return -1;
}
//==================================================================================================
// Inner Classes
//==================================================================================================
private class EquateIterator implements Iterator<Equate> {
private RecordIterator iter;
private EquateIterator(RecordIterator iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
if (iter != null) {
try {
return iter.hasNext();
}
catch (IOException e) {
program.dbError(e);
}
}
return false;
}
@Override
public Equate next() {
if (iter != null) {
try {
Record record = iter.next();
if (record != null) {
return getEquateDB(record.getKey());
}
}
catch (IOException e) {
program.dbError(e);
}
}
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove is not supported.");
}
}
}

View file

@ -21,9 +21,9 @@ import java.util.Iterator;
import java.util.List;
import ghidra.program.model.address.*;
import ghidra.program.model.data.Enum;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
/**
* EquateTable manages all equates for program. An equate defines a relationship
* between a scalar value and a string whereby the scalar may be represented by
@ -32,35 +32,20 @@ import ghidra.util.task.TaskMonitor;
*/
public interface EquateTable {
/**
* Creates equates and/or adds references for scalars
* in the given address set using the given data type.
* The data type given must be an enumeration data type.
* @param addrSet the address set to use.
* @param dataType the data type to use.
* @param monitor task monitor to cancel the remove operation
* @param shouldDoOnSubOps true if the enum should be applied inside sub-operands as well.
*
* @throws CancelledException if the operation is cancelled
* @throws IllegalArgumentException if the dataType is null or not an enum.
*/
public void applyEnum(AddressSetView addrSet, Enum dataType, TaskMonitor monitor,
boolean shouldDoOnSubOps) throws CancelledException;
/**
* Creates a new equate
* @param name the name to associate with the given value.
* @param value the value to associate with the given name.
* @return the equate
* @exception DuplicateNameException thrown if name is already in use
* as an equate.
* @throws InvalidInputException if name contains blank characters,
* is zero length, or is null
*/
public Equate createEquate(String name,long value)
public Equate createEquate(String name, long value)
throws DuplicateNameException, InvalidInputException;
/**
* Removes the equate from the program.
* @param name the name of the equate to remove.
@ -75,12 +60,13 @@ public interface EquateTable {
* @param monitor task monitor to cancel the remove operation
* @throws CancelledException if the operation was cancelled.
*/
public void deleteAddressRange(Address start, Address end, TaskMonitor monitor) throws CancelledException;
public void deleteAddressRange(Address start, Address end, TaskMonitor monitor)
throws CancelledException;
/**
* Returns the equate with the given name, null if no such equate
* exists.
* @param name the of the equate to be retrieved.
* Returns the equate with the given name, null if no such equate exists
* @param name the of the equate to be retrieved
* @return the equate
*/
public Equate getEquate(String name);
@ -114,17 +100,20 @@ public interface EquateTable {
/**
* Returns an address iterator over all the addresses where
* equates have been set.
* @return the iterator
*/
public AddressIterator getEquateAddresses();
/**
* Returns all equates defined for value.
* @param value the value to get all equates for.
* @return the equates
*/
public List<Equate> getEquates(long value);
/**
* Returns an iterator over all equates.
* @return the iterator
*/
public Iterator<Equate> getEquates();