summaryrefslogtreecommitdiffstats
path: root/runtime/mem_map_test.cc
blob: f635b5d62f29ea2ddaabed3a74e122a2aeadd36e (plain)
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
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mem_map.h"

#include <memory>

#include <valgrind.h>

#include "gtest/gtest.h"

namespace art {

class MemMapTest : public testing::Test {
 public:
  static uint8_t* BaseBegin(MemMap* mem_map) {
    return reinterpret_cast<uint8_t*>(mem_map->base_begin_);
  }
  static size_t BaseSize(MemMap* mem_map) {
    return mem_map->base_size_;
  }

  static void RemapAtEndTest(bool low_4gb) {
    std::string error_msg;
    // Cast the page size to size_t.
    const size_t page_size = static_cast<size_t>(kPageSize);
    // Map a two-page memory region.
    MemMap* m0 = MemMap::MapAnonymous("MemMapTest_RemapAtEndTest_map0",
                                      nullptr,
                                      2 * page_size,
                                      PROT_READ | PROT_WRITE,
                                      low_4gb,
                                      false,
                                      &error_msg);
    // Check its state and write to it.
    uint8_t* base0 = m0->Begin();
    ASSERT_TRUE(base0 != nullptr) << error_msg;
    size_t size0 = m0->Size();
    EXPECT_EQ(m0->Size(), 2 * page_size);
    EXPECT_EQ(BaseBegin(m0), base0);
    EXPECT_EQ(BaseSize(m0), size0);
    memset(base0, 42, 2 * page_size);
    // Remap the latter half into a second MemMap.
    MemMap* m1 = m0->RemapAtEnd(base0 + page_size,
                                "MemMapTest_RemapAtEndTest_map1",
                                PROT_READ | PROT_WRITE,
                                &error_msg);
    // Check the states of the two maps.
    EXPECT_EQ(m0->Begin(), base0) << error_msg;
    EXPECT_EQ(m0->Size(), page_size);
    EXPECT_EQ(BaseBegin(m0), base0);
    EXPECT_EQ(BaseSize(m0), page_size);
    uint8_t* base1 = m1->Begin();
    size_t size1 = m1->Size();
    EXPECT_EQ(base1, base0 + page_size);
    EXPECT_EQ(size1, page_size);
    EXPECT_EQ(BaseBegin(m1), base1);
    EXPECT_EQ(BaseSize(m1), size1);
    // Write to the second region.
    memset(base1, 43, page_size);
    // Check the contents of the two regions.
    for (size_t i = 0; i < page_size; ++i) {
      EXPECT_EQ(base0[i], 42);
    }
    for (size_t i = 0; i < page_size; ++i) {
      EXPECT_EQ(base1[i], 43);
    }
    // Unmap the first region.
    delete m0;
    // Make sure the second region is still accessible after the first
    // region is unmapped.
    for (size_t i = 0; i < page_size; ++i) {
      EXPECT_EQ(base1[i], 43);
    }
    delete m1;
  }

  void CommonInit() {
    MemMap::Init();
  }

#if defined(__LP64__) && !defined(__x86_64__)
  static uintptr_t GetLinearScanPos() {
    return MemMap::next_mem_pos_;
  }
#endif
};

#if defined(__LP64__) && !defined(__x86_64__)

#ifdef __BIONIC__
extern uintptr_t CreateStartPos(uint64_t input);
#endif

TEST_F(MemMapTest, Start) {
  CommonInit();
  uintptr_t start = GetLinearScanPos();
  EXPECT_LE(64 * KB, start);
  EXPECT_LT(start, static_cast<uintptr_t>(ART_BASE_ADDRESS));
#ifdef __BIONIC__
  // Test a couple of values. Make sure they are different.
  uintptr_t last = 0;
  for (size_t i = 0; i < 100; ++i) {
    uintptr_t random_start = CreateStartPos(i * kPageSize);
    EXPECT_NE(last, random_start);
    last = random_start;
  }

  // Even on max, should be below ART_BASE_ADDRESS.
  EXPECT_LT(CreateStartPos(~0), static_cast<uintptr_t>(ART_BASE_ADDRESS));
#endif
  // End of test.
}
#endif

TEST_F(MemMapTest, MapAnonymousEmpty) {
  CommonInit();
  std::string error_msg;
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty",
                                                   nullptr,
                                                   0,
                                                   PROT_READ,
                                                   false,
                                                   false,
                                                   &error_msg));
  ASSERT_TRUE(map.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  map.reset(MemMap::MapAnonymous("MapAnonymousEmpty",
                                 nullptr,
                                 kPageSize,
                                 PROT_READ | PROT_WRITE,
                                 false,
                                 false,
                                 &error_msg));
  ASSERT_TRUE(map.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
}

#ifdef __LP64__
TEST_F(MemMapTest, MapAnonymousEmpty32bit) {
  CommonInit();
  std::string error_msg;
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty",
                                                   nullptr,
                                                   kPageSize,
                                                   PROT_READ | PROT_WRITE,
                                                   true,
                                                   false,
                                                   &error_msg));
  ASSERT_TRUE(map.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  ASSERT_LT(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), 1ULL << 32);
}
#endif

TEST_F(MemMapTest, MapAnonymousExactAddr) {
  CommonInit();
  std::string error_msg;
  // Map at an address that should work, which should succeed.
  std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0",
                                                    reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map0.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  ASSERT_TRUE(map0->BaseBegin() == reinterpret_cast<void*>(ART_BASE_ADDRESS));
  // Map at an unspecified address, which should succeed.
  std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1",
                                                    nullptr,
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map1.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  ASSERT_TRUE(map1->BaseBegin() != nullptr);
  // Attempt to map at the same address, which should fail.
  std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2",
                                                    reinterpret_cast<uint8_t*>(map1->BaseBegin()),
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map2.get() == nullptr) << error_msg;
  ASSERT_TRUE(!error_msg.empty());
}

TEST_F(MemMapTest, RemapAtEnd) {
  RemapAtEndTest(false);
}

#ifdef __LP64__
TEST_F(MemMapTest, RemapAtEnd32bit) {
  RemapAtEndTest(true);
}
#endif

TEST_F(MemMapTest, MapAnonymousExactAddr32bitHighAddr) {
  CommonInit();
  // This test may not work under valgrind.
  if (RUNNING_ON_VALGRIND == 0) {
    uintptr_t start_addr = ART_BASE_ADDRESS + 0x1000000;
    std::string error_msg;
    std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr",
                                                     reinterpret_cast<uint8_t*>(start_addr),
                                                     0x21000000,
                                                     PROT_READ | PROT_WRITE,
                                                     true,
                                                     false,
                                                     &error_msg));
    ASSERT_TRUE(map.get() != nullptr) << error_msg;
    ASSERT_TRUE(error_msg.empty());
    ASSERT_EQ(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), start_addr);
  }
}

TEST_F(MemMapTest, MapAnonymousOverflow) {
  CommonInit();
  std::string error_msg;
  uintptr_t ptr = 0;
  ptr -= kPageSize;  // Now it's close to the top.
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousOverflow",
                                                   reinterpret_cast<uint8_t*>(ptr),
                                                   2 * kPageSize,  // brings it over the top.
                                                   PROT_READ | PROT_WRITE,
                                                   false,
                                                   false,
                                                   &error_msg));
  ASSERT_EQ(nullptr, map.get());
  ASSERT_FALSE(error_msg.empty());
}

#ifdef __LP64__
TEST_F(MemMapTest, MapAnonymousLow4GBExpectedTooHigh) {
  CommonInit();
  std::string error_msg;
  std::unique_ptr<MemMap> map(
      MemMap::MapAnonymous("MapAnonymousLow4GBExpectedTooHigh",
                           reinterpret_cast<uint8_t*>(UINT64_C(0x100000000)),
                           kPageSize,
                           PROT_READ | PROT_WRITE,
                           true,
                           false,
                           &error_msg));
  ASSERT_EQ(nullptr, map.get());
  ASSERT_FALSE(error_msg.empty());
}

TEST_F(MemMapTest, MapAnonymousLow4GBRangeTooHigh) {
  CommonInit();
  std::string error_msg;
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousLow4GBRangeTooHigh",
                                                   reinterpret_cast<uint8_t*>(0xF0000000),
                                                   0x20000000,
                                                   PROT_READ | PROT_WRITE,
                                                   true,
                                                   false,
                                                   &error_msg));
  ASSERT_EQ(nullptr, map.get());
  ASSERT_FALSE(error_msg.empty());
}
#endif

TEST_F(MemMapTest, MapAnonymousReuse) {
  CommonInit();
  std::string error_msg;
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousReserve",
                                                   nullptr,
                                                   0x20000,
                                                   PROT_READ | PROT_WRITE,
                                                   false,
                                                   false,
                                                   &error_msg));
  ASSERT_NE(nullptr, map.get());
  ASSERT_TRUE(error_msg.empty());
  std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymousReused",
                                                    reinterpret_cast<uint8_t*>(map->BaseBegin()),
                                                    0x10000,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    true,
                                                    &error_msg));
  ASSERT_NE(nullptr, map2.get());
  ASSERT_TRUE(error_msg.empty());
}

TEST_F(MemMapTest, CheckNoGaps) {
  CommonInit();
  std::string error_msg;
  constexpr size_t kNumPages = 3;
  // Map a 3-page mem map.
  std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymous0",
                                                   nullptr,
                                                   kPageSize * kNumPages,
                                                   PROT_READ | PROT_WRITE,
                                                   false,
                                                   false,
                                                   &error_msg));
  ASSERT_TRUE(map.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  // Record the base address.
  uint8_t* map_base = reinterpret_cast<uint8_t*>(map->BaseBegin());
  // Unmap it.
  map.reset();

  // Map at the same address, but in page-sized separate mem maps,
  // assuming the space at the address is still available.
  std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0",
                                                    map_base,
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map0.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1",
                                                    map_base + kPageSize,
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map1.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2",
                                                    map_base + kPageSize * 2,
                                                    kPageSize,
                                                    PROT_READ | PROT_WRITE,
                                                    false,
                                                    false,
                                                    &error_msg));
  ASSERT_TRUE(map2.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());

  // One-map cases.
  ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map0.get()));
  ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map1.get()));
  ASSERT_TRUE(MemMap::CheckNoGaps(map2.get(), map2.get()));

  // Two or three-map cases.
  ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map1.get()));
  ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map2.get()));
  ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map2.get()));

  // Unmap the middle one.
  map1.reset();

  // Should return false now that there's a gap in the middle.
  ASSERT_FALSE(MemMap::CheckNoGaps(map0.get(), map2.get()));
}

}  // namespace art