Ghidra is a software reverse engineering (SRE) framework
GP-2094 instruction correct context issue impacting delay slot processing
ghidra13bc1989535d8
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.javaindex 31ac8e71be..fc6374f62c 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeManager.java@@ -624,26 +624,9 @@ public class CodeManager implements ErrorHandler, ManagerDB {Register contextReg = contextMgr.getBaseContextRegister();if (contextReg != Register.NO_CONTEXT) {- try {- RegisterValue contextValue = context.getRegisterValue(contextReg);- Address start = address;- if (SystemUtilities.isEqual(contextValue,- contextMgr.getDefaultValue(contextReg, start))) {- contextMgr.setValue(contextReg, start, endAddr, null);- }- else {- // Do not save non-flowing context beyond- RegisterValue ctx = contextValue;- if (contextMgr.hasNonFlowingContext() && !start.equals(endAddr)) {- contextMgr.setRegisterValue(start, start, ctx);- ctx = contextMgr.getFlowValue(ctx);- start = start.next();- }- contextMgr.setRegisterValue(start, endAddr, ctx);- }- }- catch (ContextChangeException e) {- throw new AssertException(e.getMessage()); // Unexpected+ RegisterValue contextValue = context.getRegisterValue(contextReg);+ if (contextValue != null && contextValue.hasAnyValue()) {+ saveInstructionContext(address, endAddr, contextValue);}}@@ -665,6 +648,31 @@ public class CodeManager implements ErrorHandler, ManagerDB {return inst;}+ private void saveInstructionContext(Address address, Address endAddr,+ RegisterValue contextValue) {+ try {+ Address start = address;+ Register contextReg = contextValue.getRegister();+ if (SystemUtilities.isEqual(contextValue,+ contextMgr.getDefaultValue(contextReg, start))) {+ contextMgr.setValue(contextReg, start, endAddr, null);+ }+ else {+ // Do not save non-flowing context beyond+ RegisterValue ctx = contextValue;+ if (contextMgr.hasNonFlowingContext() && !start.equals(endAddr)) {+ contextMgr.setRegisterValue(start, start, ctx);+ ctx = contextMgr.getFlowValue(ctx);+ start = start.next();+ }+ contextMgr.setRegisterValue(start, endAddr, ctx);+ }+ }+ catch (ContextChangeException e) {+ throw new AssertException(e.getMessage()); // Unexpected+ }+ }+RegisterValue getOriginalPrototypeContext(InstructionPrototype prototype,Register baseContextReg) throws NoValueException {return protoMgr.getOriginalPrototypeContext(prototype, baseContextReg);diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.javaindex a9f892555a..c79fcbc919 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/RegisterValueStore.java@@ -123,9 +123,11 @@ public class RegisterValueStore {// Ensure that transaction is open to avoid delayed error conditionrangeMap.checkWritableState();}- rangeWriteCacheValue = newValue;- rangeWriteCacheMin = start;- rangeWriteCacheMax = end;+ synchronized (this) {+ rangeWriteCacheValue = newValue;+ rangeWriteCacheMin = start;+ rangeWriteCacheMax = end;+ }return;}@@ -230,21 +232,27 @@ public class RegisterValueStore {/*** Returns the RegisterValue (value and mask) associated with the given address.+ * @param register register (base or child) for which context value should be returned* @param address the address at which to get the RegisterValue.* @return the RegisterValue*/public RegisterValue getValue(Register register, Address address) {- if (rangeWriteCacheValue != null && address.compareTo(rangeWriteCacheMin) >= 0 &&- address.compareTo(rangeWriteCacheMax) <= 0) {- return rangeWriteCacheValue.getRegisterValue(register);- }-+ RegisterValue value = null;byte[] bytes = rangeMap.getValue(address);- if (bytes == null) {- return null;+ if (bytes != null) {+ value = new RegisterValue(register, bytes);+ }++ synchronized (this) {+ if (rangeWriteCacheValue != null && address.compareTo(rangeWriteCacheMin) >= 0 &&+ address.compareTo(rangeWriteCacheMax) <= 0) {+ value = value != null ? value.combineValues(rangeWriteCacheValue)+ : rangeWriteCacheValue;+ return value.getRegisterValue(register);+ }}- return new RegisterValue(register, bytes);+ return value;}/**