Ghidra is a software reverse engineering (SRE) framework
GP-2704 fix decompiler rendering of array string offcuts
dev7473682cbbc24c98bc
diff --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 adde9960ed..7a6729ca12 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@@ -20,10 +20,9 @@ import static ghidra.program.model.pcode.ElementId.*;import java.io.IOException;import java.math.BigInteger;-import java.nio.charset.Charset;+import java.nio.charset.StandardCharsets;import ghidra.app.cmd.function.CallDepthChangeInfo;-import ghidra.docking.settings.Settings;import ghidra.docking.settings.SettingsImpl;import ghidra.program.disassemble.Disassembler;import ghidra.program.model.address.*;@@ -55,6 +54,15 @@ public class DecompileCallback {public static class StringData {boolean isTruncated; // Did we truncate the stringpublic byte[] byteData; // The UTF8 encoding of the string++ public StringData(String stringVal, int maxChars) {+ this.isTruncated = false;+ if (stringVal.length() > maxChars) {+ this.isTruncated = true;+ stringVal = stringVal.substring(0, maxChars);+ }+ this.byteData = stringVal.getBytes(StandardCharsets.UTF_8);+ }}private DecompileDebug debug;@@ -69,7 +77,6 @@ public class DecompileCallback {private AddressFactory addrfactory;private ConstantPool cpool;private PcodeDataTypeManager dtmanage;- private Charset utf8Charset;private String nativeMessage;private InstructionBlock lastPseudoInstructionBlock;@@ -87,7 +94,6 @@ public class DecompileCallback {cpool = null;nativeMessage = null;debug = null;- utf8Charset = Charset.availableCharsets().get(CharsetInfo.UTF8);}/**@@ -1223,57 +1229,23 @@ public class DecompileCallback {return null;}Data data = program.getListing().getDataContaining(addr);- Settings settings = SettingsImpl.NO_SETTINGS;- AbstractStringDataType dataType = null;- StringDataInstance stringInstance = null;- int length = 0;- if (data != null) {- if (data.getDataType() instanceof AbstractStringDataType) {- // There is already a string here. Use its configuration to- // set up the StringDataInstance- settings = data;- dataType = (AbstractStringDataType) data.getDataType();- length = data.getLength();- if (length <= 0) {- return null;- }- long diff = addr.subtract(data.getAddress()) *- addr.getAddressSpace().getAddressableUnitSize();- if (diff < 0 || diff >= length) {- return null;- }- length -= diff;- MemoryBufferImpl buf = new MemoryBufferImpl(program.getMemory(), addr, 64);- stringInstance = dataType.getStringDataInstance(buf, settings, length);+ StringDataInstance stringInstance = StringDataInstance.getStringDataInstance(data);+ if (stringInstance != StringDataInstance.NULL_INSTANCE) {+ long diff =+ addr.subtract(data.getAddress()) * addr.getAddressSpace().getAddressableUnitSize();+ int length = data.getLength();+ if (length <= 0 || diff < 0 || diff >= length) {+ return null;}+ stringInstance = stringInstance.getByteOffcut((int) diff);}- if (stringInstance == null) {+ else {// There is no string and/or something else at the address.// Setup StringDataInstance based on raw memory- DataType dt = dtmanage.findBaseType(dtName, dtId);- if (dt instanceof AbstractStringDataType) {- dataType = (AbstractStringDataType) dt;- }- else {- if (dt != null) {- int size = dt.getLength();- if (size == 2) {- dataType = TerminatedUnicodeDataType.dataType;- }- else if (size == 4) {- dataType = TerminatedUnicode32DataType.dataType;- }- else {- dataType = TerminatedStringDataType.dataType;- }- }- else {- dataType = TerminatedStringDataType.dataType;- }- }+ AbstractStringDataType dt = coerceToStringDataType(dtmanage.findBaseType(dtName, dtId));MemoryBufferImpl buf = new MemoryBufferImpl(program.getMemory(), addr, 64);- stringInstance = dataType.getStringDataInstance(buf, settings, maxChars);- length = stringInstance.getStringLength();+ stringInstance = dt.getStringDataInstance(buf, SettingsImpl.NO_SETTINGS, maxChars);+ int length = stringInstance.getStringLength();if (length < 0 || length > maxChars) {return null;}@@ -1289,16 +1261,29 @@ public class DecompileCallback {if (!isValidChars(stringVal)) {return null;}- StringData stringData = new StringData();- stringData.isTruncated = false;- if (stringVal.length() > maxChars) {- stringData.isTruncated = true;- stringVal = stringVal.substring(0, maxChars);- }- stringData.byteData = stringVal.getBytes(utf8Charset);+ StringData stringData = new StringData(stringVal, maxChars);if (debug != null) {debug.getStringData(addr, stringData);}return stringData;}++ private AbstractStringDataType coerceToStringDataType(DataType dt) {+ if (dt instanceof AbstractStringDataType asdt) {+ return asdt;+ }++ int size = -1;+ if (dt != null) {+ if (dt instanceof Array arrayDT) {+ dt = arrayDT.getDataType();+ }+ size = dt.getLength();+ }+ return switch (size) {+ default -> TerminatedStringDataType.dataType;+ case 2 -> TerminatedUnicodeDataType.dataType;+ case 4 -> TerminatedUnicode32DataType.dataType;+ };+ }}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.javaindex 81a5c43cca..59cc19a5c6 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java@@ -15,14 +15,15 @@*/package ghidra.program.model.data;-import static ghidra.program.model.data.EndianSettingsDefinition.*;-import static ghidra.program.model.data.RenderUnicodeSettingsDefinition.*;+import static ghidra.program.model.data.EndianSettingsDefinition.ENDIAN;+import static ghidra.program.model.data.RenderUnicodeSettingsDefinition.RENDER;import static ghidra.program.model.data.StringLayoutEnum.*;-import static ghidra.program.model.data.TranslationSettingsDefinition.*;+import static ghidra.program.model.data.TranslationSettingsDefinition.TRANSLATION;++import java.util.*;import java.nio.*;import java.nio.charset.*;-import java.util.*;import generic.stl.Pair;import ghidra.docking.settings.*;@@ -1032,6 +1033,9 @@ public class StringDataInstance {if (isBadCharSize() || isProbe() || !isValidOffcutOffset(byteOffset)) {return NULL_INSTANCE;}+ if (byteOffset == 0) {+ return this;+ }int newLength = Math.max(0, length - byteOffset);StringDataInstance sub = new StringDataInstance(this, getOffcutLayout(),new WrappedMemBuffer(buf, byteOffset), newLength, charsetName);