ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "address_mapper.h"
 
#include <algorithm>
#include <memory>
 
#include "base/logging.h"
#include "base/macros.h"
 
#include "compat/test.h"
 
namespace quipper {
 
namespace {
 
struct Range {
  uint64_t addr;
  uint64_t size;
  uint64_t id;
  uint64_t base_offset;
 
  Range() : addr(0), size(0), id(0), base_offset(0) {}
 
  Range(uint64_t addr, uint64_t size, uint64_t id, uint64_t base_offset)
      : addr(addr), size(size), id(id), base_offset(base_offset) {}
 
  bool contains(const uint64_t check_addr) const {
    return (check_addr >= addr && check_addr < addr + size);
  }
};
 
// Some address ranges to map.  It is important that none of these overlap with
// each other, nor are any two of them contiguous.
const Range kMapRanges[] = {
    Range(0xff000000, 0x100000, 0xdeadbeef, 0),
    Range(0x00a00000, 0x10000, 0xcafebabe, 0),
    Range(0x0c000000, 0x1000000, 0x900df00d, 0),
    Range(0x00001000, 0x30000, 0x9000091e, 0),
};
 
// List of real addresses that are not in the above ranges.
const uint64_t kAddressesNotInRanges[] = {
    0x00000000, 0x00000100, 0x00038000, 0x00088888, 0x00100000, 0x004fffff,
    0x00a20000, 0x00cc0000, 0x00ffffff, 0x03e00000, 0x0b000000, 0x0d100000,
    0x0fffffff, 0x1fffffff, 0x7ffffff0, 0xdffffff0, 0xfe000000, 0xffffffff,
};
 
// Number of regularly-spaced intervals within a mapped range to test.
const int kNumRangeTestIntervals = 8;
 
// A simple test function to convert a real address to a mapped address.
// Address ranges in |ranges| are mapped starting at address 0.
uint64_t GetMappedAddressFromRanges(const Range* ranges,
                                    const unsigned int num_ranges,
                                    const uint64_t addr) {
  unsigned int i;
  uint64_t mapped_range_addr;
  for (i = 0, mapped_range_addr = 0; i < num_ranges;
       mapped_range_addr += ranges[i].size, ++i) {
    const Range& range = ranges[i];
    if (range.contains(addr)) return (addr - range.addr) + mapped_range_addr;
  }
  return static_cast<uint64_t>(-1);
}
 
}  // namespace
 
// The unit test class for AddressMapper.
class AddressMapperTest : public ::testing::Test {
 public:
  AddressMapperTest() {}
  ~AddressMapperTest() {}
 
  virtual void SetUp() { mapper_.reset(new AddressMapper); }
 
 protected:
  // Maps a range using the AddressMapper and makes sure that it was successful.
  // Uses all fields of |range|, including id and base_offset.
  bool MapRange(const Range& range, bool remove_old_mappings) {
    VLOG(1) << "Mapping range at " << std::hex << range.addr
            << " with length of " << range.size << " and id " << range.id;
    return mapper_->MapWithID(range.addr, range.size, range.id,
                              range.base_offset, remove_old_mappings);
  }
 
  // Tests a range that has been mapped. |expected_mapped_addr| is the starting
  // address that it should have been mapped to. This mapper will test the start
  // and end addresses of the range, as well as a bunch of addresses inside it.
  // Also checks lookup of ID and offset.
  void TestMappedRange(const Range& range, uint64_t expected_mapped_addr) {
    uint64_t mapped_addr = UINT64_MAX;
    AddressMapper::MappingList::const_iterator addr_iter;
 
    VLOG(1) << "Testing range at " << std::hex << range.addr
            << " with length of " << std::hex << range.size;
 
    // Check address at the beginning of the range and at subsequent intervals.
    for (int i = 0; i < kNumRangeTestIntervals; ++i) {
      const uint64_t offset = i * (range.size / kNumRangeTestIntervals);
      uint64_t addr = range.addr + offset;
      EXPECT_TRUE(mapper_->GetMappedAddressAndListIterator(addr, &mapped_addr,
                                                           &addr_iter));
      EXPECT_EQ(expected_mapped_addr + offset, mapped_addr);
 
      uint64_t mapped_offset;
      uint64_t mapped_id;
      mapper_->GetMappedIDAndOffset(addr, addr_iter, &mapped_id,
                                    &mapped_offset);
      EXPECT_EQ(range.base_offset + offset, mapped_offset);
      EXPECT_EQ(range.id, mapped_id);
    }
 
    // Check address at end of the range.
    EXPECT_TRUE(mapper_->GetMappedAddressAndListIterator(
        range.addr + range.size - 1, &mapped_addr, &addr_iter));
    EXPECT_EQ(expected_mapped_addr + range.size - 1, mapped_addr);
  }
 
  std::unique_ptr<AddressMapper> mapper_;
};
 
// Map one range at a time and test looking up addresses.
TEST_F(AddressMapperTest, MapSingle) {
  for (const Range& range : kMapRanges) {
    mapper_.reset(new AddressMapper);
    ASSERT_TRUE(MapRange(range, false));
    EXPECT_EQ(1U, mapper_->GetNumMappedRanges());
    TestMappedRange(range, 0);
 
    // Check addresses before the mapped range, should be invalid.
    uint64_t mapped_addr;
    AddressMapper::MappingList::const_iterator iter;
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(range.addr - 1,
                                                          &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(range.addr - 0x100,
                                                          &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(
        range.addr + range.size, &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(
        range.addr + range.size + 0x100, &mapped_addr, &iter));
    EXPECT_EQ(range.size, mapper_->GetMaxMappedLength());
  }
}
 
// Map all the ranges at once and test looking up addresses.
TEST_F(AddressMapperTest, MapAll) {
  uint64_t size_mapped = 0;
  for (const Range& range : kMapRanges) {
    ASSERT_TRUE(MapRange(range, false));
    size_mapped += range.size;
  }
  EXPECT_EQ(arraysize(kMapRanges), mapper_->GetNumMappedRanges());
 
  // Check the max mapped length in quipper space.
  EXPECT_EQ(size_mapped, mapper_->GetMaxMappedLength());
 
  // For each mapped range, test addresses at the start, middle, and end.
  // Also test the address right before and after each range.
  uint64_t mapped_addr;
  AddressMapper::MappingList::const_iterator iter;
  for (const Range& range : kMapRanges) {
    TestMappedRange(range, GetMappedAddressFromRanges(
                               kMapRanges, arraysize(kMapRanges), range.addr));
 
    // Check addresses before and after the mapped range, should be invalid.
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(range.addr - 1,
                                                          &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(range.addr - 0x100,
                                                          &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(
        range.addr + range.size, &mapped_addr, &iter));
    EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(
        range.addr + range.size + 0x100, &mapped_addr, &iter));
  }
 
  // Test some addresses that are out of these ranges, should not be able to
  // get mapped addresses.
  for (uint64_t addr : kAddressesNotInRanges)
    EXPECT_FALSE(
        mapper_->GetMappedAddressAndListIterator(addr, &mapped_addr, &iter));
}
 
// Map all the ranges at once and test looking up IDs and offsets.
TEST_F(AddressMapperTest, MapAllWithIDsAndOffsets) {
  for (const Range& range : kMapRanges) {
    LOG(INFO) << "Mapping range at " << std::hex << range.addr
              << " with length of " << std::hex << range.size;
    ASSERT_TRUE(mapper_->MapWithID(range.addr, range.size, range.id, 0, false));
  }
  EXPECT_EQ(arraysize(kMapRanges), mapper_->GetNumMappedRanges());
 
  // For each mapped range, test addresses at the start, middle, and end.
  // Also test the address right before and after each range.
  for (const Range& range : kMapRanges) {
    TestMappedRange(range, GetMappedAddressFromRanges(
                               kMapRanges, arraysize(kMapRanges), range.addr));
  }
}
 
// Test overlap detection.
TEST_F(AddressMapperTest, OverlapSimple) {
  // Map all the ranges first.
  for (const Range& range : kMapRanges) ASSERT_TRUE(MapRange(range, false));
 
  // Attempt to re-map each range, but offset by size / 2.
  for (const Range& range : kMapRanges) {
    Range new_range;
    new_range.addr = range.addr + range.size / 2;
    new_range.size = range.size;
    // The maps should fail because of overlap with an existing mapping.
    EXPECT_FALSE(MapRange(new_range, false));
  }
 
  // Re-map each range with the same offset.  Only this time, remove any old
  // mapped range that overlaps with it.
  for (const Range& range : kMapRanges) {
    Range new_range;
    new_range.addr = range.addr + range.size / 2;
    new_range.size = range.size;
    EXPECT_TRUE(MapRange(new_range, true));
    // Make sure the number of ranges is unchanged (one deleted, one added).
    EXPECT_EQ(arraysize(kMapRanges), mapper_->GetNumMappedRanges());
 
    // The range is shifted in real space but should still be the same in
    // quipper space.
    TestMappedRange(
        new_range, GetMappedAddressFromRanges(kMapRanges, arraysize(kMapRanges),
                                              range.addr));
  }
}
 
// Test mapping of a giant map that overlaps with all existing ranges.
TEST_F(AddressMapperTest, OverlapBig) {
  // A huge region that overlaps with all ranges in |kMapRanges|.
  const Range kBigRegion(0xa00, 0xff000000, 0x1234, 0);
 
  // Map all the ranges first.
  for (const Range& range : kMapRanges) ASSERT_TRUE(MapRange(range, false));
 
  // Make sure overlap is detected before removing old ranges.
  ASSERT_FALSE(MapRange(kBigRegion, false));
  ASSERT_TRUE(MapRange(kBigRegion, true));
  EXPECT_EQ(1U, mapper_->GetNumMappedRanges());
 
  TestMappedRange(kBigRegion, 0);
 
  // Given the list of previously unmapped addresses, test that the ones within
  // |kBigRegion| are now mapped; for the ones that are not, test that they are
  // not mapped.
  for (uint64_t addr : kAddressesNotInRanges) {
    uint64_t mapped_addr = UINT64_MAX;
    AddressMapper::MappingList::const_iterator addr_iter;
    bool map_success = mapper_->GetMappedAddressAndListIterator(
        addr, &mapped_addr, &addr_iter);
    if (kBigRegion.contains(addr)) {
      EXPECT_TRUE(map_success);
      EXPECT_EQ(addr - kBigRegion.addr, mapped_addr);
    } else {
      EXPECT_FALSE(map_success);
    }
  }
 
  // Check that addresses in the originally mapped ranges no longer map to the
  // same addresses if they fall within |kBigRegion|, and don't map at all if
  // they are not within |kBigRegion|.
  for (const Range& range : kMapRanges) {
    for (uint64_t addr = range.addr; addr < range.addr + range.size;
         addr += range.size / kNumRangeTestIntervals) {
      uint64_t mapped_addr = UINT64_MAX;
      AddressMapper::MappingList::const_iterator addr_iter;
      bool map_success = mapper_->GetMappedAddressAndListIterator(
          addr, &mapped_addr, &addr_iter);
      if (kBigRegion.contains(addr)) {
        EXPECT_TRUE(map_success);
        EXPECT_EQ(addr - kBigRegion.addr, mapped_addr);
      } else {
        EXPECT_FALSE(map_success);
      }
    }
  }
 
  EXPECT_EQ(kBigRegion.size, mapper_->GetMaxMappedLength());
}
 
// Test a mapping at the end of memory space.
TEST_F(AddressMapperTest, EndOfMemory) {
  // A region that extends to the end of the address space.
  const Range kEndRegion(0xffffffff00000000, 0x100000000, 0x3456, 0);
 
  ASSERT_TRUE(MapRange(kEndRegion, true));
  EXPECT_EQ(1U, mapper_->GetNumMappedRanges());
  TestMappedRange(kEndRegion, 0);
}
 
// Test mapping of an out-of-bounds mapping.
TEST_F(AddressMapperTest, OutOfBounds) {
  // A region toward the end of address space that overruns the end of the
  // address space.
  const Range kOutOfBoundsRegion(0xffffffff00000000, 0x00000000, 0xccddeeff, 0);
 
  ASSERT_FALSE(MapRange(kOutOfBoundsRegion, false));
  ASSERT_FALSE(MapRange(kOutOfBoundsRegion, true));
  EXPECT_EQ(0, mapper_->GetNumMappedRanges());
  uint64_t mapped_addr;
  AddressMapper::MappingList::const_iterator iter;
  EXPECT_FALSE(mapper_->GetMappedAddressAndListIterator(
      kOutOfBoundsRegion.addr + 0x100, &mapped_addr, &iter));
}
 
// Test mapping of a region that covers the entire memory space.  Then map other
// regions over it.
TEST_F(AddressMapperTest, FullRange) {
  // A huge region that covers all of the available space.
  const Range kFullRegion(0, UINT64_MAX, 0xaabbccdd, 0);
 
  ASSERT_TRUE(MapRange(kFullRegion, false));
  size_t num_expected_ranges = 1;
  EXPECT_EQ(num_expected_ranges, mapper_->GetNumMappedRanges());
 
  TestMappedRange(kFullRegion, 0);
 
  // Map some smaller ranges.
  for (const Range& range : kMapRanges) {
    // Check for collision first.
    ASSERT_FALSE(MapRange(range, false));
    ASSERT_TRUE(MapRange(range, true));
 
    // Make sure the number of mapped ranges has increased by two.  The mapping
    // should have split an existing range.
    num_expected_ranges += 2;
    EXPECT_EQ(num_expected_ranges, mapper_->GetNumMappedRanges());
  }
}
 
// Test mapping of one range in the middle of an existing range. The existing
// range should be split into two to accommodate it. Also tests the use of base
// offsets.
TEST_F(AddressMapperTest, SplitRangeWithOffsetBase) {
  // Define the two ranges, with distinct IDs.
  const Range kFirstRange(0x10000, 0x4000, 0x11223344, 0x5000);
  const Range kSecondRange(0x12000, 0x1000, 0x55667788, 0);
 
  // As a sanity check, make sure the second range is fully contained within the
  // first range.
  EXPECT_LT(kFirstRange.addr, kSecondRange.addr);
  EXPECT_GT(kFirstRange.addr + kFirstRange.size,
            kSecondRange.addr + kSecondRange.size);
 
  // Map the two ranges.
  ASSERT_TRUE(MapRange(kFirstRange, true));
  ASSERT_TRUE(MapRange(kSecondRange, true));
 
  // The first range should have been split into two parts to make way for the
  // second range. There should be a total of three ranges.
  EXPECT_EQ(3U, mapper_->GetNumMappedRanges());
 
  // Now make sure the mappings are correct.
 
  // The first range has been split up. Define the expected ranges here.
  const Range kFirstRangeHead(0x10000, 0x2000, kFirstRange.id, 0x5000);
  const Range kFirstRangeTail(0x13000, 0x1000, kFirstRange.id, 0x8000);
 
  // Test the two remaining parts of the first range.
  TestMappedRange(kFirstRangeHead, 0);
  TestMappedRange(kFirstRangeTail, kFirstRangeTail.addr - kFirstRangeHead.addr);
 
  // Test the second range normally.
  TestMappedRange(kSecondRange, kSecondRange.addr - kFirstRange.addr);
}
 
// Test mappings that are not aligned to mmap page boundaries.
TEST_F(AddressMapperTest, NotPageAligned) {
  mapper_->set_page_alignment(0x1000);
 
  // Some address ranges that do not begin on a page boundary.
  const Range kUnalignedRanges[] = {
      Range(0xff000100, 0x1fff00, 0xdeadbeef, 0x100),
      Range(0x00a00180, 0x10000, 0xcafebabe, 0x180),
      Range(0x0c000300, 0x1000800, 0x900df00d, 0x4300),
      Range(0x000017f0, 0x30000, 0x9000091e, 0x7f0),
  };
 
  // Map the ranges.
  for (const Range& range : kUnalignedRanges)
    ASSERT_TRUE(MapRange(range, true));
 
  EXPECT_EQ(4U, mapper_->GetNumMappedRanges());
 
  // Now make sure the mappings are correct.
 
  // First region is mapped as usual, except it does not start at the page
  // boundary.
  TestMappedRange(kUnalignedRanges[0], 0x00000100);
 
  // Second region follows at the end of the first, but notice that its length
  // is a full number of pages, which means...
  TestMappedRange(kUnalignedRanges[1], 0x00200180);
 
  // ... the third region starts beyond the next page boundary: 0x211000 instead
  // of 0x210000.
  TestMappedRange(kUnalignedRanges[2], 0x00211300);
 
  // Similarly, the fourth region starts beyond the next page boundary:
  // 0x1212000 instead of 0x1211000.
  TestMappedRange(kUnalignedRanges[3], 0x012127f0);
}
 
// Have one mapping in the middle of another, with a nonzero page alignment
// parameter.
TEST_F(AddressMapperTest, SplitRangeWithPageAlignment) {
  mapper_->set_page_alignment(0x1000);
 
  // These should map just fine.
  const Range kRange0(0x3000, 0x8000, 0xdeadbeef, 0);
  const Range kRange1(0x5000, 0x2000, 0xfeedbabe, 0);
 
  EXPECT_TRUE(MapRange(kRange0, true));
  EXPECT_TRUE(MapRange(kRange1, true));
 
  EXPECT_EQ(3U, mapper_->GetNumMappedRanges());
 
  // Determine the expected split mappings.
  const Range kRange0Head(0x3000, 0x2000, 0xdeadbeef, 0);
  const Range kRange0Tail(0x7000, 0x4000, 0xdeadbeef, 0x4000);
 
  // Everything should be mapped and split as usual.
  TestMappedRange(kRange0Head, 0);
  TestMappedRange(kRange0Tail, 0x4000);
  TestMappedRange(kRange1, 0x2000);
}
 
// Have one mapping in the middle of another, with a nonzero page alignment
// parameter. The overlapping region will not be aligned to page boundaries.
TEST_F(AddressMapperTest, MisalignedSplitRangeWithPageAlignment) {
  mapper_->set_page_alignment(0x1000);
 
  const Range kRange0(0x3000, 0x8000, 0xdeadbeef, 0);
  const Range kMisalignedRange(0x4800, 0x2000, 0xfeedbabe, 0);
 
  EXPECT_TRUE(MapRange(kRange0, true));
  // The misaligned mapping should not find enough space to split the existing
  // range. It is not allowed to move the existing mapping.
  EXPECT_FALSE(MapRange(kMisalignedRange, true));
}
 
}  // namespace quipper