Skip to content

Commit 41f2ad6

Browse files
Merge pull request #4762 from smowton/smowton/fix/gbf-in-mach-o
Fix is_goto_binary to pass extended Mach-O header to osx_fat_reader
2 parents 89d248e + 0aa279d commit 41f2ad6

File tree

8 files changed

+113
-20
lines changed

8 files changed

+113
-20
lines changed

src/goto-programs/read_goto_binary.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,10 @@ bool is_goto_binary(
205205
// 1. goto binaries, marked with 0x7f GBF
206206
// 2. ELF binaries, marked with 0x7f ELF
207207

208-
char hdr[4];
209-
hdr[0]=in.get();
210-
hdr[1]=in.get();
211-
hdr[2]=in.get();
212-
hdr[3]=in.get();
208+
char hdr[8];
209+
in.read(hdr, 8);
210+
if(!in)
211+
return false;
213212

214213
if(hdr[0]==0x7f && hdr[1]=='G' && hdr[2]=='B' && hdr[3]=='F')
215214
{

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SRC += analyses/ai/ai.cpp \
2727
goto-programs/goto_program_table_consistency.cpp \
2828
goto-programs/goto_program_validate.cpp \
2929
goto-programs/goto_trace_output.cpp \
30+
goto-programs/is_goto_binary.cpp \
3031
goto-programs/osx_fat_reader.cpp \
3132
goto-programs/remove_returns.cpp \
3233
goto-programs/xml_expr.cpp \

unit/goto-programs/hello_elf

7.98 KB
Binary file not shown.

unit/goto-programs/hello_elf_with_gbf

13.8 KB
Binary file not shown.
File renamed without changes.
20.2 KB
Binary file not shown.

unit/goto-programs/is_goto_binary.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for is_goto_binary
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/use_catch.h>
10+
11+
#include <goto-programs/read_goto_binary.h>
12+
#include <util/message.h>
13+
14+
#include <sstream>
15+
16+
TEST_CASE("is_goto_binary", "[core][goto-programs][is_goto_binary]")
17+
{
18+
std::string class_file("goto-programs/Hello.class");
19+
std::string fat_macho("goto-programs/hello_fat_macho");
20+
std::string fat_macho_with_gbf("goto-programs/hello_fat_macho_with_gbf");
21+
std::string elf("goto-programs/hello_elf");
22+
std::string elf_with_gbf("goto-programs/hello_elf_with_gbf");
23+
24+
std::ostringstream fat_macho_output;
25+
stream_message_handlert fat_macho_output_handler(fat_macho_output);
26+
std::ostringstream fat_macho_with_gbf_output;
27+
stream_message_handlert fat_macho_with_gbf_output_handler(
28+
fat_macho_with_gbf_output);
29+
std::ostringstream elf_output;
30+
stream_message_handlert elf_output_handler(elf_output);
31+
std::ostringstream elf_with_gbf_output;
32+
stream_message_handlert elf_with_gbf_output_handler(elf_with_gbf_output);
33+
std::ostringstream class_file_output;
34+
stream_message_handlert class_file_output_handler(class_file_output);
35+
36+
bool class_file_is_gbf =
37+
is_goto_binary(class_file, class_file_output_handler);
38+
REQUIRE(!class_file_is_gbf);
39+
REQUIRE(class_file_output.str().empty());
40+
41+
bool elf_has_gbf = is_goto_binary(elf, elf_output_handler);
42+
bool elf_with_gbf_has_gbf =
43+
is_goto_binary(elf_with_gbf, elf_with_gbf_output_handler);
44+
REQUIRE(!elf_has_gbf);
45+
REQUIRE(elf_with_gbf_has_gbf);
46+
REQUIRE(elf_output.str().empty());
47+
REQUIRE(elf_with_gbf_output.str().empty());
48+
49+
bool fat_macho_has_gbf = is_goto_binary(fat_macho, fat_macho_output_handler);
50+
bool fat_macho_with_gbf_has_gbf =
51+
is_goto_binary(fat_macho_with_gbf, fat_macho_with_gbf_output_handler);
52+
53+
#ifdef __APPLE__
54+
55+
REQUIRE(!fat_macho_has_gbf);
56+
REQUIRE(fat_macho_output.str().empty());
57+
REQUIRE(fat_macho_with_gbf_has_gbf);
58+
REQUIRE(fat_macho_with_gbf_output.str().empty());
59+
60+
#else
61+
62+
REQUIRE(!fat_macho_has_gbf);
63+
REQUIRE(
64+
fat_macho_output.str() == "Cannot read OSX fat archive on this platform\n");
65+
66+
REQUIRE(!fat_macho_with_gbf_has_gbf);
67+
REQUIRE(
68+
fat_macho_with_gbf_output.str() ==
69+
"Cannot read OSX fat archive on this platform\n");
70+
71+
#endif
72+
}

unit/goto-programs/osx_fat_reader.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,34 @@ Author: Diffblue Ltd.
1818
TEST_CASE("OSX fat binary reader", "[core][goto-programs][osx_fat_reader]")
1919
{
2020
std::ifstream class_file("goto-programs/Hello.class");
21-
std::ifstream fat_binary("goto-programs/hello_fat");
21+
std::ifstream fat_macho("goto-programs/hello_fat_macho");
22+
std::ifstream fat_macho_with_gbf("goto-programs/hello_fat_macho_with_gbf");
2223

2324
char class_header[8];
24-
char fat_binary_header[8];
25+
char fat_macho_header[8];
26+
char fat_macho_with_gbf_header[8];
2527

2628
class_file.read(class_header, 8);
27-
fat_binary.read(fat_binary_header, 8);
29+
fat_macho.read(fat_macho_header, 8);
30+
fat_macho_with_gbf.read(fat_macho_with_gbf_header, 8);
2831

2932
REQUIRE(class_file);
30-
REQUIRE(fat_binary);
33+
REQUIRE(fat_macho);
34+
REQUIRE(fat_macho_with_gbf);
3135

32-
REQUIRE(is_osx_fat_header(fat_binary_header));
36+
REQUIRE(is_osx_fat_header(fat_macho_header));
37+
REQUIRE(is_osx_fat_header(fat_macho_with_gbf_header));
3338
REQUIRE(!is_osx_fat_header(class_header));
3439

3540
class_file.seekg(0);
36-
fat_binary.seekg(0);
37-
38-
std::ostringstream fat_binary_output;
39-
stream_message_handlert fat_binary_output_handler(fat_binary_output);
41+
fat_macho.seekg(0);
42+
fat_macho_with_gbf.seekg(0);
43+
44+
std::ostringstream fat_macho_output;
45+
stream_message_handlert fat_macho_output_handler(fat_macho_output);
46+
std::ostringstream fat_macho_with_gbf_output;
47+
stream_message_handlert fat_macho_with_gbf_output_handler(
48+
fat_macho_with_gbf_output);
4049
std::ostringstream class_file_output;
4150
stream_message_handlert class_file_output_handler(class_file_output);
4251

@@ -45,9 +54,15 @@ TEST_CASE("OSX fat binary reader", "[core][goto-programs][osx_fat_reader]")
4554
REQUIRE_THROWS_AS(
4655
(osx_fat_readert{class_file, class_file_output_handler}),
4756
deserialization_exceptiont);
48-
osx_fat_readert fat_binary_reader(fat_binary, fat_binary_output_handler);
49-
REQUIRE(fat_binary_output.str().empty());
50-
REQUIRE(!fat_binary_reader.has_gb());
57+
58+
osx_fat_readert fat_macho_reader(fat_macho, fat_macho_output_handler);
59+
REQUIRE(fat_macho_output.str().empty());
60+
REQUIRE(!fat_macho_reader.has_gb());
61+
62+
osx_fat_readert fat_macho_with_gbf_reader(
63+
fat_macho_with_gbf, fat_macho_with_gbf_output_handler);
64+
REQUIRE(fat_macho_with_gbf_output.str().empty());
65+
REQUIRE(fat_macho_with_gbf_reader.has_gb());
5166

5267
#else
5368

@@ -57,10 +72,16 @@ TEST_CASE("OSX fat binary reader", "[core][goto-programs][osx_fat_reader]")
5772
class_file_output.str() ==
5873
"Cannot read OSX fat archive on this platform\n");
5974

60-
osx_fat_readert fat_binary_reader(fat_binary, fat_binary_output_handler);
61-
REQUIRE(!fat_binary_reader.has_gb());
75+
osx_fat_readert fat_macho_reader(fat_macho, fat_macho_output_handler);
76+
REQUIRE(!fat_macho_reader.has_gb());
77+
REQUIRE(
78+
fat_macho_output.str() == "Cannot read OSX fat archive on this platform\n");
79+
80+
osx_fat_readert fat_macho_with_gbf_reader(
81+
fat_macho_with_gbf, fat_macho_with_gbf_output_handler);
82+
REQUIRE(!fat_macho_with_gbf_reader.has_gb());
6283
REQUIRE(
63-
fat_binary_output.str() ==
84+
fat_macho_with_gbf_output.str() ==
6485
"Cannot read OSX fat archive on this platform\n");
6586

6687
#endif

0 commit comments

Comments
 (0)