Ghidra is a software reverse engineering (SRE) framework
GP-7351 Catch AddressOutOfBoundsException
caheckmand1acbc06f260
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.ccindex ac7d261054..10f5da088a 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/architecture.cc@@ -1454,7 +1454,7 @@ Address SegmentedResolver::resolve(uintb val,int4 sz,const Address &point,uintb// In this case the address offset is not fully specified// we check if the rest is stored in a context variable// (as with near pointers)- if (segop->getResolve().space != (AddrSpace *)0) {+ if (segop->getResolve().space != (AddrSpace *)0 && !point.isInvalid()) {uintb base = glb->context->getTrackedValue(segop->getResolve(),point);fullEncoding = (base << 8 * innersz) + (val & calc_mask(innersz));vector<uintb> seginput;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex aea3206de7..4642049149 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -1215,7 +1215,7 @@ int4 ActionConstantPtr::apply(Funcdata &data)if (opc == CPUI_INT_ADD) {if (op->getIn(1-slot)->isSpacebase()) continue; // Make sure other side is not a spacebase already}- else if ((opc == CPUI_PTRSUB)||(opc==CPUI_PTRADD))+ else if (opc == CPUI_PTRSUB || opc==CPUI_PTRADD || opc == CPUI_SEGMENTOP)continue;Address rampoint;uintb fullEncoding;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex c8f6e49467..5aa8f4f09d 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -8961,7 +8961,7 @@ int4 RuleSegment::applyOp(PcodeOp *op,Funcdata &data)data.opSetOpcode(op,CPUI_COPY);return 1;}- else if (segdef->hasFarPointerSupport()) {+ else if (segdef->hasFarPointerSupport() && segdef->isConcatenation()) {// If the hi and lo pieces come from a contigouous sourceif (!contiguous_test(vn1,vn2)) return 0;Varnode *whole = findContiguousWhole(data,vn1,vn2);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/userop.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/userop.ccindex c9074bfa6c..43bcd87b74 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/userop.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/userop.cc@@ -183,9 +183,32 @@ int4 VolatileWriteOp::extractAnnotationSize(const Varnode *vn,const PcodeOp *op)SegmentOp::SegmentOp(const string &nm,Architecture *g,int4 ind): TermPatternOp(nm,g,segment,ind){+ spc = (AddrSpace *)0;+ baseinsize = 0;+ innerinsize = 0;+ supportsfarpointer = false;+ injectId = -1;+ concatState = -1;constresolve.space = (AddrSpace *)0;}+/// Test if this op can be treated like a CPUI_PIECE+/// \return \b true if the op concatenates its inputs+bool SegmentOp::isConcatenation(void) const++{+ if (concatState >= 0)+ return (concatState == 1);+ uintb high = 0xabababababababab & calc_mask(baseinsize);+ uintb low = 0x1234567812345678 & calc_mask(innerinsize);+ uintb whole = (high << innerinsize * 8) | low;+ vector<uintb> in;+ in.push_back(high);+ in.push_back(low);+ concatState = (whole == execute(in)) ? 1 : 0;+ return (concatState == 1);+}+bool SegmentOp::unify(Funcdata &data,PcodeOp *op,vector<Varnode *> &bindlist) const{@@ -231,6 +254,7 @@ void SegmentOp::decode(Decoder &decoder)baseinsize = 0;innerinsize = 0;supportsfarpointer = false;+ concatState = -1;name = "segment"; // Default name, might be overridden by userop attributefor(;;) {uint4 attribId = decoder.getNextAttributeId();diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/userop.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/userop.hhindex fda329a171..a6bb31c6eb 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/userop.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/userop.hh@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -266,12 +266,14 @@ class SegmentOp : public TermPatternOp {int4 injectId; ///< Id of InjectPayload that emulates \b this operationint4 baseinsize; ///< The size in bytes of the \e base or \e segment valueint4 innerinsize; ///< The size in bytes of the \e near pointer value+ mutable int4 concatState; ///< 0 = not a concatenation, 1 = is a concatenation, -1 = not tested yetbool supportsfarpointer; ///< Is \b true if the joined pair base:near acts as a \b far pointerVarnodeData constresolve; ///< How to resolve constant near pointerspublic:SegmentOp(const string &nm,Architecture *g,int4 ind); ///< ConstructorAddrSpace *getSpace(void) const { return spc; } ///< Get the address space being pointed tobool hasFarPointerSupport(void) const { return supportsfarpointer; } ///< Return \b true, if \b this op supports far pointers+ bool isConcatenation(void) const; ///< Return \b true if \b this is a concatenationint4 getBaseSize(void) const { return baseinsize; } ///< Get size in bytes of the base/segment valueint4 getInnerSize(void) const { return innerinsize; } ///< Get size in bytes of the near valueconst VarnodeData &getResolve(void) const { return constresolve; } ///< Get the default register for resolving indirect segmentsdiff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.javaindex 0d6ccdf3b5..fc4e4df4e4 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java@@ -783,6 +783,9 @@ public class DecompileCallback {* @throws IOException for errors in the underlying stream writing the result*/public void getTrackedRegisters(Address addr, Encoder resultEncoder) throws IOException {+ if (addr == Address.NO_ADDRESS) {+ return;+ }ProgramContext context = program.getProgramContext();encodeTrackedPointSet(resultEncoder, addr, context);diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.javaindex 897be374e7..16bd238350 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/AddressXML.java@@ -21,8 +21,7 @@ import static ghidra.program.model.pcode.ElementId.*;import java.io.IOException;import java.util.ArrayList;-import ghidra.program.model.address.Address;-import ghidra.program.model.address.AddressSpace;+import ghidra.program.model.address.*;import ghidra.program.model.lang.*;import ghidra.program.model.listing.Program;import ghidra.program.model.listing.VariableStorage;@@ -476,11 +475,15 @@ public class AddressXML {}}decoder.closeElement(el);- if (spc == null) {- // EXTERNAL_SPACE is currently a placeholder for an unsupported decompiler address space- return Address.NO_ADDRESS;+ if (spc != null) {+ try {+ return spc.getAddress(offset);+ }+ catch (AddressOutOfBoundsException ex) {+ // Not a valid address+ }}- return spc.getAddress(offset);+ return Address.NO_ADDRESS;}/**