Ghidra is a software reverse engineering (SRE) framework
Fix Go stack parameter alignment
Ash Pera00b0bec49df3
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeManager.javaindex dd70c7c923..831d1bcd56 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeManager.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeManager.java@@ -424,6 +424,9 @@ public class DWARFDataTypeManager {// May need to add name lookup capability to AbstractFloatDataTypecase DWARFEncoding.DW_ATE_float -> AbstractFloatDataType.getFloatDataType(dwarfSize,getCorrectDTMForFixedLengthTypes(name, dwarfSize, isExplictSize));+ case DWARFEncoding.DW_ATE_complex_float -> isExplictSize // Go complex64, complex128+ ? AbstractComplexDataType.getComplexDataType(dwarfSize, null)+ : null;case DWARFEncoding.DW_ATE_signed -> AbstractIntegerDataType.getSignedDataType(dwarfSize,getCorrectDTMForFixedLengthTypes(name, dwarfSize, isExplictSize));case DWARFEncoding.DW_ATE_unsigned -> AbstractIntegerDataType.getUnsignedDataType(diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoParamStorageAllocator.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoParamStorageAllocator.javaindex 014dbf8ce8..cca85ef619 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoParamStorageAllocator.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoParamStorageAllocator.java@@ -226,13 +226,17 @@ public class GoParamStorageAllocator {}public void alignStackFor(DataType dt) {- int alignmentSize = callspecInfo.getAlignmentForType(dt);- stackOffset = NumericUtilities.getUnsignedAlignedValue(stackOffset, alignmentSize);+ alignStack(callspecInfo.getAlignmentForType(dt));}public void alignStack() {- stackOffset =- NumericUtilities.getUnsignedAlignedValue(stackOffset, callspecInfo.getMaxAlign());+ alignStack(callspecInfo.getMaxAlign());+ }++ private void alignStack(int alignment) {+ // Go aligns relative to the start of the stack arguments, not to absolute stack offsets+ long base = callspecInfo.getStackInitialOffset();+ stackOffset = base + NumericUtilities.getUnsignedAlignedValue(stackOffset - base, alignment);}public Register getClosureContextRegister() {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoRegisterInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoRegisterInfo.javaindex 923944ee85..8bec883ed6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoRegisterInfo.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoRegisterInfo.java@@ -165,15 +165,37 @@ public class GoRegisterInfo {if (isIntType(dt) && isIntrinsicSize(dt.getLength())) {return Math.min(maxAlign, dt.getLength());}- if (dt instanceof Complex8DataType /* Go complex64 */ ) {- return 4;+ if (dt instanceof AbstractComplexDataType) { // aligned as its real part+ return Math.min(maxAlign, dt.getLength() / 2);}if (dt instanceof AbstractFloatDataType) {return Math.min(maxAlign, dt.getLength());}+ if (dt instanceof Structure struct) {+ // a Go struct is aligned to its most aligned field, or to its RTTI alignment+ int align = struct.hasExplicitMinimumAlignment()+ ? struct.getExplicitMinimumAlignment()+ : 1;+ for (DataTypeComponent dtc : struct.getDefinedComponents()) {+ align = Math.max(align, isAtomicAlign64(dtc.getDataType())+ ? 8+ : getAlignmentForType(dtc.getDataType()));+ }+ return align;+ }return maxAlign;}+ private static boolean isAtomicAlign64(DataType dt) {+ // as cmd/compile, an atomic.align64 field forces 8-byte alignment on all archs+ return switch (dt.getName()) {+ case "sync/atomic.align64", "internal/runtime/atomic.align64",+ "runtime/internal/atomic.align64" ->+ true;+ default -> false;+ };+ }+static boolean isIntType(DataType dt) {return dt instanceof AbstractIntegerDataType || dt instanceof WideCharDataType ||dt instanceof WideChar16DataType || dt instanceof WideChar32DataType ||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 f3b08b1f81..5ae2b42db9 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@@ -790,6 +790,8 @@ public class GoTypeManager {case Bool -> BooleanDataType.dataType;case Float32 -> AbstractFloatDataType.getFloatDataType(32 / 8, null);case Float64 -> AbstractFloatDataType.getFloatDataType(64 / 8, null);+ case Complex64 -> Complex8DataType.dataType;+ case Complex128 -> Complex16DataType.dataType;case Int -> AbstractIntegerDataType.getSignedDataType(ptrSize, dtm); // depends on archcase Int8 -> Int8TDataType.dataType;case Int16 -> Int16TDataType.dataType;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 e240f727fb..29bf56b0c7 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@@ -53,6 +53,9 @@ public class GoBaseType implements StructureVerifier {@EOLComment("flags")private int tflag;+ @FieldMapping(fieldName = {"align", "Align_"})+ private int align;+@FieldMapping(fieldName = {"kind", "Kind_"})@EOLCommentprivate int kind;@@ -79,6 +82,13 @@ public class GoBaseType implements StructureVerifier {return ptrdata;}+ /**+ * {@return the alignment of the type being defined by this structure}+ */+ public int getAlign() {+ return align;+ }+/*** {@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/GoStructType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoStructType.javaindex ad860e2046..339cdeb364 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoStructType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoStructType.java@@ -169,6 +169,9 @@ public class GoStructType extends GoType {}DWARFUtil.packCompositeIfPossible(struct, goTypes.getDTM());+ if (typ.getAlign() > 1) {+ struct.setExplicitMinimumAlignment(typ.getAlign()); // omitted zero-len fields may align it+ }return struct;}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/golang/GoRegisterInfoTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/golang/GoRegisterInfoTest.javanew file mode 100644index 0000000000..c1b11bea44--- /dev/null+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/golang/GoRegisterInfoTest.java@@ -0,0 +1,100 @@+/* ###+ * IP: GHIDRA+ *+ * 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.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.util.bin.format.golang;++import static org.junit.Assert.*;++import org.junit.Test;++import ghidra.program.database.ProgramBuilder;+import ghidra.program.model.data.*;+import ghidra.program.model.lang.Language;+import ghidra.program.model.lang.LanguageID;+import ghidra.program.model.listing.Program;+import ghidra.test.AbstractGhidraHeadlessIntegrationTest;++public class GoRegisterInfoTest extends AbstractGhidraHeadlessIntegrationTest {++ private static final GoVer GO_VER = GoVer.parse("1.26");++ private static Structure struct(DataType... fields) {+ StructureDataType struct = new StructureDataType("S", 0);+ for (DataType field : fields) {+ struct.add(field);+ }+ return struct;+ }++ private static DataType atomicInt64() {+ return struct(new StructureDataType("sync/atomic.align64", 0), LongLongDataType.dataType);+ }++ private GoRegisterInfo regInfo(String langId) throws Exception {+ Language lang = getLanguageService().getLanguage(new LanguageID(langId));+ return GoRegisterInfoManager.getInstance().getRegisterInfoForLang(lang, GO_VER);+ }++ @Test+ public void testStructAlignment() throws Exception {+ GoRegisterInfo regInfo = regInfo(ProgramBuilder._X64);+ DataType bytes = new ArrayDataType(ByteDataType.dataType, 250, 1);+ assertEquals(1, regInfo.getAlignmentForType(struct(bytes, ByteDataType.dataType)));+ assertEquals(2, regInfo.getAlignmentForType(struct(bytes, UnsignedShortDataType.dataType)));+ assertEquals(8, regInfo.getAlignmentForType(struct(bytes, LongLongDataType.dataType)));+ assertEquals(1, regInfo.getAlignmentForType(struct()));+ }++ @Test+ public void testComplexAlignment() throws Exception {+ GoRegisterInfo regInfo = regInfo(ProgramBuilder._X64);+ DataType named = new TypedefDataType("main.T", Complex8DataType.dataType);+ assertEquals(4, regInfo.getAlignmentForType(named));+ assertEquals(4, regInfo.getAlignmentForType(struct(ByteDataType.dataType, named)));+ assertEquals(4, regInfo.getAlignmentForType(FloatComplexDataType.dataType));+ assertEquals(8, regInfo.getAlignmentForType(Complex16DataType.dataType));+ assertEquals(4, regInfo(ProgramBuilder._X86).getAlignmentForType(Complex16DataType.dataType));+ }++ @Test+ public void testRttiAlignment() throws Exception {+ GoRegisterInfo regInfo = regInfo(ProgramBuilder._X64);+ StructureDataType struct = (StructureDataType) struct(ByteDataType.dataType); // zero-len fields omitted+ struct.setExplicitMinimumAlignment(2); // Go struct{ b byte; _ [0]uint16 }+ assertEquals(2, regInfo.getAlignmentForType(struct));+ }++ @Test+ public void testAtomicAlign64() throws Exception {+ GoRegisterInfo regInfo = regInfo(ProgramBuilder._X86);+ assertEquals(8, regInfo.getAlignmentForType(atomicInt64()));+ assertEquals(8, regInfo.getAlignmentForType(struct(ByteDataType.dataType, atomicInt64())));+ assertEquals(4, regInfo.getAlignmentForType(struct(LongLongDataType.dataType)));+ }++ @Test+ public void testStackAlignmentIsRelativeToArgs() throws Exception {+ ProgramBuilder builder = new ProgramBuilder("go386", ProgramBuilder._X86, this);+ try {+ Program program = builder.getProgram();+ GoParamStorageAllocator allocator = new GoParamStorageAllocator(program, GO_VER);+ long base = allocator.getStackAllocation(ByteDataType.dataType);+ assertEquals(base + 8, allocator.getStackAllocation(atomicInt64()));+ }+ finally {+ builder.dispose();+ }+ }+}