Skip to content

Commit 676e60f

Browse files
committed
Merge remote-tracking branch
'origin/GP-4183_dev747368_PR-6072_astrelsky_golang_1.21' (Closes NationalSecurityAgency#6072)
2 parents e1dc2dd + 5e18efd commit 676e60f

29 files changed

+218
-137
lines changed

Ghidra/Features/Base/certification.manifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ data/typeinfo/golang/golang_1.17_anybit_any.gdt||GHIDRA||||END|
9191
data/typeinfo/golang/golang_1.18_anybit_any.gdt||GHIDRA||||END|
9292
data/typeinfo/golang/golang_1.19_anybit_any.gdt||GHIDRA||||END|
9393
data/typeinfo/golang/golang_1.20_anybit_any.gdt||GHIDRA||||END|
94+
data/typeinfo/golang/golang_1.21_anybit_any.gdt||GHIDRA||||END|
9495
data/typeinfo/golang/runtimesnapshot.go||GHIDRA||||END|
9596
data/typeinfo/mac_10.9/mac_osx.gdt||GHIDRA||||END|
9697
data/typeinfo/rust/rust-common.gdt||GHIDRA||||END|
Binary file not shown.

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/GolangStringAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public boolean added(Program program, AddressSetView set, TaskMonitor monitor, M
9999

100100
goBinary = GoRttiMapper.getSharedGoBinary(program, monitor);
101101
if (goBinary == null) {
102-
Msg.error(this, "Golang analyzer error: unable to get GoRttiMapper");
102+
Msg.error(this, "Golang string analyzer error: unable to get GoRttiMapper");
103103
return false;
104104
}
105105
markupSession = goBinary.createMarkupSession(monitor);

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/GolangSymbolAnalyzer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public boolean added(Program program, AddressSetView set, TaskMonitor monitor, M
112112

113113
goBinary = GoRttiMapper.getSharedGoBinary(program, monitor);
114114
if (goBinary == null) {
115-
Msg.error(this, "Golang analyzer error: unable to get GoRttiMapper");
115+
Msg.error(this, "Golang symbol analyzer error: unable to get GoRttiMapper");
116116
return false;
117117
}
118118

@@ -136,6 +136,7 @@ public boolean added(Program program, AddressSetView set, TaskMonitor monitor, M
136136
}
137137

138138
if (analyzerOptions.propagateRtti) {
139+
Msg.info(this, "Golang symbol analyzer: scheduling RTTI propagation after reference analysis");
139140
aam.schedule(new PropagateRttiBackgroundCommand(goBinary),
140141
AnalysisPriority.REFERENCE_ANALYSIS.after().priority());
141142
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.util.bin.format.golang;
17+
18+
import java.io.IOException;
19+
20+
public class BootstrapInfoException extends IOException {
21+
22+
public BootstrapInfoException() {
23+
// empty
24+
}
25+
26+
public BootstrapInfoException(String message) {
27+
super(message);
28+
}
29+
30+
public BootstrapInfoException(Throwable cause) {
31+
super(cause);
32+
}
33+
34+
public BootstrapInfoException(String message, Throwable cause) {
35+
super(message, cause);
36+
}
37+
38+
}

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/GoVer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ public enum GoVer {
2828
V1_17(1, 17),
2929
V1_18(1, 18),
3030
V1_19(1, 19),
31-
V1_20(1, 20);
31+
V1_20(1, 20),
32+
V1_21(1, 21),
33+
V1_22(1, 22);
3234

3335
private final int major;
3436
private final int minor;
3537

36-
private GoVer(int major, int minor) {
38+
GoVer(int major, int minor) {
3739
this.major = major;
3840
this.minor = minor;
3941
}
@@ -89,7 +91,7 @@ public static GoVer parse(String s) {
8991
}
9092
}
9193
catch (NumberFormatException e) {
92-
return UNKNOWN;
94+
// fall thru, return unknown
9395
}
9496
return UNKNOWN;
9597
}

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/GoRttiMapper.java

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,54 +112,63 @@ public static GoRttiMapper getSharedGoBinary(Program program, TaskMonitor monito
112112
GoRttiMapper supplier_result = getGoBinary(program);
113113
if (supplier_result != null) {
114114
supplier_result.init(monitor);
115+
return supplier_result;
115116
}
116-
return supplier_result;
117117
}
118-
catch (IllegalArgumentException | IOException e) {
119-
TransientProgramProperties.getProperty(program, FAILED_FLAG,
120-
TransientProgramProperties.SCOPE.PROGRAM, Boolean.class, () -> true); // also sets it
121-
122-
if (e instanceof IOException) {
123-
// this is a more serious error, and the stack trace should be written
124-
// to the application log
125-
Msg.error(GoRttiMapper.class,
126-
"Failed to read golang info for: " + program.getName(), e);
118+
catch (BootstrapInfoException mbie) {
119+
Msg.warn(GoRttiMapper.class, mbie.getMessage());
120+
logAnalyzerMsg(program, mbie.getMessage());
121+
}
122+
catch (IOException e) {
123+
// this is a more serious error, and the stack trace should be written
124+
// to the application log
125+
Msg.error(GoRttiMapper.class, "Failed to read golang info", e);
126+
logAnalyzerMsg(program, e.getMessage());
127+
}
127128

128-
}
129-
AutoAnalysisManager aam = AutoAnalysisManager.getAnalysisManager(program);
130-
if (aam.isAnalyzing()) {
131-
// should cause a modal popup at end of analysis that the go binary wasn't
132-
// supported
133-
MessageLog log = aam.getMessageLog();
134-
log.appendMsg(e.getMessage());
135-
}
136-
else {
137-
Msg.warn(GoRttiMapper.class, "Golang program: " + e.getMessage());
138-
}
129+
// this sets the failed flag
130+
TransientProgramProperties.getProperty(program, FAILED_FLAG,
131+
TransientProgramProperties.SCOPE.PROGRAM, Boolean.class, () -> true);
139132

140-
return null;
141-
}
133+
return null;
142134
});
143135

144136
return goBinary;
145137
}
146138

139+
private static void logAnalyzerMsg(Program program, String msg) {
140+
AutoAnalysisManager aam = AutoAnalysisManager.getAnalysisManager(program);
141+
if (aam.isAnalyzing()) {
142+
// should cause a modal popup at end of analysis that will show the message
143+
MessageLog log = aam.getMessageLog();
144+
log.appendMsg(msg);
145+
}
146+
}
147+
147148
/**
148149
* Creates a {@link GoRttiMapper} representing the specified program.
149150
*
150151
* @param program {@link Program}
151152
* @return new {@link GoRttiMapper}, or null if basic golang information is not found in the
152153
* binary
153-
* @throws IllegalArgumentException if the golang binary is an unsupported version
154+
* @throws BootstrapInfoException if it is a golang binary and has an unsupported or
155+
* unparseable version number or if there was a missing golang bootstrap .gdt file
154156
* @throws IOException if there was an error in the Ghidra golang rtti reading logic
155157
*/
156158
public static GoRttiMapper getGoBinary(Program program)
157-
throws IllegalArgumentException, IOException {
159+
throws BootstrapInfoException, IOException {
158160
GoBuildInfo buildInfo = GoBuildInfo.fromProgram(program);
159-
GoVer goVer;
160-
if (buildInfo == null || (goVer = buildInfo.getVerEnum()) == GoVer.UNKNOWN) {
161+
if (buildInfo == null) {
162+
// probably not a golang binary
161163
return null;
162164
}
165+
166+
GoVer goVer = buildInfo.getVerEnum();
167+
if (goVer == GoVer.UNKNOWN) {
168+
throw new BootstrapInfoException(
169+
"Unsupported Golang version, version info: '%s'".formatted(buildInfo.getVersion()));
170+
}
171+
163172
ResourceFile gdtFile =
164173
findGolangBootstrapGDT(goVer, buildInfo.getPointerSize(), getGolangOSString(program));
165174
if (gdtFile == null) {
@@ -324,11 +333,11 @@ private static Address getArtificalZerobaseAddress(Program program) {
324333
* if not present and types recovered via DWARF should be used instead
325334
* @throws IOException if error linking a structure mapped structure to its matching
326335
* ghidra structure, which is a programming error or a corrupted bootstrap gdt
327-
* @throws IllegalArgumentException if there is no matching bootstrap gdt for this specific
336+
* @throws BootstrapInfoException if there is no matching bootstrap gdt for this specific
328337
* type of golang binary
329338
*/
330339
public GoRttiMapper(Program program, int ptrSize, Endian endian, GoVer goVersion,
331-
ResourceFile archiveGDT) throws IOException, IllegalArgumentException {
340+
ResourceFile archiveGDT) throws IOException, BootstrapInfoException {
332341
super(program, archiveGDT);
333342

334343
this.goVersion = goVersion;
@@ -357,13 +366,15 @@ public GoRttiMapper(Program program, int ptrSize, Endian endian, GoVer goVersion
357366
if (archiveGDT == null) {
358367
// a normal'ish situation where there isn't a .gdt for this arch/binary and there
359368
// isn't any DWARF.
360-
throw new IllegalArgumentException(
361-
"Missing golang .gdt archive for %s, no fallback DWARF info, unable to extract golang RTTI info."
369+
throw new BootstrapInfoException(
370+
"Missing golang .gdt archive for %s, no fallback DWARF info, unable to extract Golang RTTI info."
362371
.formatted(goVersion));
363372
}
364-
// a bad situation where the data type info is corrupted
365-
throw new IOException("Invalid or missing Golang bootstrap GDT file: %s"
366-
.formatted(archiveGDT.getAbsolutePath()));
373+
374+
// we have a .gdt, but something failed.
375+
throw new IOException("Invalid Golang bootstrap GDT file or struct mapping info: %s"
376+
.formatted(archiveGDT.getAbsolutePath()),
377+
e);
367378
}
368379
}
369380

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoArrayType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* {@link GoType} structure that defines an array.
2828
*/
29-
@StructureMapping(structureName = "runtime.arraytype")
29+
@StructureMapping(structureName = {"runtime.arraytype", "internal/abi.ArrayType"})
3030
public class GoArrayType extends GoType {
3131

3232
@FieldMapping

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoBaseType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* struct specialized_type { basetype_struct; (various_fields)* } struct uncommon;
3636
* </pre>
3737
*/
38-
@StructureMapping(structureName = "runtime._type")
38+
@StructureMapping(structureName = {"runtime._type", "internal/abi.Type"})
3939
public class GoBaseType {
4040

4141
@ContextField
@@ -44,17 +44,17 @@ public class GoBaseType {
4444
@ContextField
4545
private GoRttiMapper programContext;
4646

47-
@FieldMapping(signedness = Signedness.Unsigned)
47+
@FieldMapping(fieldName = {"size", "Size_"}, signedness = Signedness.Unsigned)
4848
private long size;
4949

50-
@FieldMapping
50+
@FieldMapping(fieldName = {"ptrdata", "PtrBytes"})
5151
private long ptrdata;
5252

5353
@FieldMapping
5454
@EOLComment("flags")
5555
private int tflag;
5656

57-
@FieldMapping
57+
@FieldMapping(fieldName = {"kind", "Kind_"})
5858
@EOLComment
5959
private int kind;
6060

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoChanType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
* A {@link GoType} structure that defines a go channel
2727
*/
28-
@StructureMapping(structureName = "runtime.chantype")
28+
@StructureMapping(structureName = {"runtime.chantype", "internal/abi.ChanType"})
2929
public class GoChanType extends GoType {
3030

3131
@FieldMapping

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoFuncType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* A {@link GoType} structure that defines a function type.
2929
*/
30-
@StructureMapping(structureName = "runtime.functype")
30+
@StructureMapping(structureName = {"runtime.functype", "internal/abi.FuncType"})
3131
public class GoFuncType extends GoType {
3232

3333
/**

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoIMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import ghidra.program.model.address.Address;
2323
import ghidra.program.model.data.FunctionDefinition;
2424

25-
@StructureMapping(structureName = "runtime.imethod")
25+
@StructureMapping(structureName = {"runtime.imethod", "internal/abi.Imethod"})
2626
public class GoIMethod implements StructureMarkup<GoIMethod> {
2727

2828
@ContextField
@@ -36,7 +36,7 @@ public class GoIMethod implements StructureMarkup<GoIMethod> {
3636
@EOLComment("getName")
3737
private long name;
3838

39-
@FieldMapping
39+
@FieldMapping(fieldName = {"ityp", "Typ"})
4040
@MarkupReference("getType")
4141
private long ityp;
4242

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoInterfaceType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
/**
2929
* A {@link GoType} structure that defines a golang interface.
3030
*/
31-
@StructureMapping(structureName = "runtime.interfacetype")
31+
@StructureMapping(structureName = {"runtime.interfacetype", "internal/abi.InterfaceType"})
3232
public class GoInterfaceType extends GoType {
3333

3434
@FieldMapping
3535
@MarkupReference("getPkgPath")
3636
private long pkgpath; // pointer to name
3737

38-
@FieldMapping
38+
@FieldMapping(fieldName = {"mhdr", "Methods"})
3939
private GoSlice mhdr;
4040

4141
public GoInterfaceType() {

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMapType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* See {@link GoRttiMapper#getMapGoType()} or the "runtime.hmap" type for the definition of
3131
* a instance of a map variable in memory.
3232
*/
33-
@StructureMapping(structureName = "runtime.maptype")
33+
@StructureMapping(structureName = {"runtime.maptype", "internal/abi.MapType"})
3434
public class GoMapType extends GoType {
3535

3636
@FieldMapping
@@ -51,7 +51,7 @@ public class GoMapType extends GoType {
5151
@FieldMapping
5252
private int keysize;
5353

54-
@FieldMapping
54+
@FieldMapping(fieldName = {"elemsize", "ValueSize"})
5555
private int elemsize;
5656

5757
@FieldMapping

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* Structure that defines a method for a GoType, found in the type's {@link GoUncommonType} struct.
3030
*/
31-
@StructureMapping(structureName = "runtime.method")
31+
@StructureMapping(structureName = {"runtime.method", "internal/abi.Method"})
3232
public class GoMethod implements StructureMarkup<GoMethod> {
3333
@ContextField
3434
private GoRttiMapper programContext;

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPlainType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* <p>
3333
* {@link GoType} structure that defines a built-in primitive type.
3434
*/
35-
@StructureMapping(structureName = "runtime._type")
35+
@StructureMapping(structureName = {"runtime._type", "internal/abi.Type"})
3636
public class GoPlainType extends GoType implements StructureReader<GoType> {
3737
@Override
3838
public void readStructure() throws IOException {

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoPointerType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* {@link GoType} structure that defines a pointer.
2828
*/
29-
@StructureMapping(structureName = "runtime.ptrtype")
29+
@StructureMapping(structureName = {"runtime.ptrtype", "internal/abi.PtrType"})
3030
public class GoPointerType extends GoType {
3131
@FieldMapping
3232
@MarkupReference("getElement")

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoSliceType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* See {@link GoRttiMapper#getGenericSliceDT()} or the "runtime.slice" type for the definition of
3030
* a instance of a slice variable in memory.
3131
*/
32-
@StructureMapping(structureName = "runtime.slicetype")
32+
@StructureMapping(structureName = {"runtime.slicetype", "internal/abi.SliceType"})
3333
public class GoSliceType extends GoType {
3434

3535
@FieldMapping

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoStructField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Structure used to define a field in a {@link GoStructType struct type}.
2626
*/
27-
@StructureMapping(structureName = "runtime.structfield")
27+
@StructureMapping(structureName = {"runtime.structfield", "internal/abi.StructField"})
2828
public class GoStructField {
2929

3030
@ContextField

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoStructType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* Golang type information about a specific structure type.
3131
*/
32-
@StructureMapping(structureName = "runtime.structtype")
32+
@StructureMapping(structureName = {"runtime.structtype", "internal/abi.StructType"})
3333
public class GoStructType extends GoType {
3434

3535
@FieldMapping

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/golang/rtti/types/GoType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Class<? extends GoType> getSpecializedTypeClass(GoRttiMapper progr
6969
@ContextField
7070
protected StructureContext<GoType> context;
7171

72-
@FieldMapping
72+
@FieldMapping(fieldName = {"typ", "Type"})
7373
@Markup
7474
@FieldOutput
7575
protected GoBaseType typ;

0 commit comments

Comments
 (0)