Ghidra is a software reverse engineering (SRE) framework
GP-7411 updated RTTI analyzer to correctly handle duplicate classes and _ptr64 type descriptors.
ghidra0071b24e0bafe80
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/CreateTypeDescriptorBackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/CreateTypeDescriptorBackgroundCmd.javaindex 99b1d35efe..bffe486c34 100644--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/CreateTypeDescriptorBackgroundCmd.java+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/CreateTypeDescriptorBackgroundCmd.java@@ -15,17 +15,15 @@*/package ghidra.app.cmd.data;-import ghidra.app.cmd.data.rtti.RttiUtil;import ghidra.app.util.datatype.microsoft.DataApplyOptions;import ghidra.app.util.datatype.microsoft.DataValidationOptions;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.Namespace;-import ghidra.program.model.symbol.SourceType;import ghidra.program.model.util.CodeUnitInsertionException;-import ghidra.util.Msg;-import ghidra.util.exception.*;+import ghidra.util.exception.CancelledException;+import ghidra.util.exception.InvalidInputException;/*** This command will create a TypeDescriptor data type. Since unsized arrays are not properly@@ -140,6 +138,12 @@ public class CreateTypeDescriptorBackgroundCmd}String prefix = demangledName + " ";+ // This is to make the type descriptor ptr have a different symbol than the type descriptor+ String labelPrefix = "";+ if (demangledName.endsWith("__ptr64")) {+ labelPrefix = "__ptr64_";+ }+// Plate CommentEHDataTypeUtilities.createPlateCommentIfNeeded(program, prefix, RTTI_0_NAME, null,getDataAddress(), applyOptions);@@ -148,42 +152,13 @@ public class CreateTypeDescriptorBackgroundCmd// LabelNamespace classNamespace = model.getDescriptorAsNamespace();-- // if cannot demangle then use the mangled name as the namespace- if (classNamespace == null || classNamespace.isGlobal()) {- Msg.error(RttiUtil.class, "Cannot get demangled namespace from model " +- model.getAddress() + " so will use the mangled name for the namespace");- try {- classNamespace = program.getSymbolTable()- .getOrCreateNameSpace(program.getGlobalNamespace(), demangledName,- SourceType.IMPORTED);- }- catch (DuplicateNameException e) {- // ok if it is duplicate as it was likely created in another rtti handling method- }- catch (InvalidInputException e) {- Msg.error(TypeDescriptorModel.class,- "Failed to create mangled namespace: " + e.getMessage());- classNamespace = null;- return false;- }-- }-- // If PDB had been run, then the namespace here might already have been promoted to- // a class type. At this point in processing, we know that the model only has a type- // with a "class" or "struct" tag (see TypeDescriptorModel).- // <br>Note: For now this assumes all classes and structs with RTTI data must- // actually be classes. In the future this might need additional checking before- // promoting some "struct" ref types to being a class, if we can better determine- // whether or not they are actually classes.- if (!(classNamespace instanceof GhidraClass)) {- classNamespace = RttiUtil.promoteToClassNamespace(program, classNamespace);+ if (classNamespace == null) {+ return false;}// Make the symbol even if the namespace couldn't be promoted// the method to promote spits out debug error if it cannot be promoted- EHDataTypeUtilities.createSymbolIfNeeded(program, classNamespace, RTTI_0_NAME,+ EHDataTypeUtilities.createSymbolIfNeeded(program, classNamespace, labelPrefix + RTTI_0_NAME,getDataAddress(), applyOptions);return true;diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/TypeDescriptorModel.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/TypeDescriptorModel.javaindex 75858b5b65..6b2fb4c6cf 100644--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/TypeDescriptorModel.java+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/TypeDescriptorModel.java@@ -15,6 +15,8 @@*/package ghidra.app.cmd.data;+import java.util.List;+import ghidra.app.cmd.data.rtti.RttiUtil;import ghidra.app.util.datatype.microsoft.DataValidationOptions;import ghidra.app.util.datatype.microsoft.MSDataTypeUtils;@@ -24,6 +26,7 @@ import ghidra.docking.settings.SettingsImpl;import ghidra.program.model.address.*;import ghidra.program.model.data.*;import ghidra.program.model.lang.UndefinedValueException;+import ghidra.program.model.listing.GhidraClass;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.DumbMemBufferImpl;import ghidra.program.model.mem.Memory;@@ -69,6 +72,15 @@ public class TypeDescriptorModel extends AbstractCreateDataTypeModel {hasVFPointer = hasVFPointer(program);}+ // this is to handle the duplicate class case so that when re-validating a duplicate class+ // the correct namespace is passed in instead of creating/using the default+ public TypeDescriptorModel(Program program, Address address, Namespace namespace,+ DataValidationOptions validationOptions) {+ super(program, 1, address, validationOptions);+ hasVFPointer = hasVFPointer(program);+ this.namespace = namespace;+ }+@Overridepublic String getName() {return DATA_TYPE_NAME;@@ -597,29 +609,56 @@ public class TypeDescriptorModel extends AbstractCreateDataTypeModel {if (hasComplexType() && demangledDataType == null) {return null;}+Program program = getProgram();namespace = DemangledObject.createNamespace(program, demangledDataType,program.getGlobalNamespace(), false);// if for some reason the mangled name can't be demangled, use the mangled nameif (namespace.isGlobal()) {- try {- namespace = program.getSymbolTable()- .getOrCreateNameSpace(program.getGlobalNamespace(), originalTypeName,- SourceType.IMPORTED);- }- catch (DuplicateNameException e) {- // ok if it is duplicate as it was likely created in another rtti handling method- }- catch (InvalidInputException e) {- Msg.error(TypeDescriptorModel.class,- "Failed to create namespace: " + e.getMessage());- namespace = null;- }+ namespace = getOrCreateClassNamespace(program, program.getGlobalNamespace(),+ originalTypeName, SourceType.IMPORTED);+ }++ if (!(namespace instanceof GhidraClass)) {+ namespace = RttiUtil.promoteToClassNamespace(program, namespace);}++ // figure out if there is already another namespace with this name containing+ // an RTTI_Type_Descriptor and if so, create unique namespace+ List<Symbol> existingTypeDescriptor =+ program.getSymbolTable().getSymbols("RTTI_Type_Descriptor", namespace);+ if (existingTypeDescriptor.isEmpty()) {+ return namespace;+ }++ // make a new class name for the current type descriptor+ namespace = getOrCreateClassNamespace(program, namespace.getParentNamespace(),+ namespace.getName() + "_" + this.getAddress().toString(),+ SourceType.IMPORTED);+return namespace;}+ private Namespace getOrCreateClassNamespace(Program program, Namespace parentNamespace,+ String namespaceName, SourceType sourceType) {++ Namespace newNamespace = null;+ try {+ newNamespace = program.getSymbolTable()+ .getOrCreateNameSpace(parentNamespace, namespaceName, sourceType);+ }+ catch (DuplicateNameException e) {+ // won't ever get here since above is getting existing namespace or creating new one+ // of the given name+ }+ catch (InvalidInputException e) {+ Msg.error(TypeDescriptorModel.class,+ "Failed to create mangled namespace: " + e.getMessage());+ }+ return newNamespace;+ }+private boolean isNamespaceDeleted(Namespace other) {Symbol nsSymbol = other.getSymbol();if (nsSymbol == null) {diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti1Model.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti1Model.javaindex 81226812d5..d177c61398 100644--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti1Model.java+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti1Model.java@@ -27,6 +27,7 @@ import ghidra.program.model.address.AddressOutOfBoundsException;import ghidra.program.model.data.*;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.*;+import ghidra.program.model.symbol.Symbol;/***/@@ -122,7 +123,14 @@ public class Rtti1Model extends AbstractCreateRttiDataModel {if (rtti0Address == null) {invalid("Invalid TypeDescriptor reference (pTypeDescriptor)."); // throws Exception}- rtti0Model = new TypeDescriptorModel(program, rtti0Address, validationOptions);+ Symbol rtti0Symbol = program.getSymbolTable().getPrimarySymbol(rtti0Address);+ if (rtti0Symbol != null && rtti0Symbol.getName().equals("RTTI_Type_Descriptor")) {+ rtti0Model = new TypeDescriptorModel(program, rtti0Address,+ rtti0Symbol.getParentNamespace(), validationOptions);+ }+ else {+ rtti0Model = new TypeDescriptorModel(program, rtti0Address, validationOptions);+ }if (validateReferredToData) {try {rtti0Model.validate();diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti4Model.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti4Model.javaindex be38b072d1..7dca81f15a 100644--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti4Model.java+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/Rtti4Model.java@@ -26,6 +26,7 @@ import ghidra.app.util.datatype.microsoft.MSDataTypeUtils;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;import ghidra.program.model.listing.Program;+import ghidra.program.model.symbol.Symbol;/*** Model for run-time type information about the RTTI4 data type, which represents a@@ -104,7 +105,15 @@ public class Rtti4Model extends AbstractCreateRttiDataModel {throw new InvalidDataTypeException(getName() + " data type at " + getAddress() +" doesn't refer to a valid location " + rtti0Address + " for the Type Descriptor.");}- rtti0Model = new TypeDescriptorModel(program, rtti0Address, validationOptions);++ Symbol rtti0Symbol = program.getSymbolTable().getPrimarySymbol(rtti0Address);+ if (rtti0Symbol != null && rtti0Symbol.getName().equals("RTTI_Type_Descriptor")) {+ rtti0Model = new TypeDescriptorModel(program, rtti0Address,+ rtti0Symbol.getParentNamespace(), validationOptions);+ }+ else {+ rtti0Model = new TypeDescriptorModel(program, rtti0Address, validationOptions);+ }if (validateReferredToData) {rtti0Model.validate();}