1 //===-- sanitizer_procmaps_mac_test.cpp ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //===----------------------------------------------------------------------===//
13 # include "sanitizer_common/sanitizer_platform.h"
23 # include <mach-o/dyld.h>
24 # include <mach-o/loader.h>
26 # include "gtest/gtest.h"
28 # include "sanitizer_common/sanitizer_procmaps.h"
30 namespace __sanitizer
{
32 class MemoryMappingLayoutMock final
: public MemoryMappingLayout
{
34 static constexpr uuid_command mock_uuid_command
= {
36 .cmdsize
= sizeof(uuid_command
),
40 static constexpr char dylib_name
[] = "libclang_rt.\0\0\0"; // 8 bytes aligned, padded with zeros per loader.h
41 static constexpr dylib_command mock_dylib_command
= {
43 .cmdsize
= sizeof(dylib_command
) + sizeof(dylib_name
),
46 .offset
= sizeof(dylib_command
)
51 static constexpr uuid_command mock_trap_command
= {
57 const char *start_load_cmd_addr
;
59 std::vector
<unsigned char> mock_header
;
62 MemoryMappingLayoutMock(): MemoryMappingLayout(false) {
63 EXPECT_EQ(mock_uuid_command
.cmdsize
% 8, 0u);
64 EXPECT_EQ(mock_dylib_command
.cmdsize
% 8, 0u);
69 const struct mach_header_64
*header
= (mach_header_64
*)_dyld_get_image_header(0); // Any header will do
70 const size_t header_size
= sizeof(mach_header_64
);
72 const struct mach_header
*header
= _dyld_get_image_header(0);
73 const size_t header_size
= sizeof(mach_header
);
75 const size_t mock_header_size_with_extras
= header_size
+ header
->sizeofcmds
+
76 mock_uuid_command
.cmdsize
+ mock_dylib_command
.cmdsize
+ sizeof(uuid_command
);
78 mock_header
.reserve(mock_header_size_with_extras
);
79 // Copy the original header
80 copy((unsigned char *)header
,
81 (unsigned char *)header
+ header_size
+ header
->sizeofcmds
,
82 back_inserter(mock_header
));
83 // The following commands are not supposed to be processed
84 // by the (correct) ::Next method at all, since they're not
85 // accounted for in header->ncmds .
86 copy((unsigned char *)&mock_uuid_command
,
87 ((unsigned char *)&mock_uuid_command
) + mock_uuid_command
.cmdsize
,
88 back_inserter(mock_header
));
89 copy((unsigned char *)&mock_dylib_command
,
90 ((unsigned char *)&mock_dylib_command
) + sizeof(dylib_command
), // as mock_dylib_command.cmdsize contains the following string
91 back_inserter(mock_header
));
92 copy((unsigned char *)dylib_name
,
93 ((unsigned char *)dylib_name
) + sizeof(dylib_name
),
94 back_inserter(mock_header
));
96 // Append a command w. huge size to have the test detect the read overrun
97 copy((unsigned char *)&mock_trap_command
,
98 ((unsigned char *)&mock_trap_command
) + sizeof(uuid_command
),
99 back_inserter(mock_header
));
101 start_load_cmd_addr
= (const char *)(mock_header
.data() + header_size
);
102 sizeofcmds
= header
->sizeofcmds
;
104 const char *last_byte_load_cmd_addr
= (start_load_cmd_addr
+sizeofcmds
-1);
105 data_
.current_image
= -1; // So the loop in ::Next runs just once
108 size_t SizeOfLoadCommands() {
112 size_t CurrentLoadCommandOffset() {
113 size_t offset
= data_
.current_load_cmd_addr
- start_load_cmd_addr
;
118 virtual ImageHeader
*CurrentImageHeader() override
{
119 return (ImageHeader
*)mock_header
.data();
123 TEST(MemoryMappingLayout
, Next
) {
124 __sanitizer::MemoryMappingLayoutMock memory_mapping
;
125 __sanitizer::MemoryMappedSegment segment
;
126 size_t size
= memory_mapping
.SizeOfLoadCommands();
127 while (memory_mapping
.Next(&segment
)) {
128 size_t offset
= memory_mapping
.CurrentLoadCommandOffset();
129 EXPECT_LE(offset
, size
);
131 size_t final_offset
= memory_mapping
.CurrentLoadCommandOffset();
132 EXPECT_EQ(final_offset
, size
); // All commands processed, no more, no less
135 } // namespace __sanitizer
137 # endif // SANITIZER_APPLE