1//===-- sanitizer_procmaps_mac_test.cpp ---------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10//
11//===----------------------------------------------------------------------===//
12
13# include "sanitizer_common/sanitizer_platform.h"
14
15# if SANITIZER_APPLE
16
17# include <stdlib.h>
18# include <string.h>
19# include <stdint.h>
20# include <stdio.h>
21
22# include <vector>
23# include <mach-o/dyld.h>
24# include <mach-o/loader.h>
25
26# include "gtest/gtest.h"
27
28# include "sanitizer_common/sanitizer_procmaps.h"
29
30namespace __sanitizer {
31
32class MemoryMappingLayoutMock final : public MemoryMappingLayout {
33private:
34 static constexpr uuid_command mock_uuid_command = {
35 .cmd = LC_UUID,
36 .cmdsize = sizeof(uuid_command),
37 .uuid = {}
38 };
39
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 = {
42 .cmd = LC_LOAD_DYLIB,
43 .cmdsize = sizeof(dylib_command) + sizeof(dylib_name),
44 .dylib = {
45 .name = {
46 .offset = sizeof(dylib_command)
47 }
48 }
49 };
50
51 static constexpr uuid_command mock_trap_command = {
52 .cmd = LC_UUID,
53 .cmdsize = 0x10000,
54 .uuid = {}
55 };
56
57 const char *start_load_cmd_addr;
58 size_t sizeofcmds;
59 std::vector<unsigned char> mock_header;
60
61public:
62 MemoryMappingLayoutMock(): MemoryMappingLayout(false) {
63 EXPECT_EQ(mock_uuid_command.cmdsize % 8, 0u);
64 EXPECT_EQ(mock_dylib_command.cmdsize % 8, 0u);
65
66 Reset();
67
68#ifdef MH_MAGIC_64
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);
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 = header_size + header->sizeofcmds +
76 mock_uuid_command.cmdsize + mock_dylib_command.cmdsize + sizeof(uuid_command);
77
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));
95
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));
100
101 start_load_cmd_addr = (const char *)(mock_header.data() + header_size);
102 sizeofcmds = header->sizeofcmds;
103
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
106 }
107
108 size_t SizeOfLoadCommands() {
109 return sizeofcmds;
110 }
111
112 size_t CurrentLoadCommandOffset() {
113 size_t offset = data_.current_load_cmd_addr - start_load_cmd_addr;
114 return offset;
115 }
116
117protected:
118 virtual ImageHeader *CurrentImageHeader() override {
119 return (ImageHeader *)mock_header.data();
120 }
121};
122
123TEST(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);
130 }
131 size_t final_offset = memory_mapping.CurrentLoadCommandOffset();
132 EXPECT_EQ(final_offset, size); // All commands processed, no more, no less
133}
134
135} // namespace __sanitizer
136
137# endif // SANITIZER_APPLE
138

source code of compiler-rt/lib/sanitizer_common/tests/sanitizer_procmaps_mac_test.cpp