Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6583 - Version Tracking - Option to apply empty composites for data

dragonmacherb33cc7588bf3
diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/markuptype/DataTypeMarkupType.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/markuptype/DataTypeMarkupType.java
index e507d2c680..d7248e4c6a 100644
--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/markuptype/DataTypeMarkupType.java
+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/markuptype/DataTypeMarkupType.java
@@ -4,9 +4,9 @@
* 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://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.
@@ -15,8 +15,9 @@
*/
package ghidra.feature.vt.api.markuptype;
-import java.util.ArrayList;
-import java.util.List;
+import static ghidra.feature.vt.gui.util.VTOptionDefines.*;
+
+import java.util.*;
import ghidra.feature.vt.api.impl.MarkupItemImpl;
import ghidra.feature.vt.api.main.*;
@@ -34,7 +35,6 @@ import ghidra.program.model.listing.*;
import ghidra.program.model.util.CodeUnitInsertionException;
import ghidra.program.util.*;
import ghidra.util.Msg;
-import ghidra.util.SystemUtilities;
import ghidra.util.exception.AssertException;
public class DataTypeMarkupType extends VTMarkupType {
@@ -217,6 +217,7 @@ public class DataTypeMarkupType extends VTMarkupType {
throw new VersionTrackingApplyException(
"Data Type Markup cannot be applied since there isn't Data at the destination address!");
}
+
DataType originalDataType = originalData.getDataType();
int originalDataLength = originalData.getLength();
Address endAddress;
@@ -231,6 +232,7 @@ public class DataTypeMarkupType extends VTMarkupType {
"Data Type Markup cannot be applied since there isn't enough space at the " +
"destination address!");
}
+
AddressSet addressSet = new AddressSet(startAddress, endAddress);
InstructionIterator instructions = listing.getInstructions(addressSet, true);
boolean hasInstructions = instructions.hasNext();
@@ -242,6 +244,7 @@ public class DataTypeMarkupType extends VTMarkupType {
" before this Data Type Markup can be applied.";
throw new VersionTrackingApplyException(message);
}
+
boolean replaceUndefinedDataOnly =
(replaceChoice == ReplaceDataChoices.REPLACE_UNDEFINED_DATA_ONLY);
boolean replaceFirstOnly = (replaceChoice == ReplaceDataChoices.REPLACE_FIRST_DATA_ONLY);
@@ -251,7 +254,6 @@ public class DataTypeMarkupType extends VTMarkupType {
Data nextNonUndefinedDataAfter =
DataUtilities.getNextNonUndefinedDataAfter(program, startAddress, endAddress);
boolean hasOtherDefinedData = nextNonUndefinedDataAfter != null;
-
if (replaceUndefinedDataOnly && hasDefinedData) {
// Just return since we are only replacing undefined data and this has some defined data.
return false;
@@ -263,7 +265,7 @@ public class DataTypeMarkupType extends VTMarkupType {
return false;
}
- listing.clearCodeUnits(startAddress, endAddress, false); // Clear the necessary code units.
+ listing.clearCodeUnits(startAddress, endAddress, false);
try {
listing.createData(startAddress, dataType, dataLength);
@@ -285,7 +287,7 @@ public class DataTypeMarkupType extends VTMarkupType {
// If we get an error trying to put the original back then dump a message and bail out.
Msg.error(this,
"Couldn't restore data type of " + originalDataType.getName() +
- " after failing to set data type markup at " + address.toString() + ".\n" +
+ " after failing to set data type markup at " + address.toString() + ". " +
e2.getMessage());
}
}
@@ -319,13 +321,13 @@ public class DataTypeMarkupType extends VTMarkupType {
throw new VersionTrackingApplyException("The destination Data cannot be null!");
}
- DataType sourceDataType = sourceData.getDataType();
+ VTSession session = association.getSession();
+ Program destinationProgram = session.getDestinationProgram();
+ DataType sourceDataType = getSourceDataType(sourceData, destinationProgram, markupOptions);
DataType destinationDataType = destinationData.getDataType();
- if (SystemUtilities.isEqual(sourceDataType, destinationDataType)) {
+ if (Objects.equals(sourceDataType, destinationDataType)) {
return false;
}
- VTSession session = association.getSession();
- Program destinationProgram = session.getDestinationProgram();
int sourceDataLength = sourceDataType.getLength();
if (sourceDataLength <= 0) {
@@ -342,6 +344,23 @@ public class DataTypeMarkupType extends VTMarkupType {
}
}
+ private DataType getSourceDataType(Data sourceData, Program destinationProgram,
+ ToolOptions markupOptions) {
+
+ DataType sourceDt = sourceData.getDataType();
+ if (!markupOptions.getBoolean(USE_EMPTY_COMPOSITES,
+ DEFAULT_OPTION_FOR_USE_EMPTY_STRUCTURES)) {
+ return sourceDt;
+ }
+
+ // The user would like to create empty structures when creating data types
+ ProgramBasedDataTypeManager dtm = destinationProgram.getDataTypeManager();
+ DataTypeCleaner dtCleaner = new DataTypeCleaner(dtm, false);
+ DataType cleanedDt = dtCleaner.clean(sourceDt);
+ dtCleaner.close();
+ return cleanedDt;
+ }
+
private String getApplyFailedMessage(Address sourceAddress, Address destinationAddress,
Exception e, int newLength, int oldLength) {
Address startAddress = destinationAddress.add(oldLength);
diff --git a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/stringable/FunctionSignatureStringable.java b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/stringable/FunctionSignatureStringable.java
index 34daa5ddab..6c4482ca2d 100644
--- a/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/stringable/FunctionSignatureStringable.java
+++ b/Ghidra/Features/VersionTracking/src/main/java/ghidra/feature/vt/api/stringable/FunctionSignatureStringable.java
@@ -682,9 +682,9 @@ public class FunctionSignatureStringable extends Stringable {
markupOptions.getEnum(VTOptionDefines.FUNCTION_NAME, DEFAULT_OPTION_FOR_FUNCTION_NAME);
Symbol functionSymbol = destFunction.getSymbol();
String currentDestNamespace = functionSymbol.getParentNamespace().getName();
- String newDestnClassName = destClassDt.getName();
+ String newDestClassName = destClassDt.getName();
if (functionNameChoice == FunctionNameChoices.EXCLUDE &&
- !currentDestNamespace.equals(newDestnClassName)) {
+ !currentDestNamespace.equals(newDestClassName)) {
// This check is odd. I suppose we are trying to handle the case where the name was not
// changed, but the namespace was updated. In that case, we only need to copy the class
@@ -709,8 +709,9 @@ public class FunctionSignatureStringable extends Stringable {
}
// resolve the full data type
- destDtm.resolve(dt,
- DataTypeConflictHandler.REPLACE_EMPTY_STRUCTS_OR_RENAME_AND_ADD_HANDLER);
+ DataTypeConflictHandler conflictHandler =
+ DataTypeConflictHandler.REPLACE_EMPTY_STRUCTS_OR_RENAME_AND_ADD_HANDLER;
+ destDtm.resolve(dt, conflictHandler);
warnAboutClassConflicts(destFunction, destClassDt, srcClassPath);
}
@@ -822,11 +823,11 @@ public class FunctionSignatureStringable extends Stringable {
boolean forceApply, boolean useCustomStorage, DataTypeCleaner dtCleaner)
throws InvalidInputException {
- Parameter returnParam = destFunction.getReturn();
+ Parameter existingReturnParam = destFunction.getReturn();
ParameterDataTypeChoices returnTypeChoice =
markupOptions.getEnum(FUNCTION_RETURN_TYPE, DEFAULT_OPTION_FOR_FUNCTION_RETURN_TYPE);
if (returnTypeChoice == ParameterDataTypeChoices.EXCLUDE) {
- return returnParam; // Not replacing return type.
+ return existingReturnParam; // Not replacing return type.
}
DataType toReturnType = destFunction.getReturnType();
@@ -839,12 +840,12 @@ public class FunctionSignatureStringable extends Stringable {
returnTypeChoice == ParameterDataTypeChoices.REPLACE_UNDEFINED_DATA_TYPES_ONLY;
if (!forceApply && onlyReplaceUndefineds) {
if (!isToDefault && !isToUndefined) {
- return returnParam; // can't do it because we should only replace undefined data types.
+ return existingReturnParam; // can't do it because we should only replace undefined data types.
}
}
if (!forceApply && isFromDefault) {
- return returnParam; // do nothing since default data type is lowest priority.
+ return existingReturnParam; // do nothing since default data type is lowest priority.
}
DataType returnType = (forceApply) ? fromReturnType
diff --git a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/VTMatchApplyFunctionSignatureTest.java b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/VTMatchApplyFunctionSignatureTest.java
index 0bb26f3be6..17aa8a8c94 100644
--- a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/VTMatchApplyFunctionSignatureTest.java
+++ b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/VTMatchApplyFunctionSignatureTest.java
@@ -41,6 +41,7 @@ import ghidra.program.model.lang.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.listing.Function.FunctionUpdateType;
import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.SymbolTable;
import ghidra.program.util.DefaultLanguageService;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv;
@@ -66,10 +67,6 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
// call_Strncpy 00401300 FUN... 00401310 3 params w/ matching types
// Canary_Tester_... 0040131c FUN... 0040132c 1 param & identical signature
- public VTMatchApplyFunctionSignatureTest() {
- super();
- }
-
@Before
public void setUp() throws Exception {
@@ -111,7 +108,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
env.dispose();
}
- private StructureDataType getPersonStruct(Program program) {
+ private StructureDataType createPersonStruct(Program program) {
StructureDataType struct =
new StructureDataType(CategoryPath.ROOT, "Person", 0, program.getDataTypeManager());
TypeDef personType =
@@ -125,8 +122,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
return struct;
}
- //Create Gadget structure - slightly different than usual in last field to simplify test
- private Structure createNonEmptyGadgetStruct() {
+ private Structure createSourceGadgetStruct_NotEmpty() {
Structure gadgetStruct = new StructureDataType("Gadget", 0);
PointerDataType charPtr = new PointerDataType(new CharDataType());
@@ -139,7 +135,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
}
- private Structure createDifferentGadgetStruct() {
+ private Structure createDestinationGadgetStruct_NotEmpty() {
Structure gadgetStruct = new StructureDataType("Gadget", 0);
PointerDataType charPtr = new PointerDataType(new CharDataType());
@@ -165,7 +161,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
builder.createClassNamespace("Gadget", null, SourceType.IMPORTED);
- StructureDataType struct = getPersonStruct(p);
+ StructureDataType struct = createPersonStruct(p);
builder.addDataType(struct);
Pointer ptr1 = PointerDataType.getPointer(struct, p.getDataTypeManager());
Pointer ptr2 = PointerDataType.getPointer(ptr1, p.getDataTypeManager());
@@ -472,7 +468,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
// Use case: Source has populated Gadget struct, dest has empty Gadget struct; apply empty structs
@Test
- public void testApplyMatch_ReplaceSignature_EmptyStructureOption_SourceThisParam_DestEmptyGadget()
+ public void testApplyMatch_ReplaceSignature_EmptyStructureOption_SourceThisParam_DestEmptyGadgetInDtm()
throws Exception {
useMatch("0x00401040", "0x00401040");
@@ -517,8 +513,177 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
"undefined FUN_00401040(void * this, undefined4 param_1)");
}
+ // Use Case: Source has populated Gadget struct this param,
+ // dest has different populated Gadget this param,
+ // Option: apply empty structs
+ // replace undefined only
+ // Expect: No replace
+ @Test
+ public void testApplyMatch_ReplaceUndefinedOnly_EmptyStructureOption_SourceNonEmptyGadget_DestNonEmptyGadget()
+ throws Exception {
+
+ useMatch("0x00401040", "0x00401040");
+
+ applySourceNonEmtpyGadget_DestNonEmtpyGadget();
+
+ // Check initial values
+ checkSignatures("undefined use(Gadget * this, Person * person)",
+ "undefined FUN_00401040(Gadget * this, undefined4 param_1)");
+
+ // Set the function signature options for this test
+ ToolOptions applyOptions = controller.getOptions();
+ applyOptions.setEnum(VTOptionDefines.FUNCTION_NAME,
+ FunctionNameChoices.REPLACE_DEFAULT_ONLY);
+ applyOptions.setEnum(FUNCTION_SIGNATURE,
+ FunctionSignatureChoices.WHEN_SAME_PARAMETER_COUNT);
+ applyOptions.setEnum(CALLING_CONVENTION, CallingConventionChoices.SAME_LANGUAGE);
+ applyOptions.setEnum(PARAMETER_DATA_TYPES,
+ ParameterDataTypeChoices.REPLACE_UNDEFINED_DATA_TYPES_ONLY);
+ applyOptions.setEnum(PARAMETER_NAMES, SourcePriorityChoices.REPLACE);
+ applyOptions.setEnum(PARAMETER_COMMENTS, CommentChoices.APPEND_TO_EXISTING);
+ applyOptions.setEnum(NO_RETURN, ReplaceChoices.EXCLUDE);
+ applyOptions.setEnum(FUNCTION_RETURN_TYPE, ParameterDataTypeChoices.REPLACE);
+ applyOptions.setBoolean(VTOptionDefines.USE_NAMESPACE_FUNCTIONS, true); //replace namespace
+ applyOptions.setBoolean(VTOptionDefines.USE_EMPTY_COMPOSITES, true);
+
+ applyTestMatch();
+
+ // Verify apply.
+ checkSignatures("undefined use(Gadget * this, Person * person)",
+ "undefined use(Gadget * this, Person * person)");
+ assertEquals(VTAssociationStatus.ACCEPTED, testMatch.getAssociation().getStatus());
+ checkFunctionSignatureStatus(testMatch, VTMarkupItemStatus.REPLACED);
+
+ checkDestinationGadgetThisParamIsUnchanged();
+
+ unapplyTestMatch("undefined use(Gadget * this, Person * person)",
+ "undefined FUN_00401040(Gadget * this, undefined4 param_1)");
+ }
+
+ // Use Case: Source has populated Gadget struct this param,
+ // dest has different populated Gadget this param,
+ // Option: apply empty structs
+ // replace always
+ // Expect: **The this param does not get changed, since there is already a class named Gadget
+ @Test
+ public void testApplyMatch_Replace_EmptyStructureOption_SourceNonEmptyGadget_DestNonEmptyGadget()
+ throws Exception {
+
+ useMatch("0x00401040", "0x00401040");
+
+ applySourceNonEmtpyGadget_DestNonEmtpyGadget();
+
+ // Check initial values
+ checkSignatures("undefined use(Gadget * this, Person * person)",
+ "undefined FUN_00401040(Gadget * this, undefined4 param_1)");
+
+ // Set the function signature options for this test
+ ToolOptions applyOptions = controller.getOptions();
+ applyOptions.setEnum(VTOptionDefines.FUNCTION_NAME,
+ FunctionNameChoices.REPLACE_DEFAULT_ONLY);
+ applyOptions.setEnum(FUNCTION_SIGNATURE,
+ FunctionSignatureChoices.WHEN_SAME_PARAMETER_COUNT);
+ applyOptions.setEnum(CALLING_CONVENTION, CallingConventionChoices.SAME_LANGUAGE);
+ applyOptions.setEnum(PARAMETER_DATA_TYPES, ParameterDataTypeChoices.REPLACE);
+ applyOptions.setEnum(PARAMETER_NAMES, SourcePriorityChoices.REPLACE);
+ applyOptions.setEnum(PARAMETER_COMMENTS, CommentChoices.APPEND_TO_EXISTING);
+ applyOptions.setEnum(NO_RETURN, ReplaceChoices.EXCLUDE);
+ applyOptions.setEnum(FUNCTION_RETURN_TYPE, ParameterDataTypeChoices.REPLACE);
+ applyOptions.setBoolean(VTOptionDefines.USE_NAMESPACE_FUNCTIONS, true); //replace namespace
+ applyOptions.setBoolean(VTOptionDefines.USE_EMPTY_COMPOSITES, true);
+
+ applyTestMatch();
+
+ // Verify apply.
+ checkSignatures("undefined use(Gadget * this, Person * person)",
+ "undefined use(Gadget * this, Person * person)");
+ assertEquals(VTAssociationStatus.ACCEPTED, testMatch.getAssociation().getStatus());
+ checkFunctionSignatureStatus(testMatch, VTMarkupItemStatus.REPLACED);
+
+ checkDestinationGadgetThisParamIsUnchanged();
+
+ unapplyTestMatch("undefined use(Gadget * this, Person * person)",
+ "undefined FUN_00401040(Gadget * this, undefined4 param_1)");
+ }
+
+ // Use Case: Source has populated Gadget struct return type,
+ // dest has different populated Gadget return type,
+ // Option: apply empty structs
+ // replace always
+ // Expect: **The this param does not get changed, since there is already a class named Gadget.
+ // The return type is a conflict, since both programs had a non-empty Gadget.
@Test
- public void testApplyMatch_ReplaceSignature_EmptyStructureOption_SourceReturnGadger_DestReturnUndefined()
+ public void testApplyMatch_Replace_EmptyStructureOption_SourceNonEmptyGadget_DestNonEmptyGadget_ReturnType()
+ throws Exception {
+
+ useMatch("0x00401040", "0x00401040");
+
+ applySourceNonEmtpyGadgetReturnType_DestNonEmtpyGadgetReturnType();
+
+ // Check initial values
+ checkSignatures("Gadget * use(Gadget * this, Person * person)",
+ "Gadget * FUN_00401040(void * this, undefined4 param_1)");
+
+ // Set the function signature options for this test
+ ToolOptions applyOptions = controller.getOptions();
+ applyOptions.setEnum(VTOptionDefines.FUNCTION_NAME,
+ FunctionNameChoices.REPLACE_DEFAULT_ONLY);
+ applyOptions.setEnum(FUNCTION_SIGNATURE,
+ FunctionSignatureChoices.WHEN_SAME_PARAMETER_COUNT);
+ applyOptions.setEnum(CALLING_CONVENTION, CallingConventionChoices.SAME_LANGUAGE);
+ applyOptions.setEnum(PARAMETER_DATA_TYPES,
+ ParameterDataTypeChoices.REPLACE_UNDEFINED_DATA_TYPES_ONLY);
+ applyOptions.setEnum(PARAMETER_NAMES, SourcePriorityChoices.REPLACE);
+ applyOptions.setEnum(PARAMETER_COMMENTS, CommentChoices.APPEND_TO_EXISTING);
+ applyOptions.setEnum(NO_RETURN, ReplaceChoices.EXCLUDE);
+ applyOptions.setEnum(FUNCTION_RETURN_TYPE, ParameterDataTypeChoices.REPLACE);
+ applyOptions.setBoolean(VTOptionDefines.USE_NAMESPACE_FUNCTIONS, true); //replace namespace
+ applyOptions.setBoolean(VTOptionDefines.USE_EMPTY_COMPOSITES, true);
+
+ applyTestMatch();
+
+ // Verify apply.
+ checkSignatures("Gadget * use(Gadget * this, Person * person)",
+ "Gadget.conflict * use(Gadget * this, Person * person)");
+ assertEquals(VTAssociationStatus.ACCEPTED, testMatch.getAssociation().getStatus());
+ checkFunctionSignatureStatus(testMatch, VTMarkupItemStatus.REPLACED);
+
+ checkDestinationGadgetThisParamIsUnchanged();
+
+ // Since the destination already had a non-empty Gadget, a conflict gets created. Further,
+ // the destination signature is still using the original Gadget for the this pointer. This
+ // is inconsistent with how the return type and other parameter types get applied.
+ ProgramBasedDataTypeManager destDtm = destinationProgram.getDataTypeManager();
+ DataType gadgetConflict = destDtm.getDataType(CategoryPath.ROOT, "Gadget.conflict");
+
+ // not defined, since we used the empty composite option
+ assertTrue(gadgetConflict.isNotYetDefined());
+
+ unapplyTestMatch("Gadget * use(Gadget * this, Person * person)",
+ "Gadget * FUN_00401040(void * this, undefined4 param_1)");
+ }
+
+ private void checkDestinationGadgetThisParamIsUnchanged() {
+
+ ProgramBasedDataTypeManager destDtm = destinationProgram.getDataTypeManager();
+ Structure destGadget = (Structure) destDtm.getDataType(CategoryPath.ROOT, "Gadget");
+
+ // assume the test setup the dest Gadget to be not undefined
+ assertFalse(destGadget.isNotYetDefined());
+
+ Parameter p0 = destinationFunction.getParameter(0);
+ DataType dt = p0.getDataType();
+ Pointer p = (Pointer) dt;
+ DataType actualDt = p.getDataType();
+
+ Structure expectedGadget = createDestinationGadgetStruct_NotEmpty();
+ assertTrue("Gadget should not have changed. Expected\n%s\nFound\n%s"
+ .formatted(expectedGadget, actualDt),
+ expectedGadget.isEquivalent(actualDt));
+ }
+
+ @Test
+ public void testApplyMatch_ReplaceSignature_EmptyStructureOption_SourceReturnGadget_DestReturnUndefined()
throws Exception {
useMatch("0x00401060", "0x00401060");
@@ -561,7 +726,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
}
@Test
- public void testApplyMatch_ReplaceSignature_EmptyStructureOptionOff_SourceReturnGadger_DestReturnUndefined()
+ public void testApplyMatch_ReplaceSignature_EmptyStructureOptionOff_SourceReturnGadget_DestReturnUndefined()
throws Exception {
useMatch("0x00401060", "0x00401060");
@@ -719,7 +884,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertNotNull(existingEmptyGadgetStruct);
// replace the source empty gadget with a populated one
- Structure gadgetStruct = createNonEmptyGadgetStruct();
+ Structure gadgetStruct = createSourceGadgetStruct_NotEmpty();
tx(sourceDtm, () -> {
sourceDtm.addDataType(gadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
});
@@ -798,7 +963,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertNotNull(existingEmptyGadgetStruct);
// replace the source empty gadget with a populated one
- Structure gadgetStruct = createNonEmptyGadgetStruct();
+ Structure gadgetStruct = createSourceGadgetStruct_NotEmpty();
tx(sourceDtm, () -> {
sourceDtm.addDataType(gadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
});
@@ -808,7 +973,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertTrue(updatedSourceGadget.isEquivalent(gadgetStruct));
// add different Gadget to the destination program
- Structure differentGadgetStruct = createDifferentGadgetStruct();
+ Structure differentGadgetStruct = createDestinationGadgetStruct_NotEmpty();
tx(destDtm, () -> {
destDtm.addDataType(differentGadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
});
@@ -1927,7 +2092,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertNotNull(existingEmptyGadgetStruct);
// replace the source empty gadget with a populated one
- Structure gadgetStruct = createNonEmptyGadgetStruct();
+ Structure gadgetStruct = createSourceGadgetStruct_NotEmpty();
tx(sourceDtm, () -> {
sourceDtm.addDataType(gadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
});
@@ -1965,7 +2130,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertNotNull(existingEmptyGadgetStruct);
// replace the source empty gadget with a populated one
- Structure gadgetStruct = createNonEmptyGadgetStruct();
+ Structure gadgetStruct = createSourceGadgetStruct_NotEmpty();
tx(sourceDtm, () -> {
sourceDtm.addDataType(gadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
});
@@ -1977,6 +2142,79 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
return gadgetStruct;
}
+ private void applySourceNonEmtpyGadget_DestNonEmtpyGadget()
+ throws Exception {
+
+ ProgramBasedDataTypeManager sourceDtm = sourceProgram.getDataTypeManager();
+ ProgramBasedDataTypeManager destDtm = destinationProgram.getDataTypeManager();
+
+ DataType existingEmptyGadgetStruct = sourceDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertNotNull(existingEmptyGadgetStruct);
+
+ // replace the source empty gadget with a populated one
+ Structure nonEmptyGadgetStruct = createSourceGadgetStruct_NotEmpty();
+
+ tx(sourceDtm, () -> {
+ sourceDtm.addDataType(nonEmptyGadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
+ });
+
+ // verify it took
+ DataType updatedSourceGadget = sourceDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertTrue(updatedSourceGadget.isEquivalent(nonEmptyGadgetStruct));
+
+ // add non-empty gadget to the destination program
+ Structure destGadget = createDestinationGadgetStruct_NotEmpty();
+ tx(destDtm, () -> {
+ destDtm.addDataType(destGadget, DataTypeConflictHandler.REPLACE_HANDLER);
+ });
+
+ // verify it took
+ Structure resolvedDestGadget = (Structure) destDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertFalse(resolvedDestGadget.isNotYetDefined());
+
+ tx(destinationProgram, () -> {
+ SymbolTable st = destinationProgram.getSymbolTable();
+ GhidraClass c = st.createClass(null, "Gadget", SourceType.USER_DEFINED);
+ destinationFunction.setParentNamespace(c);
+ });
+ }
+
+ private void applySourceNonEmtpyGadgetReturnType_DestNonEmtpyGadgetReturnType()
+ throws Exception {
+
+ ProgramBasedDataTypeManager sourceDtm = sourceProgram.getDataTypeManager();
+ ProgramBasedDataTypeManager destDtm = destinationProgram.getDataTypeManager();
+
+ DataType existingEmptyGadgetStruct = sourceDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertNotNull(existingEmptyGadgetStruct);
+
+ // replace the source empty gadget with a populated one
+ Structure nonEmptyGadgetStruct = createSourceGadgetStruct_NotEmpty();
+ tx(sourceDtm, () -> {
+ sourceDtm.addDataType(nonEmptyGadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
+ });
+
+ // verify it took
+ DataType updatedSourceGadget = sourceDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertTrue(updatedSourceGadget.isEquivalent(nonEmptyGadgetStruct));
+
+ PointerDataType ptr = new PointerDataType(nonEmptyGadgetStruct);
+ setReturnType(sourceFunction, ptr, SourceType.USER_DEFINED);
+
+ // add non-empty gadget to the destination program
+ Structure destGadget = createDestinationGadgetStruct_NotEmpty();
+ tx(destDtm, () -> {
+ destDtm.addDataType(destGadget, DataTypeConflictHandler.REPLACE_HANDLER);
+ });
+
+ // verify it took
+ Structure resolvedDestGadget = (Structure) destDtm.getDataType(CategoryPath.ROOT, "Gadget");
+ assertFalse(resolvedDestGadget.isNotYetDefined());
+
+ ptr = new PointerDataType(resolvedDestGadget);
+ setReturnType(destinationFunction, ptr, SourceType.USER_DEFINED);
+ }
+
private Structure applySourceNonEmtpyGadget_DestEmtpyGadgetInDtm() {
ProgramBasedDataTypeManager sourceDtm = sourceProgram.getDataTypeManager();
@@ -1986,7 +2224,7 @@ public class VTMatchApplyFunctionSignatureTest extends AbstractGhidraHeadedInteg
assertNotNull(existingEmptyGadgetStruct);
// replace the source empty gadget with a populated one
- Structure gadgetStruct = createNonEmptyGadgetStruct();
+ Structure gadgetStruct = createSourceGadgetStruct_NotEmpty();
tx(sourceDtm, () -> {
sourceDtm.addDataType(gadgetStruct, DataTypeConflictHandler.REPLACE_HANDLER);
diff --git a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/markupitem/DataTypeMarkupItemTest.java b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/markupitem/DataTypeMarkupItemTest.java
index f6e518fef1..921199ff38 100644
--- a/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/markupitem/DataTypeMarkupItemTest.java
+++ b/Ghidra/Features/VersionTracking/src/test.slow/java/ghidra/feature/vt/api/markupitem/DataTypeMarkupItemTest.java
@@ -4,9 +4,9 @@
* 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://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.
@@ -36,10 +36,6 @@ import ghidra.program.model.mem.*;
public class DataTypeMarkupItemTest extends AbstractVTMarkupItemTest {
- public DataTypeMarkupItemTest() {
- super();
- }
-
@Test
public void testReplaceDataTypeWhereNone() throws Exception {
@@ -332,10 +328,104 @@ public class DataTypeMarkupItemTest extends AbstractVTMarkupItemTest {
doTestFindAndApplyMarkupItem_ApplyFails(validator);
}
+ @Test
+ public void testReplace_EmptyStructureOption_MatchingData() throws Exception {
+
+ // apply a Gadget struct to the source
+ Structure gadget = createGadgetStruct();
+ String dataAddr = "0x01007500";
+ sourceBuilder.setBytes(dataAddr, "4c 6f 61 64 43 75 72 73 6f 72 57 00"); // arbitrary bytes
+ sourceBuilder.applyDataType(dataAddr, gadget);
+ Address sourceAddr = addr(dataAddr, sourceProgram);
+ Data sourceData = sourceProgram.getListing().getDataAt(sourceAddr);
+
+ destinationBuilder.setBytes(dataAddr, "4c 6f 61 64 43 75 72 73 6f 72 57 00"); // same bytes
+ destinationBuilder.applyDataType(dataAddr, gadget);
+ Address destinationAddr = addr(dataAddr, destinationProgram);
+ Data destinationData = destinationProgram.getListing().getDataAt(destinationAddr);
+
+ DataTypeValidator validator = new DataTypeValidator(sourceData, destinationData,
+ ReplaceDataChoices.REPLACE_FIRST_DATA_ONLY) {
+ @Override
+ protected void assertApplied() {
+
+ Data appliedData =
+ destinationProgram.getListing().getDataAt(destinationAddr);
+ DataType dt = appliedData.getDataType();
+ assertTrue(dt instanceof Structure);
+ Structure struct = (Structure) dt;
+ assertEquals("Gadget", struct.getName());
+ assertTrue("Struct should have been empty due to options setting",
+ struct.isNotYetDefined());
+ }
+
+ @Override
+ public ToolOptions getOptions() {
+ ToolOptions vtOptions = super.getOptions();
+ vtOptions.setBoolean(VTOptionDefines.USE_EMPTY_COMPOSITES, true);
+ return vtOptions;
+ }
+ };
+ doTestFindAndDoNothingOnApplyOfSameMarkupItem(validator);
+ }
+
+ @Test
+ public void testReplace_EmptyStructureOption_WhereNone() throws Exception {
+
+ // apply a Gadget struct to the source
+ Structure gadget = createGadgetStruct();
+ String dataAddr = "0x01007500";
+ sourceBuilder.setBytes(dataAddr, "4c 6f 61 64 43 75 72 73 6f 72 57 00"); // arbitrary bytes
+ sourceBuilder.applyDataType(dataAddr, gadget);
+ Address sourceAddr = addr(dataAddr, sourceProgram);
+ Data sourceData = sourceProgram.getListing().getDataAt(sourceAddr);
+
+ destinationBuilder.setBytes(dataAddr, "4c 6f 61 64 43 75 72 73 6f 72 57 00"); // same bytes
+ Address destinationAddr = addr(dataAddr, destinationProgram);
+ Data destinationData = destinationProgram.getListing().getDataAt(destinationAddr);
+
+ DataTypeValidator validator = new DataTypeValidator(sourceData, destinationData,
+ ReplaceDataChoices.REPLACE_FIRST_DATA_ONLY) {
+ @Override
+ protected void assertApplied() {
+
+ Data appliedData =
+ destinationProgram.getListing().getDataAt(destinationAddr);
+ DataType dt = appliedData.getDataType();
+ assertTrue(dt instanceof Structure);
+ Structure struct = (Structure) dt;
+ assertEquals("Gadget", struct.getName());
+ assertTrue("Struct should have been empty due to options setting",
+ struct.isNotYetDefined());
+ }
+
+ @Override
+ public ToolOptions getOptions() {
+ ToolOptions vtOptions = super.getOptions();
+ vtOptions.setBoolean(VTOptionDefines.USE_EMPTY_COMPOSITES, true);
+ return vtOptions;
+ }
+ };
+ doTestFindAndApplyMarkupItem(validator);
+ }
+
//==================================================================================================
// Private Methods
//==================================================================================================
+ private Structure createGadgetStruct() {
+
+ Structure gadgetStruct = new StructureDataType("Gadget", 0);
+ PointerDataType charPtr = new PointerDataType(new CharDataType());
+ gadgetStruct.add(charPtr, "name", "");
+ gadgetStruct.add(new IntegerDataType(), "type", "");
+ gadgetStruct.add(new BooleanDataType(), "deployed", "");
+ gadgetStruct.add(new PointerDataType(), "workingOn", "");
+
+ return gadgetStruct;
+
+ }
+
private Data setDataType(Program program, Address address, DataType dataType, int length) {
int txID = program.startTransaction("Change Data Type");
@@ -391,9 +481,9 @@ public class DataTypeMarkupItemTest extends AbstractVTMarkupItemTest {
}
}
- //==================================================================================================
- // Inner Classes
- //==================================================================================================
+//==================================================================================================
+// Inner Classes
+//==================================================================================================
private class DataTypeValidator extends TestDataProviderAndValidator {
diff --git a/Ghidra/Features/VersionTracking/src/test/java/ghidra/feature/vt/api/markupitem/AbstractVTMarkupItemTest.java b/Ghidra/Features/VersionTracking/src/test/java/ghidra/feature/vt/api/markupitem/AbstractVTMarkupItemTest.java
index 2c677ff726..0c6006ed5a 100644
--- a/Ghidra/Features/VersionTracking/src/test/java/ghidra/feature/vt/api/markupitem/AbstractVTMarkupItemTest.java
+++ b/Ghidra/Features/VersionTracking/src/test/java/ghidra/feature/vt/api/markupitem/AbstractVTMarkupItemTest.java
@@ -95,7 +95,7 @@ public abstract class AbstractVTMarkupItemTest extends AbstractGhidraHeadedInteg
builder.createFunction(entry);
}
- private void createData(ProgramBuilder builder) throws Exception {
+ protected void createData(ProgramBuilder builder) throws Exception {
// "LoadCursorW", 00
builder.setBytes("0x010074e6", "4c 6f 61 64 43 75 72 73 6f 72 57 00");
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java
index df0580546f..289c7006e1 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/function/FunctionDB.java
@@ -24,7 +24,6 @@ import javax.help.UnsupportedOperationException;
import db.DBRecord;
import ghidra.program.database.*;
-import ghidra.program.database.data.DataTypeManagerDB;
import ghidra.program.database.external.ExternalManagerDB;
import ghidra.program.database.map.AddressMap;
import ghidra.program.database.symbol.*;
@@ -437,7 +436,7 @@ public class FunctionDB extends DatabaseObject implements Function {
if (storage != null && storage.isUnassignedStorage()) {
storage = null;
}
- long typeId = ((DataTypeManagerDB) program.getDataTypeManager()).getResolvedID(type);
+ long typeId = program.getDataTypeManager().getResolvedID(type);
rec.setLongValue(FunctionAdapter.RETURN_DATA_TYPE_ID_COL, typeId);
rec.setString(FunctionAdapter.RETURN_STORAGE_COL,
storage != null ? storage.getSerializationString() : null);
@@ -1226,7 +1225,7 @@ public class FunctionDB extends DatabaseObject implements Function {
* Resolve a variable's type and storage.
* @param var variable to be resolved
* @param voidOK if true the use of a 0-length {@link VoidDataType} for the specified
- * variable is allowed (i.e., {@link ReturnParameterDB}), else false should be specifed.
+ * variable is allowed (i.e., {@link ReturnParameterDB}), else false should be specified.
* @param useUnassignedStorage if true storage should be set to {@link VariableStorage#UNASSIGNED_STORAGE}
* else an attempt should be made to adjust the storage.
* @return resolved variable
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/symbol/SymbolTable.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/symbol/SymbolTable.java
index ef31e7061d..9bacb8e6f7 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/symbol/SymbolTable.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/symbol/SymbolTable.java
@@ -306,7 +306,8 @@ public interface SymbolTable {
* requirement within their parent namespace.
*
* @param name the name of the namespace to be retrieved
- * @param namespace the parent namespace of the namespace to be retrieved
+ * @param namespace the parent namespace of the namespace to be retrieved; null for the global
+ * namespace
* @return the namespace with the given name in the given parent namespace
* @throws IllegalArgumentException if the given parent namespace is from a different program
* than that of this symbol table