Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mem_map.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 21 | #include "common_runtime_test.h" |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 22 | #include "base/memory_tool.h" |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 27 | class MemMapTest : public CommonRuntimeTest { |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 28 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 29 | static uint8_t* BaseBegin(MemMap* mem_map) { |
| 30 | return reinterpret_cast<uint8_t*>(mem_map->base_begin_); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 31 | } |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 32 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 33 | static size_t BaseSize(MemMap* mem_map) { |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 34 | return mem_map->base_size_; |
| 35 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 36 | |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 37 | static uint8_t* GetValidMapAddress(size_t size, bool low_4gb) { |
| 38 | // Find a valid map address and unmap it before returning. |
| 39 | std::string error_msg; |
| 40 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("temp", |
| 41 | nullptr, |
| 42 | size, |
| 43 | PROT_READ, |
| 44 | low_4gb, |
| 45 | false, |
| 46 | &error_msg)); |
| 47 | CHECK(map != nullptr); |
| 48 | return map->Begin(); |
| 49 | } |
| 50 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 51 | static void RemapAtEndTest(bool low_4gb) { |
| 52 | std::string error_msg; |
| 53 | // Cast the page size to size_t. |
| 54 | const size_t page_size = static_cast<size_t>(kPageSize); |
| 55 | // Map a two-page memory region. |
| 56 | MemMap* m0 = MemMap::MapAnonymous("MemMapTest_RemapAtEndTest_map0", |
| 57 | nullptr, |
| 58 | 2 * page_size, |
| 59 | PROT_READ | PROT_WRITE, |
| 60 | low_4gb, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 61 | false, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 62 | &error_msg); |
| 63 | // Check its state and write to it. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 64 | uint8_t* base0 = m0->Begin(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | ASSERT_TRUE(base0 != nullptr) << error_msg; |
| 66 | size_t size0 = m0->Size(); |
| 67 | EXPECT_EQ(m0->Size(), 2 * page_size); |
| 68 | EXPECT_EQ(BaseBegin(m0), base0); |
| 69 | EXPECT_EQ(BaseSize(m0), size0); |
| 70 | memset(base0, 42, 2 * page_size); |
| 71 | // Remap the latter half into a second MemMap. |
| 72 | MemMap* m1 = m0->RemapAtEnd(base0 + page_size, |
| 73 | "MemMapTest_RemapAtEndTest_map1", |
| 74 | PROT_READ | PROT_WRITE, |
| 75 | &error_msg); |
| 76 | // Check the states of the two maps. |
| 77 | EXPECT_EQ(m0->Begin(), base0) << error_msg; |
| 78 | EXPECT_EQ(m0->Size(), page_size); |
| 79 | EXPECT_EQ(BaseBegin(m0), base0); |
| 80 | EXPECT_EQ(BaseSize(m0), page_size); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 81 | uint8_t* base1 = m1->Begin(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 82 | size_t size1 = m1->Size(); |
| 83 | EXPECT_EQ(base1, base0 + page_size); |
| 84 | EXPECT_EQ(size1, page_size); |
| 85 | EXPECT_EQ(BaseBegin(m1), base1); |
| 86 | EXPECT_EQ(BaseSize(m1), size1); |
| 87 | // Write to the second region. |
| 88 | memset(base1, 43, page_size); |
| 89 | // Check the contents of the two regions. |
| 90 | for (size_t i = 0; i < page_size; ++i) { |
| 91 | EXPECT_EQ(base0[i], 42); |
| 92 | } |
| 93 | for (size_t i = 0; i < page_size; ++i) { |
| 94 | EXPECT_EQ(base1[i], 43); |
| 95 | } |
| 96 | // Unmap the first region. |
| 97 | delete m0; |
| 98 | // Make sure the second region is still accessible after the first |
| 99 | // region is unmapped. |
| 100 | for (size_t i = 0; i < page_size; ++i) { |
| 101 | EXPECT_EQ(base1[i], 43); |
| 102 | } |
| 103 | delete m1; |
| 104 | } |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 105 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 106 | void CommonInit() { |
| 107 | MemMap::Init(); |
| 108 | } |
| 109 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 110 | #if defined(__LP64__) && !defined(__x86_64__) |
| 111 | static uintptr_t GetLinearScanPos() { |
| 112 | return MemMap::next_mem_pos_; |
| 113 | } |
| 114 | #endif |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 115 | }; |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 116 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 117 | #if defined(__LP64__) && !defined(__x86_64__) |
| 118 | |
| 119 | #ifdef __BIONIC__ |
| 120 | extern uintptr_t CreateStartPos(uint64_t input); |
| 121 | #endif |
| 122 | |
| 123 | TEST_F(MemMapTest, Start) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 124 | CommonInit(); |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 125 | uintptr_t start = GetLinearScanPos(); |
| 126 | EXPECT_LE(64 * KB, start); |
| 127 | EXPECT_LT(start, static_cast<uintptr_t>(ART_BASE_ADDRESS)); |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 128 | #ifdef __BIONIC__ |
| 129 | // Test a couple of values. Make sure they are different. |
| 130 | uintptr_t last = 0; |
| 131 | for (size_t i = 0; i < 100; ++i) { |
| 132 | uintptr_t random_start = CreateStartPos(i * kPageSize); |
| 133 | EXPECT_NE(last, random_start); |
| 134 | last = random_start; |
| 135 | } |
| 136 | |
| 137 | // Even on max, should be below ART_BASE_ADDRESS. |
| 138 | EXPECT_LT(CreateStartPos(~0), static_cast<uintptr_t>(ART_BASE_ADDRESS)); |
| 139 | #endif |
| 140 | // End of test. |
| 141 | } |
| 142 | #endif |
| 143 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 144 | TEST_F(MemMapTest, MapAnonymousEmpty) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 145 | CommonInit(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 146 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 147 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 148 | nullptr, |
| 149 | 0, |
| 150 | PROT_READ, |
| 151 | false, |
| 152 | false, |
| 153 | &error_msg)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 154 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
| 155 | ASSERT_TRUE(error_msg.empty()); |
| 156 | map.reset(MemMap::MapAnonymous("MapAnonymousEmpty", |
| 157 | nullptr, |
| 158 | kPageSize, |
| 159 | PROT_READ | PROT_WRITE, |
| 160 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 161 | false, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 162 | &error_msg)); |
| 163 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 164 | ASSERT_TRUE(error_msg.empty()); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | #ifdef __LP64__ |
| 168 | TEST_F(MemMapTest, MapAnonymousEmpty32bit) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 169 | CommonInit(); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 170 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 171 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 172 | nullptr, |
| 173 | kPageSize, |
| 174 | PROT_READ | PROT_WRITE, |
| 175 | true, |
| 176 | false, |
| 177 | &error_msg)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 178 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
| 179 | ASSERT_TRUE(error_msg.empty()); |
| 180 | ASSERT_LT(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), 1ULL << 32); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 181 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 182 | TEST_F(MemMapTest, MapFile32Bit) { |
| 183 | CommonInit(); |
| 184 | std::string error_msg; |
| 185 | ScratchFile scratch_file; |
| 186 | constexpr size_t kMapSize = kPageSize; |
| 187 | std::unique_ptr<uint8_t[]> data(new uint8_t[kMapSize]()); |
| 188 | ASSERT_TRUE(scratch_file.GetFile()->WriteFully(&data[0], kMapSize)); |
| 189 | std::unique_ptr<MemMap> map(MemMap::MapFile(/*byte_count*/kMapSize, |
| 190 | PROT_READ, |
| 191 | MAP_PRIVATE, |
| 192 | scratch_file.GetFd(), |
| 193 | /*start*/0, |
| 194 | /*low_4gb*/true, |
| 195 | scratch_file.GetFilename().c_str(), |
| 196 | &error_msg)); |
| 197 | ASSERT_TRUE(map != nullptr) << error_msg; |
| 198 | ASSERT_TRUE(error_msg.empty()); |
| 199 | ASSERT_EQ(map->Size(), kMapSize); |
| 200 | ASSERT_LT(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), 1ULL << 32); |
| 201 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 202 | #endif |
| 203 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 204 | TEST_F(MemMapTest, MapAnonymousExactAddr) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 205 | CommonInit(); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 206 | std::string error_msg; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 207 | // Find a valid address. |
| 208 | uint8_t* valid_address = GetValidMapAddress(kPageSize, /*low_4gb*/false); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 209 | // Map at an address that should work, which should succeed. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 210 | std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0", |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 211 | valid_address, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 212 | kPageSize, |
| 213 | PROT_READ | PROT_WRITE, |
| 214 | false, |
| 215 | false, |
| 216 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 217 | ASSERT_TRUE(map0.get() != nullptr) << error_msg; |
| 218 | ASSERT_TRUE(error_msg.empty()); |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 219 | ASSERT_TRUE(map0->BaseBegin() == valid_address); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 220 | // Map at an unspecified address, which should succeed. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 221 | std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 222 | nullptr, |
| 223 | kPageSize, |
| 224 | PROT_READ | PROT_WRITE, |
| 225 | false, |
| 226 | false, |
| 227 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 228 | ASSERT_TRUE(map1.get() != nullptr) << error_msg; |
| 229 | ASSERT_TRUE(error_msg.empty()); |
| 230 | ASSERT_TRUE(map1->BaseBegin() != nullptr); |
| 231 | // Attempt to map at the same address, which should fail. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 232 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 233 | reinterpret_cast<uint8_t*>(map1->BaseBegin()), |
| 234 | kPageSize, |
| 235 | PROT_READ | PROT_WRITE, |
| 236 | false, |
| 237 | false, |
| 238 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 239 | ASSERT_TRUE(map2.get() == nullptr) << error_msg; |
| 240 | ASSERT_TRUE(!error_msg.empty()); |
| 241 | } |
| 242 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 243 | TEST_F(MemMapTest, RemapAtEnd) { |
| 244 | RemapAtEndTest(false); |
| 245 | } |
| 246 | |
| 247 | #ifdef __LP64__ |
| 248 | TEST_F(MemMapTest, RemapAtEnd32bit) { |
| 249 | RemapAtEndTest(true); |
| 250 | } |
| 251 | #endif |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 252 | |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 253 | TEST_F(MemMapTest, MapAnonymousExactAddr32bitHighAddr) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 254 | CommonInit(); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 255 | // This test may not work under valgrind. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 256 | if (RUNNING_ON_MEMORY_TOOL == 0) { |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 257 | constexpr size_t size = 0x100000; |
| 258 | // Try all addresses starting from 2GB to 4GB. |
| 259 | size_t start_addr = 2 * GB; |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 260 | std::string error_msg; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 261 | std::unique_ptr<MemMap> map; |
| 262 | for (; start_addr <= std::numeric_limits<uint32_t>::max() - size; start_addr += size) { |
| 263 | map.reset(MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr", |
| 264 | reinterpret_cast<uint8_t*>(start_addr), |
| 265 | size, |
| 266 | PROT_READ | PROT_WRITE, |
| 267 | /*low_4gb*/true, |
| 268 | false, |
| 269 | &error_msg)); |
| 270 | if (map != nullptr) { |
| 271 | break; |
| 272 | } |
| 273 | } |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 274 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
Roland Levillain | 8d02644 | 2016-01-19 17:30:33 +0000 | [diff] [blame^] | 275 | ASSERT_GE(reinterpret_cast<uintptr_t>(map->End()), 2u * GB); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 276 | ASSERT_TRUE(error_msg.empty()); |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 277 | ASSERT_EQ(BaseBegin(map.get()), reinterpret_cast<void*>(start_addr)); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 278 | } |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST_F(MemMapTest, MapAnonymousOverflow) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 282 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 283 | std::string error_msg; |
| 284 | uintptr_t ptr = 0; |
| 285 | ptr -= kPageSize; // Now it's close to the top. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 286 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousOverflow", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 287 | reinterpret_cast<uint8_t*>(ptr), |
| 288 | 2 * kPageSize, // brings it over the top. |
| 289 | PROT_READ | PROT_WRITE, |
| 290 | false, |
| 291 | false, |
| 292 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 293 | ASSERT_EQ(nullptr, map.get()); |
| 294 | ASSERT_FALSE(error_msg.empty()); |
| 295 | } |
| 296 | |
| 297 | #ifdef __LP64__ |
| 298 | TEST_F(MemMapTest, MapAnonymousLow4GBExpectedTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 299 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 300 | std::string error_msg; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 301 | std::unique_ptr<MemMap> map( |
| 302 | MemMap::MapAnonymous("MapAnonymousLow4GBExpectedTooHigh", |
| 303 | reinterpret_cast<uint8_t*>(UINT64_C(0x100000000)), |
| 304 | kPageSize, |
| 305 | PROT_READ | PROT_WRITE, |
| 306 | true, |
| 307 | false, |
| 308 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 309 | ASSERT_EQ(nullptr, map.get()); |
| 310 | ASSERT_FALSE(error_msg.empty()); |
| 311 | } |
| 312 | |
| 313 | TEST_F(MemMapTest, MapAnonymousLow4GBRangeTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 314 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 315 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 316 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousLow4GBRangeTooHigh", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 317 | reinterpret_cast<uint8_t*>(0xF0000000), |
| 318 | 0x20000000, |
| 319 | PROT_READ | PROT_WRITE, |
| 320 | true, |
| 321 | false, |
| 322 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 323 | ASSERT_EQ(nullptr, map.get()); |
| 324 | ASSERT_FALSE(error_msg.empty()); |
| 325 | } |
| 326 | #endif |
| 327 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 328 | TEST_F(MemMapTest, MapAnonymousReuse) { |
| 329 | CommonInit(); |
| 330 | std::string error_msg; |
| 331 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousReserve", |
| 332 | nullptr, |
| 333 | 0x20000, |
| 334 | PROT_READ | PROT_WRITE, |
| 335 | false, |
| 336 | false, |
| 337 | &error_msg)); |
| 338 | ASSERT_NE(nullptr, map.get()); |
| 339 | ASSERT_TRUE(error_msg.empty()); |
| 340 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymousReused", |
| 341 | reinterpret_cast<uint8_t*>(map->BaseBegin()), |
| 342 | 0x10000, |
| 343 | PROT_READ | PROT_WRITE, |
| 344 | false, |
| 345 | true, |
| 346 | &error_msg)); |
| 347 | ASSERT_NE(nullptr, map2.get()); |
| 348 | ASSERT_TRUE(error_msg.empty()); |
| 349 | } |
| 350 | |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 351 | TEST_F(MemMapTest, CheckNoGaps) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 352 | CommonInit(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 353 | std::string error_msg; |
| 354 | constexpr size_t kNumPages = 3; |
| 355 | // Map a 3-page mem map. |
| 356 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymous0", |
| 357 | nullptr, |
| 358 | kPageSize * kNumPages, |
| 359 | PROT_READ | PROT_WRITE, |
| 360 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 361 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 362 | &error_msg)); |
| 363 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
| 364 | ASSERT_TRUE(error_msg.empty()); |
| 365 | // Record the base address. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 366 | uint8_t* map_base = reinterpret_cast<uint8_t*>(map->BaseBegin()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 367 | // Unmap it. |
| 368 | map.reset(); |
| 369 | |
| 370 | // Map at the same address, but in page-sized separate mem maps, |
| 371 | // assuming the space at the address is still available. |
| 372 | std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0", |
| 373 | map_base, |
| 374 | kPageSize, |
| 375 | PROT_READ | PROT_WRITE, |
| 376 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 377 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 378 | &error_msg)); |
| 379 | ASSERT_TRUE(map0.get() != nullptr) << error_msg; |
| 380 | ASSERT_TRUE(error_msg.empty()); |
| 381 | std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1", |
| 382 | map_base + kPageSize, |
| 383 | kPageSize, |
| 384 | PROT_READ | PROT_WRITE, |
| 385 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 386 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 387 | &error_msg)); |
| 388 | ASSERT_TRUE(map1.get() != nullptr) << error_msg; |
| 389 | ASSERT_TRUE(error_msg.empty()); |
| 390 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2", |
| 391 | map_base + kPageSize * 2, |
| 392 | kPageSize, |
| 393 | PROT_READ | PROT_WRITE, |
| 394 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 395 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 396 | &error_msg)); |
| 397 | ASSERT_TRUE(map2.get() != nullptr) << error_msg; |
| 398 | ASSERT_TRUE(error_msg.empty()); |
| 399 | |
| 400 | // One-map cases. |
| 401 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map0.get())); |
| 402 | ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map1.get())); |
| 403 | ASSERT_TRUE(MemMap::CheckNoGaps(map2.get(), map2.get())); |
| 404 | |
| 405 | // Two or three-map cases. |
| 406 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map1.get())); |
| 407 | ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map2.get())); |
| 408 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map2.get())); |
| 409 | |
| 410 | // Unmap the middle one. |
| 411 | map1.reset(); |
| 412 | |
| 413 | // Should return false now that there's a gap in the middle. |
| 414 | ASSERT_FALSE(MemMap::CheckNoGaps(map0.get(), map2.get())); |
| 415 | } |
| 416 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 417 | } // namespace art |