Skip to content

Commit b81df5e

Browse files
author
Mariusz Borsa
committed
[Sanitizers][Darwin][NFC] clang-format sanitizer_procmaps_mac files
Apparently it wasn't done before. Attaching it to the fix for: rdar://143903403
1 parent 0931113 commit b81df5e

File tree

2 files changed

+69
-68
lines changed

2 files changed

+69
-68
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,19 @@ static const load_command *NextCommand(const load_command *lc) {
334334
return (const load_command *)((const char *)lc + lc->cmdsize);
335335
}
336336

337-
#ifdef MH_MAGIC_64
337+
# ifdef MH_MAGIC_64
338338
static const size_t header_size = sizeof(mach_header_64);
339-
#else
339+
# else
340340
static const size_t header_size = sizeof(mach_header);
341-
#endif
341+
# endif
342342

343343
static void FindUUID(const load_command *first_lc, const mach_header *hdr,
344344
u8 *uuid_output) {
345345
unsigned short curcmd = 0;
346346
for (const load_command *lc = first_lc; curcmd < hdr->ncmds;
347347
curcmd++, lc = NextCommand(lc)) {
348-
349-
CHECK_LT((const char*)lc, (const char*)hdr + header_size + hdr->sizeofcmds);
348+
CHECK_LT((const char *)lc,
349+
(const char *)hdr + header_size + hdr->sizeofcmds);
350350

351351
if (lc->cmd != LC_UUID)
352352
continue;
@@ -363,8 +363,8 @@ static bool IsModuleInstrumented(const load_command *first_lc,
363363
unsigned short curcmd = 0;
364364
for (const load_command *lc = first_lc; curcmd < hdr->ncmds;
365365
curcmd++, lc = NextCommand(lc)) {
366-
367-
CHECK_LT((const char*)lc, (const char*)hdr + header_size + hdr->sizeofcmds);
366+
CHECK_LT((const char *)lc,
367+
(const char *)hdr + header_size + hdr->sizeofcmds);
368368

369369
if (lc->cmd != LC_LOAD_DYLIB)
370370
continue;
@@ -413,11 +413,10 @@ bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
413413
continue;
414414
}
415415
}
416-
FindUUID((const load_command *)data_.current_load_cmd_addr,
417-
hdr, data_.current_uuid);
416+
FindUUID((const load_command *)data_.current_load_cmd_addr, hdr,
417+
data_.current_uuid);
418418
data_.current_instrumented = IsModuleInstrumented(
419-
(const load_command *)data_.current_load_cmd_addr,
420-
hdr);
419+
(const load_command *)data_.current_load_cmd_addr, hdr);
421420
}
422421

423422
while (data_.current_load_cmd_count > 0) {

compiler-rt/lib/sanitizer_common/tests/sanitizer_procmaps_mac_test.cpp

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6360
public:
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

138140
TEST(MemoryMappingLayout, NextUnInstrumented) {

0 commit comments

Comments
 (0)