Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2612 copy default return address to prototype if necessary

James175ebc1e4841
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/SpecExtensionTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/SpecExtensionTest.java
index a24f39cc8c..9abcabfe0d 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/SpecExtensionTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/SpecExtensionTest.java
@@ -154,6 +154,8 @@ public class SpecExtensionTest extends AbstractDecompilerTest {
program.endTransaction(id1, true);
PrototypeModel myproto = cspec.getCallingConvention("myproto");
assertNotNull(myproto);
+ assertNotNull(myproto.getReturnAddress());
+ assertArrayEquals(defaultModel.getReturnAddress(), myproto.getReturnAddress());
int id = program.startTransaction("test extension install");
Address addr = program.getAddressFactory().getDefaultAddressSpace().getAddress(0x100112c);
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramCompilerSpec.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramCompilerSpec.java
index c6dddb30ac..c7b011ab64 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramCompilerSpec.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/ProgramCompilerSpec.java
@@ -136,6 +136,7 @@ public class ProgramCompilerSpec extends BasicCompilerSpec {
continue;
}
markPrototypeAsExtension(model);
+ setDefaultReturnAddressIfNeeded(model);
finalList.add(model);
usermodels.put(model.getName(), model);
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/BasicCompilerSpec.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/BasicCompilerSpec.java
index a018cd459d..78487bfba8 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/BasicCompilerSpec.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/BasicCompilerSpec.java
@@ -77,7 +77,7 @@ public class BasicCompilerSpec implements CompilerSpec {
private List<Varnode> preferSplit; // List of registers the decompiler prefers to split
private AddressSet noHighPtr; // Memory regions the decompiler treats as not addressable
private AddressSet readOnlySet; // (Additional) memory ranges the decompiler treats as read-only
- private Varnode returnAddress; // Register/memory where decompiler expects return address to be stored
+ protected Varnode returnAddress; // Register/memory where decompiler expects return address to be stored
private int funcPtrAlign; // Alignment of function pointers, 0=no alignment (default)
private List<Pair<AddressSpace, Integer>> deadCodeDelay;
private List<AddressRange> inferPtrBounds; // Restrictions on where decompiler can infer pointers
@@ -1066,6 +1066,7 @@ public class BasicCompilerSpec implements CompilerSpec {
model = new PrototypeModel();
model.restoreXml(parser, this);
}
+ setDefaultReturnAddressIfNeeded(model);
modelList.add(model);
return model;
}
@@ -1169,6 +1170,19 @@ public class BasicCompilerSpec implements CompilerSpec {
model.isExtension = true;
}
+ /**
+ * Sets the {@code returnaddress} of {@code model} to the {@code returnAddress}
+ * of {@code this} if the model does not have a return address set.
+ * @param model prototype
+ */
+ protected void setDefaultReturnAddressIfNeeded(PrototypeModel model) {
+ if (model.getReturnAddress() == null) {
+ Varnode[] retAddr =
+ (returnAddress == null) ? new Varnode[0] : new Varnode[] { returnAddress };
+ model.setReturnAddress(retAddr);
+ }
+ }
+
@Override
public boolean isEquivalent(CompilerSpec obj) {
if (getClass() != obj.getClass()) {
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java
index 3d8787a46e..52a7dd2c34 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/PrototypeModel.java
@@ -156,9 +156,6 @@ public class PrototypeModel {
* @return list of registers/memory used to store the return address
*/
public Varnode[] getReturnAddress() {
- if (returnaddress == null) {
- returnaddress = new Varnode[0];
- }
return returnaddress;
}
@@ -444,8 +441,7 @@ public class PrototypeModel {
encoder.writeSignedInteger(ATTRIB_STACKSHIFT, stackshift);
GenericCallingConvention nameType = GenericCallingConvention.guessFromName(name);
if (nameType != genericCallingConvention) {
- encoder.writeString(ATTRIB_TYPE,
- genericCallingConvention.getDeclarationName());
+ encoder.writeString(ATTRIB_TYPE, genericCallingConvention.getDeclarationName());
}
if (hasThis) {
encoder.writeBool(ATTRIB_HASTHIS, true);
@@ -794,4 +790,12 @@ public class PrototypeModel {
public String toString() {
return getName();
}
+
+ /**
+ * Set the return address
+ * @param returnaddress return address
+ */
+ protected void setReturnAddress(Varnode[] returnaddress) {
+ this.returnaddress = returnaddress;
+ }
}