@@ -37,18 +37,15 @@ class MemoryMappingLayoutMock final : public MemoryMappingLayout {
3737 .uuid = {}
3838 };
3939
40- static constexpr char libclang_rt_dylib_name[] = " libclang_rt.\0\0\0 " ; // 8 bytes aligned, padded with zeros per loader.h
41- static constexpr char uninstrumented_dylib_name[] = " uninst___rt.\0\0\0 " ; // 8 bytes aligned, padded with zeros per loader.h
40+ static constexpr char libclang_rt_dylib_name[] =
41+ " libclang_rt.\0\0\0 " ; // 8 bytes aligned, padded with zeros per loader.h
42+ static constexpr char uninstrumented_dylib_name[] =
43+ " uninst___rt.\0\0\0 " ; // 8 bytes aligned, padded with zeros per loader.h
4244
4345 static constexpr dylib_command mock_dylib_command = {
44- .cmd = LC_LOAD_DYLIB,
45- .cmdsize = sizeof (dylib_command) + sizeof (libclang_rt_dylib_name),
46- .dylib = {
47- .name = {
48- .offset = sizeof (dylib_command)
49- }
50- }
51- };
46+ .cmd = LC_LOAD_DYLIB,
47+ .cmdsize = sizeof (dylib_command) + sizeof (libclang_rt_dylib_name),
48+ .dylib = {.name = {.offset = sizeof (dylib_command)}}};
5249
5350 static constexpr uuid_command mock_trap_command = {
5451 .cmd = LC_UUID,
@@ -61,52 +58,57 @@ class MemoryMappingLayoutMock final : public MemoryMappingLayout {
6158 std::vector<unsigned char > mock_header;
6259
6360public:
64- MemoryMappingLayoutMock (bool instrumented): MemoryMappingLayout(false ) {
65- EXPECT_EQ (mock_uuid_command.cmdsize % 8 , 0u );
66- EXPECT_EQ (mock_dylib_command.cmdsize % 8 , 0u );
67-
68- Reset ();
69-
70- #ifdef MH_MAGIC_64
71- const struct mach_header_64 *header = (mach_header_64 *)_dyld_get_image_header (0 ); // Any header will do
72- const size_t header_size = sizeof (mach_header_64);
73- #else
74- const struct mach_header *header = _dyld_get_image_header (0 );
75- const size_t header_size = sizeof (mach_header);
76- #endif
77- const size_t mock_header_size_with_extras = header_size + header->sizeofcmds +
78- mock_uuid_command.cmdsize + mock_dylib_command.cmdsize + sizeof (uuid_command);
79-
80- mock_header.reserve (mock_header_size_with_extras);
81- // Copy the original header
82- copy ((unsigned char *)header,
83- (unsigned char *)header + header_size + header->sizeofcmds ,
84- back_inserter (mock_header));
85- // The following commands are not supposed to be processed
86- // by the (correct) ::Next method at all, since they're not
87- // accounted for in header->ncmds .
88- copy ((unsigned char *)&mock_uuid_command,
89- ((unsigned char *)&mock_uuid_command) + mock_uuid_command.cmdsize ,
90- back_inserter (mock_header));
91- copy ((unsigned char *)&mock_dylib_command,
92- ((unsigned char *)&mock_dylib_command) + sizeof (dylib_command), // as mock_dylib_command.cmdsize contains the following string
93- back_inserter (mock_header));
94- const char (&dylib_name)[16 ] = instrumented ? libclang_rt_dylib_name : uninstrumented_dylib_name;
95- copy ((unsigned char *)dylib_name,
96- ((unsigned char *)dylib_name) + sizeof (dylib_name),
97- back_inserter (mock_header));
98-
99- // Append a command w. huge size to have the test detect the read overrun
100- copy ((unsigned char *)&mock_trap_command,
101- ((unsigned char *)&mock_trap_command) + sizeof (uuid_command),
102- back_inserter (mock_header));
103-
104- start_load_cmd_addr = (const char *)(mock_header.data () + header_size);
105- sizeofcmds = header->sizeofcmds ;
106-
107- const char *last_byte_load_cmd_addr = (start_load_cmd_addr+sizeofcmds-1 );
108- data_.current_image = -1 ; // So the loop in ::Next runs just once
109- }
61+ MemoryMappingLayoutMock (bool instrumented) : MemoryMappingLayout(false ) {
62+ EXPECT_EQ (mock_uuid_command.cmdsize % 8 , 0u );
63+ EXPECT_EQ (mock_dylib_command.cmdsize % 8 , 0u );
64+
65+ Reset ();
66+
67+ # ifdef MH_MAGIC_64
68+ const struct mach_header_64 *header =
69+ (mach_header_64 *)_dyld_get_image_header (0 ); // Any header will do
70+ const size_t header_size = sizeof (mach_header_64);
71+ # else
72+ const struct mach_header *header = _dyld_get_image_header (0 );
73+ const size_t header_size = sizeof (mach_header);
74+ # endif
75+ const size_t mock_header_size_with_extras =
76+ header_size + header->sizeofcmds + mock_uuid_command.cmdsize +
77+ mock_dylib_command.cmdsize + sizeof (uuid_command);
78+
79+ mock_header.reserve (mock_header_size_with_extras);
80+ // Copy the original header
81+ copy ((unsigned char *)header,
82+ (unsigned char *)header + header_size + header->sizeofcmds ,
83+ back_inserter (mock_header));
84+ // The following commands are not supposed to be processed
85+ // by the (correct) ::Next method at all, since they're not
86+ // accounted for in header->ncmds .
87+ copy ((unsigned char *)&mock_uuid_command,
88+ ((unsigned char *)&mock_uuid_command) + mock_uuid_command.cmdsize ,
89+ back_inserter (mock_header));
90+ copy ((unsigned char *)&mock_dylib_command,
91+ ((unsigned char *)&mock_dylib_command) +
92+ sizeof (dylib_command), // as mock_dylib_command.cmdsize contains
93+ // the following string
94+ back_inserter (mock_header));
95+ const char (&dylib_name)[16 ] =
96+ instrumented ? libclang_rt_dylib_name : uninstrumented_dylib_name;
97+ copy ((unsigned char *)dylib_name,
98+ ((unsigned char *)dylib_name) + sizeof (dylib_name),
99+ back_inserter (mock_header));
100+
101+ // Append a command w. huge size to have the test detect the read overrun
102+ copy ((unsigned char *)&mock_trap_command,
103+ ((unsigned char *)&mock_trap_command) + sizeof (uuid_command),
104+ back_inserter (mock_header));
105+
106+ start_load_cmd_addr = (const char *)(mock_header.data () + header_size);
107+ sizeofcmds = header->sizeofcmds ;
108+
109+ const char *last_byte_load_cmd_addr = (start_load_cmd_addr + sizeofcmds - 1 );
110+ data_.current_image = -1 ; // So the loop in ::Next runs just once
111+ }
110112
111113 size_t SizeOfLoadCommands () {
112114 return sizeofcmds;
@@ -132,7 +134,7 @@ TEST(MemoryMappingLayout, NextInstrumented) {
132134 EXPECT_LE (offset, size);
133135 }
134136 size_t final_offset = memory_mapping.CurrentLoadCommandOffset ();
135- EXPECT_EQ (final_offset, size); // All commands processed, no more, no less
137+ EXPECT_EQ (final_offset, size); // All commands processed, no more, no less
136138}
137139
138140TEST (MemoryMappingLayout, NextUnInstrumented) {
0 commit comments