1 //===-- CoreFileMemoryRangesTests.cpp
2 //---------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
13 #include "lldb/Target/CoreFileMemoryRanges.h"
14 #include "lldb/lldb-types.h"
16 using namespace lldb_private
;
18 TEST(CoreFileMemoryRangesTest
, MapOverlappingRanges
) {
19 lldb_private::CoreFileMemoryRanges ranges
;
20 const lldb::addr_t start_addr
= 0x1000;
21 const lldb::addr_t increment_addr
= 0x1000;
22 const size_t iterations
= 10;
23 for (size_t i
= 0; i
< iterations
; i
++) {
24 const lldb::addr_t start
= start_addr
+ (i
* increment_addr
);
25 const lldb::addr_t end
= start
+ increment_addr
;
27 const uint32_t permissions
= 0x3;
28 llvm::AddressRange
range(start
, end
);
29 const CoreFileMemoryRange core_range
= {range
, permissions
};
30 // The range data is Start, Size, While the range is start-end.
31 CoreFileMemoryRanges::Entry entry
= {start
, end
- start
, core_range
};
35 Status error
= ranges
.FinalizeCoreFileSaveRanges();
36 EXPECT_TRUE(error
.Success());
37 ASSERT_THAT(1, ranges
.GetSize());
38 const auto range
= ranges
.GetEntryAtIndex(0);
40 ASSERT_THAT(start_addr
, range
->GetRangeBase());
41 ASSERT_THAT(start_addr
+ (iterations
* increment_addr
), range
->GetRangeEnd());
44 TEST(CoreFileMemoryRangesTest
, RangesSplitByPermissions
) {
45 lldb_private::CoreFileMemoryRanges ranges
;
46 const lldb::addr_t start_addr
= 0x1000;
47 const lldb::addr_t increment_addr
= 0x1000;
48 const size_t iterations
= 10;
49 for (size_t i
= 0; i
< iterations
; i
++) {
50 const lldb::addr_t start
= start_addr
+ (i
* increment_addr
);
51 const lldb::addr_t end
= start
+ increment_addr
;
52 const uint32_t permissions
= i
;
53 llvm::AddressRange
range(start
, end
);
54 const CoreFileMemoryRange core_range
= {range
, permissions
};
55 // The range data is Start, Size, While the range is start-end.
56 CoreFileMemoryRanges::Entry entry
= {start
, end
- start
, core_range
};
60 Status error
= ranges
.FinalizeCoreFileSaveRanges();
61 EXPECT_TRUE(error
.Success());
62 ASSERT_THAT(10, ranges
.GetSize());
63 const auto range
= ranges
.GetEntryAtIndex(0);
65 ASSERT_THAT(start_addr
, range
->GetRangeBase());
66 ASSERT_THAT(start_addr
+ increment_addr
, range
->GetRangeEnd());
69 TEST(CoreFileMemoryRangesTest
, MapPartialOverlappingRanges
) {
70 lldb_private::CoreFileMemoryRanges ranges
;
71 const lldb::addr_t start_addr
= 0x1000;
72 const lldb::addr_t increment_addr
= 0x1000;
73 const size_t iterations
= 10;
74 for (size_t i
= 0; i
< iterations
; i
++) {
75 const lldb::addr_t start
= start_addr
+ (i
* increment_addr
);
76 const lldb::addr_t end
= start
+ increment_addr
;
78 const uint32_t permissions
= 0x3;
79 llvm::AddressRange
range(start
, end
);
80 const CoreFileMemoryRange core_range
= {range
, permissions
};
81 // The range data is Start, Size, While the range is start-end.
82 CoreFileMemoryRanges::Entry entry
= {start
, end
- start
, core_range
};
86 const lldb::addr_t unique_start
= 0x7fff0000;
87 const lldb::addr_t unique_end
= unique_start
+ increment_addr
;
88 llvm::AddressRange
range(unique_start
, unique_end
);
89 const uint32_t permissions
= 0x3;
90 const CoreFileMemoryRange core_range
= {range
, permissions
};
91 // The range data is Start, Size, While the range is start-end.
92 CoreFileMemoryRanges::Entry entry
= {unique_start
, unique_end
- unique_start
,
96 Status error
= ranges
.FinalizeCoreFileSaveRanges();
97 EXPECT_TRUE(error
.Success());
98 ASSERT_THAT(2, ranges
.GetSize());
99 const auto merged_range
= ranges
.GetEntryAtIndex(0);
100 ASSERT_TRUE(merged_range
);
101 ASSERT_THAT(start_addr
, merged_range
->GetRangeBase());
102 ASSERT_THAT(start_addr
+ (iterations
* increment_addr
),
103 merged_range
->GetRangeEnd());
104 const auto unique_range
= ranges
.GetEntryAtIndex(1);
105 ASSERT_TRUE(unique_range
);
106 ASSERT_THAT(unique_start
, unique_range
->GetRangeBase());
107 ASSERT_THAT(unique_end
, unique_range
->GetRangeEnd());
110 TEST(CoreFileMemoryRangesTest
, SuperiorAndInferiorRanges_SamePermissions
) {
111 lldb_private::CoreFileMemoryRanges ranges
;
112 const lldb::addr_t start_addr
= 0x1000;
113 const lldb::addr_t increment_addr
= 0x1000;
114 const lldb::addr_t superior_region_end
= start_addr
+ increment_addr
* 10;
115 llvm::AddressRange
range(start_addr
, superior_region_end
);
116 const CoreFileMemoryRange core_range
= {range
, 0x3};
117 CoreFileMemoryRanges::Entry entry
= {
118 start_addr
, superior_region_end
- start_addr
, core_range
};
119 ranges
.Append(entry
);
120 const lldb::addr_t inferior_region_end
= start_addr
+ increment_addr
;
121 llvm::AddressRange
inferior_range(start_addr
, inferior_region_end
);
122 const CoreFileMemoryRange inferior_core_range
= {inferior_range
, 0x3};
123 CoreFileMemoryRanges::Entry inferior_entry
= {
124 start_addr
, inferior_region_end
- start_addr
, inferior_core_range
};
125 ranges
.Append(inferior_entry
);
127 Status error
= ranges
.FinalizeCoreFileSaveRanges();
128 EXPECT_TRUE(error
.Success());
129 ASSERT_THAT(1, ranges
.GetSize());
130 const auto searched_range
= ranges
.GetEntryAtIndex(0);
131 ASSERT_TRUE(searched_range
);
132 ASSERT_THAT(start_addr
, searched_range
->GetRangeBase());
133 ASSERT_THAT(superior_region_end
, searched_range
->GetRangeEnd());
136 TEST(CoreFileMemoryRangesTest
, SuperiorAndInferiorRanges_DifferentPermissions
) {
137 lldb_private::CoreFileMemoryRanges ranges
;
138 const lldb::addr_t start_addr
= 0x1000;
139 const lldb::addr_t increment_addr
= 0x1000;
140 const lldb::addr_t superior_region_end
= start_addr
+ increment_addr
* 10;
141 llvm::AddressRange
range(start_addr
, superior_region_end
);
142 const CoreFileMemoryRange core_range
= {range
, 0x3};
143 CoreFileMemoryRanges::Entry entry
= {
144 start_addr
, superior_region_end
- start_addr
, core_range
};
145 ranges
.Append(entry
);
146 const lldb::addr_t inferior_region_end
= start_addr
+ increment_addr
;
147 llvm::AddressRange
inferior_range(start_addr
, inferior_region_end
);
148 const CoreFileMemoryRange inferior_core_range
= {inferior_range
, 0x4};
149 CoreFileMemoryRanges::Entry inferior_entry
= {
150 start_addr
, inferior_region_end
- start_addr
, inferior_core_range
};
151 ranges
.Append(inferior_entry
);
153 Status error
= ranges
.FinalizeCoreFileSaveRanges();
154 EXPECT_TRUE(error
.Fail());
157 TEST(CoreFileMemoryRangesTest
, NonIntersectingRangesSamePermissions
) {
158 const int permissions
= 0x7;
159 lldb_private::CoreFileMemoryRanges ranges
;
160 const lldb::addr_t region_one_start
= 0x1000;
161 const lldb::addr_t region_one_end
= 0x2000;
162 llvm::AddressRange
range_one(region_one_start
, region_one_end
);
163 const CoreFileMemoryRange core_range_one
= {range_one
, permissions
};
164 CoreFileMemoryRanges::Entry entry_one
= {
165 region_one_start
, region_one_end
- region_one_start
, core_range_one
};
166 ranges
.Append(entry_one
);
167 const lldb::addr_t region_two_start
= 0xb000;
168 const lldb::addr_t region_two_end
= 0xc000;
169 llvm::AddressRange
range_two(region_two_start
, region_two_end
);
170 const CoreFileMemoryRange core_range_two
= {range_two
, permissions
};
171 CoreFileMemoryRanges::Entry entry_two
= {
172 region_two_start
, region_two_end
- region_two_start
, core_range_two
};
173 ranges
.Append(entry_two
);
175 Status error
= ranges
.FinalizeCoreFileSaveRanges();
176 EXPECT_TRUE(error
.Success());
177 ASSERT_THAT(2UL, ranges
.GetSize());
178 ASSERT_THAT(region_one_start
, ranges
.GetEntryAtIndex(0)->GetRangeBase());
179 ASSERT_THAT(region_two_start
, ranges
.GetEntryAtIndex(1)->GetRangeBase());
182 TEST(CoreFileMemoryRangesTest
, PartialOverlapping
) {
183 const int permissions
= 0x3;
184 lldb_private::CoreFileMemoryRanges ranges
;
185 const lldb::addr_t start_addr
= 0x1000;
186 const lldb::addr_t end_addr
= 0x2000;
187 llvm::AddressRange
range_one(start_addr
, end_addr
);
188 const CoreFileMemoryRange core_range_one
= {range_one
, permissions
};
189 CoreFileMemoryRanges::Entry entry_one
= {start_addr
, end_addr
- start_addr
,
191 llvm::AddressRange
range_two(start_addr
/ 2, end_addr
/ 2);
192 const CoreFileMemoryRange core_range_two
= {range_two
, permissions
};
193 CoreFileMemoryRanges::Entry entry_two
= {
194 start_addr
/ 2, end_addr
/ 2 - start_addr
/ 2, core_range_two
};
195 ranges
.Append(entry_one
);
196 ranges
.Append(entry_two
);
198 Status error
= ranges
.FinalizeCoreFileSaveRanges();
199 EXPECT_TRUE(error
.Success());
200 ASSERT_THAT(1, ranges
.GetSize());
201 const auto searched_range
= ranges
.GetEntryAtIndex(0);
202 ASSERT_TRUE(searched_range
);
203 ASSERT_THAT(start_addr
/ 2, searched_range
->GetRangeBase());
204 ASSERT_THAT(end_addr
, searched_range
->GetRangeEnd());