Skip to content

Commit 52b0b6c

Browse files
Fix
1 parent f92224d commit 52b0b6c

30 files changed

+880
-530
lines changed

src/main/java/golanganalyzerextension/FunctionModifier.java

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import golanganalyzerextension.function.FileLine;
1515
import golanganalyzerextension.function.GolangFunction;
1616
import golanganalyzerextension.gobinary.GolangBinary;
17+
import golanganalyzerextension.gobinary.exceptions.BinaryAccessException;
1718
import golanganalyzerextension.log.Logger;
1819
import golanganalyzerextension.service.GolangAnalyzerExtensionService;
1920

@@ -102,39 +103,42 @@ private boolean init_file_name_list() {
102103
}
103104

104105
int pointer_size=go_bin.get_pointer_size();
105-
Address gopclntab_base=go_bin.get_gopclntab_base();
106-
func_num=go_bin.get_address_value(gopclntab_base, 8, pointer_size);
107-
file_name_list=new ArrayList<>();
108-
if(is_go116) {
109-
return true;
110-
}
111-
Address func_list_base=go_bin.get_address(gopclntab_base, 8+pointer_size);
112-
if(func_list_base==null) {
106+
Address gopclntab_base=go_bin.get_gopclntab_base().orElse(null);
107+
if(gopclntab_base==null) {
113108
return false;
114109
}
115110

116-
long file_name_table_offset=go_bin.get_address_value(func_list_base, func_num*pointer_size*2+pointer_size, pointer_size);
117-
Address file_name_table=go_bin.get_address(gopclntab_base, file_name_table_offset);
118-
long file_name_table_size=go_bin.get_address_value(file_name_table, 4);
119-
if(file_name_table==null || file_name_table_size==0) {
120-
return false;
121-
}
111+
try {
112+
func_num=go_bin.get_address_value(gopclntab_base, 8, pointer_size);
113+
file_name_list=new ArrayList<>();
114+
if(is_go116) {
115+
return true;
116+
}
117+
Address func_list_base=go_bin.get_address(gopclntab_base, 8+pointer_size);
122118

123-
for(int i=1;i<file_name_table_size;i++) {
124-
long file_name_offset=go_bin.get_address_value(file_name_table, 4*i,4);
125-
if(file_name_offset==0) {
119+
long file_name_table_offset=go_bin.get_address_value(func_list_base, func_num*pointer_size*2+pointer_size, pointer_size);
120+
Address file_name_table=go_bin.get_address(gopclntab_base, file_name_table_offset);
121+
long file_name_table_size=go_bin.get_address_value(file_name_table, 4);
122+
if(file_name_table_size==0) {
126123
return false;
127124
}
128-
Address file_name_addr=go_bin.get_address(gopclntab_base, file_name_offset);
129-
if(file_name_addr==null) {
130-
return false;
125+
126+
for(int i=1;i<file_name_table_size;i++) {
127+
long file_name_offset=go_bin.get_address_value(file_name_table, 4*i,4);
128+
if(file_name_offset==0) {
129+
return false;
130+
}
131+
Address file_name_addr=go_bin.get_address(gopclntab_base, file_name_offset);
132+
file_name_list.add(go_bin.create_string_data(file_name_addr).orElse(String.format("not_found_%x", file_name_addr.getOffset())));
131133
}
132-
file_name_list.add(go_bin.create_string_data(file_name_addr).orElse(String.format("not_found_%x", file_name_addr.getOffset())));
133-
}
134134

135-
service.store_filename_list(file_name_list);
135+
service.store_filename_list(file_name_list);
136136

137-
return true;
137+
return true;
138+
} catch (BinaryAccessException e) {
139+
Logger.append_message(String.format("Failed to init file name list: pcheader_addr=%s, message=%s", gopclntab_base, e.getMessage()));
140+
return false;
141+
}
138142
}
139143

140144
private boolean init_functions() {
@@ -148,38 +152,54 @@ private boolean init_functions() {
148152
}
149153

150154
int pointer_size=go_bin.get_pointer_size();
151-
Address gopclntab_base=go_bin.get_gopclntab_base();
152-
gofunc_list=new ArrayList<>();
153-
Address func_list_base=null;
154-
if(is_go118) {
155-
func_list_base=go_bin.get_address(gopclntab_base, go_bin.get_address_value(gopclntab_base, 8+pointer_size*7, pointer_size));
156-
}else if(is_go116) {
157-
func_list_base=go_bin.get_address(gopclntab_base, go_bin.get_address_value(gopclntab_base, 8+pointer_size*6, pointer_size));
158-
}else {
159-
func_list_base=go_bin.get_address(gopclntab_base, 8+pointer_size);
160-
}
161-
if(func_list_base==null) {
155+
Address gopclntab_base=go_bin.get_gopclntab_base().orElse(null);
156+
if(gopclntab_base==null) {
162157
return false;
163158
}
164159

165-
for(int i=0; i<func_num; i++) {
166-
long func_addr_value=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2, is_go118?4:pointer_size);
160+
gofunc_list=new ArrayList<>();
161+
Address func_list_base;
162+
try {
167163
if(is_go118) {
168-
func_addr_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
169-
}
170-
long func_info_offset=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2+(is_go118?4:pointer_size), is_go118?4:pointer_size);
171-
Address func_info_addr=null;
172-
if(is_go116) {
173-
func_info_addr=go_bin.get_address(func_list_base, func_info_offset);
164+
func_list_base=go_bin.get_address(gopclntab_base, go_bin.get_address_value(gopclntab_base, 8+pointer_size*7, pointer_size));
165+
}else if(is_go116) {
166+
func_list_base=go_bin.get_address(gopclntab_base, go_bin.get_address_value(gopclntab_base, 8+pointer_size*6, pointer_size));
174167
}else {
175-
func_info_addr=go_bin.get_address(gopclntab_base, func_info_offset);
168+
func_list_base=go_bin.get_address(gopclntab_base, 8+pointer_size);
176169
}
170+
} catch (BinaryAccessException e) {
171+
Logger.append_message(String.format("Failed to init funcs: pcheader_addr=%s, message=%s", gopclntab_base, e.getMessage()));
172+
return false;
173+
}
177174

178-
long func_entry_value=go_bin.get_address_value(func_info_addr, is_go118?4:pointer_size);
179-
long func_end_value=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2+(is_go118?4:pointer_size)*2, is_go118?4:pointer_size);
180-
if(is_go118) {
181-
func_entry_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
182-
func_end_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
175+
for(int i=0; i<func_num; i++) {
176+
long func_addr_value;
177+
long func_info_offset;
178+
Address func_info_addr;
179+
long func_entry_value;
180+
long func_end_value;
181+
try {
182+
func_addr_value=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2, is_go118?4:pointer_size);
183+
if(is_go118) {
184+
func_addr_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
185+
}
186+
func_info_offset=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2+(is_go118?4:pointer_size), is_go118?4:pointer_size);
187+
188+
if(is_go116) {
189+
func_info_addr=go_bin.get_address(func_list_base, func_info_offset);
190+
}else {
191+
func_info_addr=go_bin.get_address(gopclntab_base, func_info_offset);
192+
}
193+
194+
func_entry_value=go_bin.get_address_value(func_info_addr, is_go118?4:pointer_size);
195+
func_end_value=go_bin.get_address_value(func_list_base, i*(is_go118?4:pointer_size)*2+(is_go118?4:pointer_size)*2, is_go118?4:pointer_size);
196+
if(is_go118) {
197+
func_entry_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
198+
func_end_value+=go_bin.get_address_value(gopclntab_base, 8+pointer_size*2, pointer_size);
199+
}
200+
} catch (BinaryAccessException e) {
201+
Logger.append_message(String.format("Failed to init func: pcheader_addr=%s, func_list_base=%s, i=%d, message=%s", gopclntab_base, func_list_base, i, e.getMessage()));
202+
return false;
183203
}
184204

185205
if(func_addr_value==0 || func_info_offset==0 || func_entry_value==0) {
@@ -249,17 +269,25 @@ private void modify_func_param(GolangFunction gofunc) {
249269

250270
private void add_func_info_comment(GolangFunction gofunc) {
251271
String comment="Name: "+gofunc.get_func_name()+"\n";
252-
comment+=String.format("Start: %x\n", gofunc.get_func_addr().getOffset());
253-
comment+=String.format("End: %x", gofunc.get_func_addr().add(gofunc.get_func_size()).getOffset());
254-
go_bin.set_comment(gofunc.get_func_addr(), ghidra.program.model.listing.CodeUnit.PLATE_COMMENT, comment);
272+
comment+=String.format("Start: %s\n", gofunc.get_func_addr());
273+
try {
274+
comment+=String.format("End: %s", go_bin.get_address(gofunc.get_func_addr(), gofunc.get_func_size()));
275+
go_bin.set_comment(gofunc.get_func_addr(), ghidra.program.model.listing.CodeUnit.PLATE_COMMENT, comment);
276+
} catch (BinaryAccessException e) {
277+
Logger.append_message(String.format("Failed to add func comment: addr=%s, name=%s, message=%s", gofunc.get_func_addr(), gofunc.get_func_name(), e.getMessage()));
278+
}
255279
}
256280

257281
private void add_file_line_comment(GolangFunction gofunc) {
258282
Address addr=gofunc.get_func_addr();
259283
Map<Integer, FileLine> comment_map=gofunc.get_file_line_comment_map();
260284

261285
for(Integer key: comment_map.keySet()) {
262-
go_bin.set_comment(go_bin.get_address(addr, key), ghidra.program.model.listing.CodeUnit.PRE_COMMENT, comment_map.get(key).toString());
286+
try {
287+
go_bin.set_comment(go_bin.get_address(addr, key), ghidra.program.model.listing.CodeUnit.PRE_COMMENT, comment_map.get(key).toString());
288+
} catch (BinaryAccessException e) {
289+
Logger.append_message(String.format("Failed to add file line comment: addr=%s, name=%s, message=%s", gofunc.get_func_addr(), gofunc.get_func_name(), e.getMessage()));
290+
}
263291
}
264292
}
265293
}

src/main/java/golanganalyzerextension/StructureManager.java

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import golanganalyzerextension.exceptions.InvalidBinaryStructureException;
1212
import golanganalyzerextension.gobinary.GolangBinary;
1313
import golanganalyzerextension.gobinary.ModuleData;
14+
import golanganalyzerextension.gobinary.exceptions.BinaryAccessException;
1415
import golanganalyzerextension.log.Logger;
1516
import golanganalyzerextension.service.GolangAnalyzerExtensionService;
1617

@@ -83,7 +84,7 @@ private String make_datatype_comment(GolangDatatype go_datatype, DatatypeHolder
8384
if(go_datatype.get_uncommon_type().isPresent()) {
8485
comment+="Method:\n";
8586
for(UncommonMethod method : go_datatype.get_uncommon_type().get().get_method_list()) {
86-
comment+=String.format(" +%s %#x %#x\n", method.get_name(), method.get_interface_method_addr().getOffset(), method.get_normal_method_addr().getOffset());
87+
comment+=String.format(" +%s %s %s\n", method.get_name(), method.get_interface_method_addr().orElse(null), method.get_normal_method_addr().orElse(null));
8788
}
8889
}
8990

@@ -92,7 +93,11 @@ private String make_datatype_comment(GolangDatatype go_datatype, DatatypeHolder
9293

9394
private boolean init_basig_golang_datatype() {
9495
ByteBuffer buffer=ByteBuffer.allocate(Long.BYTES);
95-
buffer.putLong(go_bin.get_gopclntab_base().getOffset());
96+
Address gopclntab_base=go_bin.get_gopclntab_base().orElse(null);
97+
if(gopclntab_base==null) {
98+
return false;
99+
}
100+
buffer.putLong(gopclntab_base.getOffset());
96101
buffer.flip();
97102
buffer.order(ByteOrder.LITTLE_ENDIAN);
98103
long reverse=buffer.getLong();
@@ -103,50 +108,49 @@ private boolean init_basig_golang_datatype() {
103108
int pointer_size=go_bin.get_pointer_size();
104109
Address base_addr=null;
105110
while(true) {
106-
if(pointer_size==4) {
107-
base_addr=go_bin.find_memory(base_addr, gopclntab_base_bytes, new byte[] {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00});
108-
}else {
109-
base_addr=go_bin.find_memory(base_addr, gopclntab_base_bytes, new byte[] {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff});
110-
}
111-
if(base_addr==null) {
112-
break;
113-
}
114-
115-
ModuleData module_data=null;
116111
try {
117-
module_data=new ModuleData(go_bin, base_addr);
118-
} catch(InvalidBinaryStructureException e) {
119-
Logger.append_message(String.format("Failed to get module data: %s", e.getMessage()));
120-
base_addr=go_bin.get_address(base_addr, 4);
112+
if(pointer_size==4) {
113+
base_addr=go_bin.find_memory(base_addr, gopclntab_base_bytes, new byte[] {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00}).orElse(null);
114+
}else {
115+
base_addr=go_bin.find_memory(base_addr, gopclntab_base_bytes, new byte[] {(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff}).orElse(null);
116+
}
121117
if(base_addr==null) {
122118
break;
123119
}
124-
continue;
125-
}
126120

127-
Address type_addr=module_data.get_type_addr();
128-
Address typelink_addr=module_data.get_typelink_addr();
129-
long typelink_len=module_data.get_typelink_len();
130-
boolean is_go16=module_data.get_is_go16();
131-
datatype_holder=new DatatypeHolder(go_bin, is_go16);
132-
133-
for(long i=0;i<typelink_len;i++)
134-
{
135-
long offset=0;
136-
if(is_go16) {
137-
offset=go_bin.get_address_value(typelink_addr, pointer_size*i, pointer_size)-type_addr.getOffset();
138-
}else {
139-
offset=go_bin.get_address_value(typelink_addr, i*4, 4);
140-
}
121+
ModuleData module_data;
141122
try {
142-
analyze_type(type_addr, offset, is_go16);
123+
module_data=new ModuleData(go_bin, base_addr);
143124
} catch(InvalidBinaryStructureException e) {
144-
Logger.append_message(String.format("Failed to analyze type: addr=%x, offset=%x message=%s", type_addr.getOffset(), offset, e.getMessage()));
125+
Logger.append_message(String.format("Failed to get module data: %s", e.getMessage()));
126+
base_addr=go_bin.get_address(base_addr, 4);
127+
continue;
128+
}
129+
130+
Address type_addr=module_data.get_type_addr();
131+
Address typelink_addr=module_data.get_typelink_addr();
132+
long typelink_len=module_data.get_typelink_len();
133+
boolean is_go16=module_data.get_is_go16();
134+
datatype_holder=new DatatypeHolder(go_bin, is_go16);
135+
136+
for(long i=0;i<typelink_len;i++)
137+
{
138+
long offset;
139+
if(is_go16) {
140+
offset=go_bin.get_address_value(typelink_addr, pointer_size*i, pointer_size)-type_addr.getOffset();
141+
}else {
142+
offset=go_bin.get_address_value(typelink_addr, i*4, 4);
143+
}
144+
try {
145+
analyze_type(type_addr, offset, is_go16);
146+
} catch(InvalidBinaryStructureException e) {
147+
Logger.append_message(String.format("Failed to analyze type: addr=%s, offset=%x, message=%s", type_addr, offset, e.getMessage()));
148+
}
145149
}
146-
}
147150

148-
base_addr=go_bin.get_address(base_addr, 4);
149-
if(base_addr==null) {
151+
base_addr=go_bin.get_address(base_addr, 4);
152+
} catch (BinaryAccessException e) {
153+
Logger.append_message(String.format("Failed to get datatypes: addr=%s, message=%s", base_addr, e.getMessage()));
150154
break;
151155
}
152156
}
@@ -167,7 +171,11 @@ private boolean analyze_type(Address type_base_addr, long offset, boolean is_go1
167171
datatype_holder.put_datatype(offset, go_datatype);
168172

169173
for(long dependence_type_key : go_datatype.get_dependence_type_key_list()) {
170-
analyze_type(type_base_addr, dependence_type_key, is_go16);
174+
try {
175+
analyze_type(type_base_addr, dependence_type_key, is_go16);
176+
} catch(InvalidBinaryStructureException e) {
177+
Logger.append_message(String.format("Failed to analyze dependence type: addr=%s, offset=%x, message=%s", type_base_addr, offset, e.getMessage()));
178+
}
171179
}
172180
go_datatype.make_datatype(datatype_holder);
173181
datatype_holder.replace_datatype(offset, go_datatype);

src/main/java/golanganalyzerextension/datatype/ArrayGolangDatatype.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ghidra.program.model.data.VoidDataType;
88
import golanganalyzerextension.DatatypeHolder;
99
import golanganalyzerextension.gobinary.GolangBinary;
10+
import golanganalyzerextension.gobinary.exceptions.BinaryAccessException;
1011

1112

1213
public class ArrayGolangDatatype extends GolangDatatype {
@@ -32,7 +33,7 @@ public void make_datatype(DatatypeHolder datatype_holder) {
3233
}
3334

3435
@Override
35-
void parse_datatype() {
36+
void parse_datatype() throws BinaryAccessException {
3637
int pointer_size=go_bin.get_pointer_size();
3738

3839
long elem_addr_value=go_bin.get_address_value(ext_base_addr, pointer_size);

src/main/java/golanganalyzerextension/datatype/ChanGolangDatatype.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ghidra.program.model.data.StructureDataType;
77
import golanganalyzerextension.DatatypeHolder;
88
import golanganalyzerextension.gobinary.GolangBinary;
9+
import golanganalyzerextension.gobinary.exceptions.BinaryAccessException;
910

1011

1112
public class ChanGolangDatatype extends GolangDatatype {
@@ -48,7 +49,7 @@ public DataType get_inner_datatype(boolean once) {
4849
}
4950

5051
@Override
51-
void parse_datatype() {
52+
void parse_datatype() throws BinaryAccessException {
5253
int pointer_size=go_bin.get_pointer_size();
5354

5455
long elem_addr_value=go_bin.get_address_value(ext_base_addr, pointer_size);

src/main/java/golanganalyzerextension/datatype/FuncGolangDatatype.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ghidra.program.model.data.VoidDataType;
99
import golanganalyzerextension.DatatypeHolder;
1010
import golanganalyzerextension.gobinary.GolangBinary;
11+
import golanganalyzerextension.gobinary.exceptions.BinaryAccessException;
1112

1213

1314
public class FuncGolangDatatype extends GolangDatatype {
@@ -24,7 +25,7 @@ public void make_datatype(DatatypeHolder datatype_searcher) {
2425
}
2526

2627
@Override
27-
void parse_datatype() {
28+
void parse_datatype() throws BinaryAccessException {
2829
int pointer_size=go_bin.get_pointer_size();
2930

3031
int in_len=(short)go_bin.get_address_value(ext_base_addr, 2);

0 commit comments

Comments
 (0)