Ghidra is a software reverse engineering (SRE) framework
GP-6871 fix issue when searching for unindexed GoTypes
dev747368ed77fbc07b16
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoTypeManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoTypeManager.javaindex df05ea1c69..f110559c78 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoTypeManager.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoTypeManager.java@@ -119,7 +119,14 @@ public class GoTypeManager {GoType goType = getTypeUnchecked(typeAddr);if (goType != null) {- goType.discoverGoTypes(discoveredTypes);+ try {+ goType.discoverGoTypes(discoveredTypes);+ }+ catch (IOException e) {+ Msg.warn(this, "Failed to recurse into Go type %s"+ .formatted(goType.getStructureContext().getStructureAddress()),+ e);+ }}else {Msg.warn(this, "Failed to read Go type at " + typeAddr);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoArrayType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoArrayType.javaindex aa2a9b7938..f637c8778f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoArrayType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoArrayType.java@@ -130,7 +130,9 @@ public class GoArrayType extends GoType {protected String getTypeDeclString() throws IOException {// type CustomArraytype [elementcount]elementTypeString selfName = typ.getName();- String elemName = getElement().getName();+ GoType elementType = getElement();+ String elemName =+ elementType != null ? elementType.getName() : "<missing type %x>".formatted(elem);String arrayDefStr = "[%d]%s".formatted(len, elemName);String defStrWithLinks = "[%d]%s".formatted(len,AddressAnnotatedStringHandler.createAddressAnnotationString(elem, elemName));@@ -139,4 +141,19 @@ public class GoArrayType extends GoType {return "type %s%s".formatted(hasName ? selfName + " " : "", defStrWithLinks);}+ @Override+ public boolean isValid() {+ try {+ GoType elementType = getElement();+ return elementType != null && super.isValid() && isValidSize(elementType);+ }+ catch (IOException e) {+ return false;+ }+ }++ private boolean isValidSize(GoType elementType) {+ return typ.getSize() == (elementType.getBaseType().getSize() * len);+ }+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoBaseType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoBaseType.javaindex 38c49d1d95..e240f727fb 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoBaseType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoBaseType.java@@ -72,6 +72,13 @@ public class GoBaseType implements StructureVerifier {return size;}+ /**+ * {@return the number of bytes of this type that contain pointer data}+ */+ public long getPtrBytes() {+ return ptrdata;+ }+/*** {@return the {@link GoKind} enum assigned to this type definition}*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoChanType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoChanType.javaindex a29a79c086..76ecc71319 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoChanType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoChanType.java@@ -83,7 +83,12 @@ public class GoChanType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize();+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ return typ.getSize() == programContext.getPtrSize() &&+ typ.getPtrBytes() == programContext.getPtrSize();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoFuncType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoFuncType.javaindex c230990640..2c878941b9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoFuncType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoFuncType.java@@ -286,7 +286,12 @@ public class GoFuncType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize();+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ return typ.getSize() == programContext.getPtrSize() &&+ typ.getPtrBytes() == programContext.getPtrSize();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoInterfaceType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoInterfaceType.javaindex a9f723def5..25cb5d0f77 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoInterfaceType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoInterfaceType.java@@ -185,7 +185,12 @@ public class GoInterfaceType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize() * 2; // runtime.iface?+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ return typ.getSize() == programContext.getPtrSize() * 2 &&+ typ.getPtrBytes() == programContext.getPtrSize() * 2;}@Overridediff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMapType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMapType.javaindex dd5b6811c3..a8d59c2be0 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMapType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMapType.java@@ -169,7 +169,12 @@ public class GoMapType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize();+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ return typ.getSize() == programContext.getPtrSize() &&+ typ.getPtrBytes() == programContext.getPtrSize();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPlainType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPlainType.javaindex 1197c56ddb..ef8b9c959a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPlainType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPlainType.java@@ -67,20 +67,39 @@ public class GoPlainType extends GoType implements StructureReader<GoType> {@Overridepublic boolean isValid() {- return super.isValid() && switch (typ.getKind()) {- case Bool -> typ.getSize() == 1;- case Float32 -> typ.getSize() == 4;- case Float64 -> typ.getSize() == 8;- case Uint, Int -> typ.getSize() == programContext.getPtrSize();- case Uint8, Int8 -> typ.getSize() == 1;- case Uint16, Int16 -> typ.getSize() == 2;- case Uint32, Int32 -> typ.getSize() == 4;- case Uint64, Int64 -> typ.getSize() == 8;- case Uintptr -> typ.getSize() == programContext.getPtrSize();- case String -> typ.getSize() == programContext.getPtrSize() * 2;- case UnsafePointer -> typ.getSize() == programContext.getPtrSize();+ return super.isValid() && isValidSize();+ }++ public boolean isValidSize() {+ return switch (typ.getKind()) {+ case Bool -> isValidPrimitiveSize(1);++ case Float32 -> isValidPrimitiveSize(4);+ case Float64 -> isValidPrimitiveSize(8);+ case Complex64 -> isValidPrimitiveSize(8);+ case Complex128 -> isValidPrimitiveSize(16);++ case Uint, Int -> isValidPrimitiveSize(programContext.getPtrSize());+ case Uint8, Int8 -> isValidPrimitiveSize(1);+ case Uint16, Int16 -> isValidPrimitiveSize(2);+ case Uint32, Int32 -> isValidPrimitiveSize(4);+ case Uint64, Int64 -> isValidPrimitiveSize(8);+ case Uintptr -> isValidPrimitiveSize(programContext.getPtrSize());++ case UnsafePointer -> typ.getSize() == programContext.getPtrSize() &&+ typ.getPtrBytes() == programContext.getPtrSize();++ case String -> typ.getSize() == programContext.getPtrSize() * 2 &&+ typ.getPtrBytes() == programContext.getPtrSize();++ // Pointer, Chan, Map, Slice, Array validSize checking in each custom GoXYZType class+default -> true;};}+ private boolean isValidPrimitiveSize(long expectedSize) {+ return typ.getPtrBytes() == 0 && typ.getSize() == expectedSize;+ }+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPointerType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPointerType.javaindex 2ccd469a84..912c7c2d3a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPointerType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPointerType.java@@ -107,7 +107,14 @@ public class GoPointerType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize();+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ long ptrBytesSize = typ.getPtrBytes();+ // Future: ptrbytessize will occasionally be 0 instead of ptrSize. Need to nail down why+ return typ.getSize() == programContext.getPtrSize() &&+ (ptrBytesSize == 0 || ptrBytesSize == programContext.getPtrSize());}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoSliceType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoSliceType.javaindex a08ae4bfe2..44c19fbdd6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoSliceType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoSliceType.java@@ -135,7 +135,12 @@ public class GoSliceType extends GoType {@Overridepublic boolean isValid() {- return super.isValid() && typ.getSize() == programContext.getPtrSize() * 3; // TODO: knowing the correct size is a bit of a hack+ return super.isValid() && isValidSize();+ }++ private boolean isValidSize() {+ return typ.getSize() == programContext.getPtrSize() * 3 &&+ typ.getPtrBytes() == programContext.getPtrSize();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoType.javaindex 9b9dafa46c..6baa3c62a9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoType.java@@ -56,10 +56,8 @@ public abstract class GoType implements StructureMarkup<GoType>, StructureVerifipublic static Class<? extends GoType> getSpecializedTypeClass(GoRttiMapper programContext,long offset) throws IOException {GoTypeDetector typeDetector = programContext.readStructure(GoTypeDetector.class, offset);- Class<? extends GoType> result = specializedTypeClasses.get(typeDetector.getKind());- if (result == null) {- result = GoPlainType.class;- }+ Class<? extends GoType> result =+ specializedTypeClasses.getOrDefault(typeDetector.getKind(), GoPlainType.class);return result;}