Ghidra is a software reverse engineering (SRE) framework
GP-2807 Fix potential error creating function/data on existing function/data, minor datatype name fix
emteere8197183de00c
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/blobs/CliAbstractSig.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/blobs/CliAbstractSig.javaindex d4f194ea85..9897ffc86a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/blobs/CliAbstractSig.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/blobs/CliAbstractSig.java@@ -529,10 +529,14 @@ public abstract class CliAbstractSig extends CliBlob implements CliRepresentableprivate int typeSizeBytes;private int genArgCount;private int countSizeBytes;+ private long dataOffset;private List<CliSigType> argTypes = new ArrayList<>();public CliTypeGenericInst(BinaryReader reader, CliElementType typeCode) throws IOException {super(typeCode);++ dataOffset = reader.getPointerIndex();+firstType = CliElementType.fromInt(reader.readNextByte()); // Should be Class or ValueTypelong origIndex = reader.getPointerIndex();encodedType = decodeCompressedUnsignedInt(reader);@@ -600,7 +604,7 @@ public abstract class CliAbstractSig extends CliBlob implements CliRepresentable@Overridepublic DataType getDefinitionDataType() {StructureDataType struct = new StructureDataType(new CategoryPath(PATH),- "GenericInstType" + argTypes.toString(), 0);+ "GenericInstType" + "_" + dataOffset, 0);// TODO: the toString() is included in the above line so GenericInst types can contain other GenericInst's, otherwise this is prohibited by StructureDataTypestruct.add(CliTypeCodeDataType.dataType, "GenericInst", "GenericInst");struct.add(CliTypeCodeDataType.dataType, "ClassOrValueType", "Class or ValueType");@@ -696,11 +700,14 @@ public abstract class CliAbstractSig extends CliBlob implements CliRepresentablepublic class CliTypeSzArray extends CliSigType {private List<CliCustomMod> customMods = new ArrayList<>();private CliSigType type;+ private long dataOffset;public CliTypeSzArray(BinaryReader reader, CliElementType typeCode)throws IOException, InvalidInputException {super(typeCode);+ dataOffset = reader.getPointerIndex();+while (CliCustomMod.isCustomMod(reader)) {customMods.add(new CliCustomMod(reader));}@@ -735,7 +742,7 @@ public abstract class CliAbstractSig extends CliBlob implements CliRepresentable@Overridepublic DataType getDefinitionDataType() {- StructureDataType struct = new StructureDataType(new CategoryPath(PATH), "SzArray", 0);+ StructureDataType struct = new StructureDataType(new CategoryPath(PATH), "SzArray" + "_" + dataOffset, 0);struct.add(CliTypeCodeDataType.dataType, "TypeCode", "SzArray");for (CliCustomMod mod : customMods) {struct.add(mod.getDefinitionDataType());diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/tables/CliTableMethodDef.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/tables/CliTableMethodDef.javaindex 391f644a01..16fc02f907 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/tables/CliTableMethodDef.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/cli/tables/CliTableMethodDef.java@@ -433,25 +433,20 @@ public class CliTableMethodDef extends CliAbstractTable {}}+ FunctionManager funcMgr = program.getFunctionManager();+try {- Function newFunc = program.getFunctionManager()- .createFunction(funcName, startAddr, funcAddrSet, SourceType.ANALYSIS);- newFunc.setReturnType(returnType, SourceType.ANALYSIS);- newFunc.updateFunction(null, null, FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS,+ // Create the function, if already created, update the existing function+ Function func = funcMgr.getFunctionAt(startAddr);+ if (func == null) {+ func = funcMgr+ .createFunction(funcName, startAddr, funcAddrSet, SourceType.ANALYSIS);+ }+ func.setReturnType(returnType, SourceType.ANALYSIS);+ func.updateFunction(null, null, FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS,true, SourceType.ANALYSIS, parameters);- // If Code here is not of the correct type, create a data element to stop disassembly, and comment- PrototypeModel cliCallingConvention = program.getLanguage()- .getDefaultCompilerSpec()- .getCallingConvention(CLITABLEMETHODDEF_CLRCALL_CONVENTION);-- if ((cliCallingConvention == null && !methodRow.isNative()) ||- (cliCallingConvention != null && methodRow.isNative())) {- Data data = program.getListing()- .createData(startAddr, new ArrayDataType(BYTE, (int) endAddr.subtract(startAddr) + 1, 1));- data.setComment(CodeUnit.PRE_COMMENT,- (methodRow.isManaged() ? ".NET CLR Managed Code" : "Native Code"));- }+ markToPreventIncorrectProcessorDisassembly(program, methodRow, startAddr, endAddr);}catch (CodeUnitInsertionException e) {// Ignore, something there already@@ -468,8 +463,8 @@ public class CliTableMethodDef extends CliAbstractTable {String err = "Error processing function \" (\" + methodRowIndex + \")" + funcName +"\": Overlapping function (" + startAddr + ", " + endAddr + ": ";- Function existingFuncA = program.getFunctionManager().getFunctionAt(startAddr);- Function existingFuncB = program.getFunctionManager().getFunctionAt(endAddr);+ Function existingFuncA = funcMgr.getFunctionContaining(startAddr);+ Function existingFuncB = funcMgr.getFunctionContaining(endAddr);if (existingFuncA != null && existingFuncB == null) {err = err + existingFuncA.getName();@@ -497,6 +492,42 @@ public class CliTableMethodDef extends CliAbstractTable {}}+ /**+ *+ * For .Net code in programs loaded as x86, protect CLI code from disassembly.+ * For x86 code in programs loaded as CLI, protect x86 code from disassembly.+ *+ * @param program program either loaded as x86 or CLI+ * @param methodRow CliMethodTable row+ * @param startAddr start address of the function+ * @param endAddr end address of the function+ * @throws CodeUnitInsertionException couldn't create dagta.+ */+ private void markToPreventIncorrectProcessorDisassembly(Program program, CliMethodDefRow methodRow,+ Address startAddr, Address endAddr) throws CodeUnitInsertionException {+++ PrototypeModel cliCallingConvention = program.getLanguage()+ .getDefaultCompilerSpec()+ .getCallingConvention(CLITABLEMETHODDEF_CLRCALL_CONVENTION);++ // If Code here is not of the correct type, create a data array of bytes to stop disassembly+ if ((cliCallingConvention == null && !methodRow.isNative()) ||+ (cliCallingConvention != null && methodRow.isNative())) {+ Listing listing = program.getListing();+ Data data = listing.getDefinedDataAt(startAddr);+ if (data == null) {+ int codeLength = (int) endAddr.subtract(startAddr) + 1;+ ArrayDataType codeDT = new ArrayDataType(BYTE, codeLength, 1);+ data = listing.createData(startAddr, codeDT);++ // comment the type of code that should appear here+ data.setComment(CodeUnit.PRE_COMMENT,+ (methodRow.isManaged() ? ".NET CLR Managed Code" : "Native Code"));+ }+ }+ }+@Overridepublic StructureDataType getRowDataType() {StructureDataType rowDt = new StructureDataType(new CategoryPath(PATH), "MethodDef Row", 0);