Ghidra is a software reverse engineering (SRE) framework
GP-13 - MDMang castoperator review fixes
ghizard0bf7d614edb3
diff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/MDMangGhidra.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/MDMangGhidra.javaindex 730e9f927a..7e58328a08 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/MDMangGhidra.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/MDMangGhidra.java@@ -93,8 +93,7 @@ public class MDMangGhidra extends MDMang {newType = new DemangledType(subMangled, demangledSource, qual.toString());}else {- newType =- new DemangledType(mangledSource, demangledSource, qual.toString());+ newType = new DemangledType(mangledSource, demangledSource, qual.toString());}parentType.setNamespace(newType);parentType = newType;@@ -187,8 +186,8 @@ public class MDMangGhidra extends MDMang {variable = new DemangledVariable(mangledSource, demangledSource, "");}else {- variable = new DemangledVariable(mangledSource, demangledSource,- objectCPP.getName());+ variable =+ new DemangledVariable(mangledSource, demangledSource, objectCPP.getName());variable.setNamespace(processNamespace(objectCPP.getQualfication()));}variable.setDatatype(dt);@@ -351,12 +350,14 @@ public class MDMangGhidra extends MDMang {convention = CompilerSpec.CALLING_CONVENTION_thiscall;}function.setCallingConvention(convention);- MDDataType retType = functionType.getReturnType();- if ((retType != null) && !retType.toString().isEmpty()) {- function.setReturnType(processDataType(null, retType));+ if (functionType.hasReturn()) {+ MDDataType retType = functionType.getReturnType();+ if (!retType.toString().isEmpty()) {+ function.setReturnType(processDataType(null, retType));+ }}- MDArgumentsList args = functionType.getArgumentsList();- if (args != null) {+ if (functionType.hasArgs()) {+ MDArgumentsList args = functionType.getArgumentsList();for (int index = 0; index < args.getNumArgs(); index++) {function.addParameter(processDataType(null, args.getArg(index)));}@@ -413,10 +414,14 @@ public class MDMangGhidra extends MDMang {MDFunctionType functionType = (MDFunctionType) pointerType.getReferencedType();functionPointer.setCallingConvention(functionType.getCallingConvention().toString());functionPointer.setModifier(pointerType.getCVMod().toString());- functionPointer.setReturnType(processDataType(null, functionType.getReturnType()));- MDArgumentsList args = functionType.getArgumentsList();- for (int index = 0; index < args.getNumArgs(); index++) {- functionPointer.addParameter(processDataType(null, args.getArg(index)));+ if (functionType.hasReturn()) {+ functionPointer.setReturnType(processDataType(null, functionType.getReturnType()));+ }+ if (functionType.hasArgs()) {+ MDArgumentsList args = functionType.getArgumentsList();+ for (int index = 0; index < args.getNumArgs(); index++) {+ functionPointer.addParameter(processDataType(null, args.getArg(index)));+ }}MDCVMod thisPointerCVMod = functionType.getThisPointerCVMod();if (thisPointerCVMod != null) {@@ -449,10 +454,14 @@ public class MDMangGhidra extends MDMang {MDFunctionType functionType = (MDFunctionType) refType.getReferencedType();functionReference.setCallingConvention(functionType.getCallingConvention().toString());functionReference.setModifier(refType.getCVMod().toString());- functionReference.setReturnType(processDataType(null, functionType.getReturnType()));- MDArgumentsList args = functionType.getArgumentsList();- for (int index = 0; index < args.getNumArgs(); index++) {- functionReference.addParameter(processDataType(null, args.getArg(index)));+ if (functionType.hasReturn()) {+ functionReference.setReturnType(processDataType(null, functionType.getReturnType()));+ }+ if (functionType.hasArgs()) {+ MDArgumentsList args = functionType.getArgumentsList();+ for (int index = 0; index < args.getNumArgs(); index++) {+ functionReference.addParameter(processDataType(null, args.getArg(index)));+ }}// TODO: fill in lots of functionReference.____ itemsreturn functionReference;@@ -466,10 +475,14 @@ public class MDMangGhidra extends MDMang {functionDefinition.setCallingConvention(functionType.getCallingConvention().toString());functionDefinition.setModifier(functionIndirectType.getCVMod().toString());functionDefinition.incrementPointerLevels();- functionDefinition.setReturnType(processDataType(null, functionType.getReturnType()));- MDArgumentsList args = functionType.getArgumentsList();- for (int index = 0; index < args.getNumArgs(); index++) {- functionDefinition.addParameter(processDataType(null, args.getArg(index)));+ if (functionType.hasReturn()) {+ functionDefinition.setReturnType(processDataType(null, functionType.getReturnType()));+ }+ if (functionType.hasArgs()) {+ MDArgumentsList args = functionType.getArgumentsList();+ for (int index = 0; index < args.getNumArgs(); index++) {+ functionDefinition.addParameter(processDataType(null, args.getArg(index)));+ }}// TODO: fill in lots of functionIndirect.____ itemsreturn functionDefinition;@@ -486,10 +499,14 @@ public class MDMangGhidra extends MDMang {functionDefinition.setCallingConvention(functionType.getCallingConvention().toString());functionDefinition.setModifier(modifierType.getCVMod().toString());functionDefinition.incrementPointerLevels();- functionDefinition.setReturnType(processDataType(null, functionType.getReturnType()));- MDArgumentsList args = functionType.getArgumentsList();- for (int index = 0; index < args.getNumArgs(); index++) {- functionDefinition.addParameter(processDataType(null, args.getArg(index)));+ if (functionType.hasReturn()) {+ functionDefinition.setReturnType(processDataType(null, functionType.getReturnType()));+ }+ if (functionType.hasArgs()) {+ MDArgumentsList args = functionType.getArgumentsList();+ for (int index = 0; index < args.getNumArgs(); index++) {+ functionDefinition.addParameter(processDataType(null, args.getArg(index)));+ }}// TODO: fill in lots of functionIndirect.____ itemsreturn functionDefinition;diff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/functiontype/MDFunctionType.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/functiontype/MDFunctionType.javaindex bc15fa57a8..25178c965f 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/functiontype/MDFunctionType.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/functiontype/MDFunctionType.java@@ -31,8 +31,8 @@ public class MDFunctionType extends MDType {private MDCVMod thisPointerCVMod;private MDThrowAttribute throwAttribute;private boolean hasCVModifier = false;- private boolean hasReturn = true;- private boolean hasArgs = true;+ private boolean hasReturn;+ private boolean hasArgs;private boolean isTypeCast;protected boolean fromModifier = false;@@ -43,7 +43,13 @@ public class MDFunctionType extends MDType {}public MDFunctionType(MDMang dmang) {+ this(dmang, true, true);+ }++ public MDFunctionType(MDMang dmang, boolean hasArgs, boolean hasReturn) {super(dmang);+ this.hasArgs = hasArgs;+ this.hasReturn = hasReturn;}public MDCallingConvention getCallingConvention() {@@ -78,14 +84,6 @@ public class MDFunctionType extends MDType {hasCVModifier = true;}- public void clearHasCVModifier() {- hasCVModifier = false;- }-- public void setNoReturn() {- hasReturn = false;- }-public boolean hasReturn() {return hasReturn;}@@ -94,10 +92,6 @@ public class MDFunctionType extends MDType {return hasArgs;}- public void setNoArgs() {- hasArgs = false;- }-public boolean isTypeCast() {return isTypeCast;}@@ -135,8 +129,8 @@ public class MDFunctionType extends MDType {@Overridepublic void insert(StringBuilder builder) {super.insert(builder);- if ((builder.length() != 0) && (builder.charAt(0) != ' ') && (builder.charAt(0) != '*') &&- (builder.charAt(0) != '&') && (builder.charAt(0) != '(')) {+ String badChars = "*&( ";+ if (builder.length() != 0 && badChars.indexOf(builder.charAt(0)) == -1) {dmang.insertString(builder, " ");}// Separate conventionBuilder with insertion of MDBasedType is used here to reflect MSFTdiff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDBasicName.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDBasicName.javaindex 530e23d470..30dc921bef 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDBasicName.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDBasicName.java@@ -15,6 +15,7 @@*/package mdemangler.naming;+import ghidra.util.Msg;import mdemangler.*;import mdemangler.object.MDObjectCPP;import mdemangler.template.MDTemplateNameAndArguments;@@ -25,7 +26,7 @@ import mdemangler.template.MDTemplateNameAndArguments;*/public class MDBasicName extends MDParsableItem {MDSpecialName specialName;- MDTemplateNameAndArguments tn;+ MDTemplateNameAndArguments templateNameAndArguments;MDReusableName reusableName;MDObjectCPP embeddedObject;MDQualification embeddedObjectQualification;@@ -43,8 +44,8 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {return specialName.isConstructor();}- else if (tn != null) {- return tn.isConstructor();+ if (templateNameAndArguments != null) {+ return templateNameAndArguments.isConstructor();}return false;}@@ -53,8 +54,8 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {return specialName.isDestructor();}- else if (tn != null) {- return tn.isDestructor();+ if (templateNameAndArguments != null) {+ return templateNameAndArguments.isDestructor();}return false;}@@ -63,8 +64,8 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {return specialName.isTypeCast();}- if (tn != null) {- return tn.isTypeCast();+ if (templateNameAndArguments != null) {+ return templateNameAndArguments.isTypeCast();}return false;}@@ -98,10 +99,10 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {return specialName.getName();}- else if (tn != null) {- return tn.getName();+ if (templateNameAndArguments != null) {+ return templateNameAndArguments.getName();}- else if (reusableName != null) {+ if (reusableName != null) {return reusableName.getName();}return "";@@ -122,8 +123,11 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {specialName.setName(name);}- else if (tn != null) {- tn.setName(name);+ else if (templateNameAndArguments != null) {+ templateNameAndArguments.setName(name);+ }+ else {+ Msg.warn(this, "name cannot be set");}}@@ -138,8 +142,11 @@ public class MDBasicName extends MDParsableItem {if (specialName != null) {specialName.setCastTypeString(castTypeString);}- else if (tn != null) {- tn.setCastTypeString(castTypeString);+ else if (templateNameAndArguments != null) {+ templateNameAndArguments.setCastTypeString(castTypeString);+ }+ else {+ Msg.warn(this, "castTypeString cannot be set");}}@@ -155,7 +162,7 @@ public class MDBasicName extends MDParsableItem {embeddedObject.insert(builder);}else {- tn.insert(builder);+ templateNameAndArguments.insert(builder);}if (nameModifier != null) {builder.append(nameModifier);@@ -167,8 +174,8 @@ public class MDBasicName extends MDParsableItem {// First pass can only have name fragment of special nameif (dmang.peek() == '?') {if (dmang.peek(1) == '$') {- tn = new MDTemplateNameAndArguments(dmang);- tn.parse();+ templateNameAndArguments = new MDTemplateNameAndArguments(dmang);+ templateNameAndArguments.parse();}else if (dmang.peek(1) == '?') {// Seems to only hit here for second '?' of "???" sequence.diff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDQualifiedBasicName.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDQualifiedBasicName.javaindex 7dbd110f5a..77a7a59663 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDQualifiedBasicName.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/naming/MDQualifiedBasicName.java@@ -37,16 +37,10 @@ public class MDQualifiedBasicName extends MDParsableItem {}public boolean isConstructor() {- if (basicName == null) {- return false;- }return basicName.isConstructor();}public boolean isDestructor() {- if (basicName == null) {- return false;- }return basicName.isDestructor();}@@ -55,10 +49,7 @@ public class MDQualifiedBasicName extends MDParsableItem {* @return int RTTI number:{0-4, or -1 if not an RTTI}*/public int getRTTINumber() {- if (basicName != null) {- return basicName.getRTTINumber();- }- return -1;+ return basicName.getRTTINumber();}/**@@ -66,9 +57,6 @@ public class MDQualifiedBasicName extends MDParsableItem {* @return {@code true} if Basic Name is of {@link MDString} type.*/public boolean isString() {- if (basicName == null) {- return false;- }return basicName.isString();}@@ -94,9 +82,6 @@ public class MDQualifiedBasicName extends MDParsableItem {}public void setCastTypeString(String castTypeString) {- if (basicName == null) {- return;- }basicName.setCastTypeString(castTypeString);}diff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/object/MDObjectCPP.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/object/MDObjectCPP.javaindex 8190bfac9b..bc8722db9a 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/object/MDObjectCPP.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/object/MDObjectCPP.java@@ -15,6 +15,7 @@*/package mdemangler.object;+import ghidra.util.Msg;import mdemangler.*;import mdemangler.functiontype.MDFunctionType;import mdemangler.naming.*;@@ -143,15 +144,27 @@ public class MDObjectCPP extends MDObject {if (!typeInfo.getNameModifier().isEmpty()) {qualifiedName.setNameModifier(typeInfo.getNameModifier());}- if (qualifiedName.isTypeCast() && typeInfo.getMDType() instanceof MDFunctionType &&- ((MDFunctionType) typeInfo.getMDType()).hasReturn()) {- qualifiedName.setCastTypeString(- ((MDFunctionType) typeInfo.getMDType()).getReturnType().toString());+ if (qualifiedName.isTypeCast()) {+ applyFunctionReturnTypeToTypeCastOperatorName();}}}}+ private void applyFunctionReturnTypeToTypeCastOperatorName() {+ // Make sure there is a function with a return type+ if (!(typeInfo.getMDType() instanceof MDFunctionType)) {+ Msg.warn(this, "Cannot get function return type from non-function");+ return;+ }+ MDFunctionType functionType = (MDFunctionType) typeInfo.getMDType();+ if (functionType.hasReturn()) {+ Msg.warn(this, "No return type available to set to cast operator name");+ return;+ }+ qualifiedName.setCastTypeString(functionType.getReturnType().toString());+ }+/*** Processes the hashed object in the normal fashion.* @throws MDException On parsing error.diff --git a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/typeinfo/MDVCall.java b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/typeinfo/MDVCall.javaindex 91e711a2ea..28c2ec61c4 100644--- a/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/typeinfo/MDVCall.java+++ b/Ghidra/Features/MicrosoftDmang/src/main/java/mdemangler/typeinfo/MDVCall.java@@ -49,13 +49,11 @@ public class MDVCall extends MDMemberFunctionInfo {public MDVCall(MDMang dmang) {super(dmang);- MDFunctionType functionType = new MDFunctionType(dmang);+ MDFunctionType functionType = new MDFunctionType(dmang, false, false);mdtype = functionType;// TODO: consider what to do... from what I understand, this is// also "virtual" but "virtual" does not get printed.setThunk();- functionType.setNoReturn();- functionType.setNoArgs();callIndex = new MDEncodedNumber(dmang);}