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 | |
Mathieu Chartier | 486932a | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 167 | TEST_F(MemMapTest, MapAnonymousFailNullError) { |
| 168 | CommonInit(); |
| 169 | // Test that we don't crash with a null error_str when mapping at an invalid location. |
| 170 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousInvalid", |
| 171 | reinterpret_cast<uint8_t*>(kPageSize), |
| 172 | 0x20000, |
| 173 | PROT_READ | PROT_WRITE, |
| 174 | false, |
| 175 | false, |
| 176 | nullptr)); |
| 177 | ASSERT_EQ(nullptr, map.get()); |
| 178 | } |
| 179 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 180 | #ifdef __LP64__ |
| 181 | TEST_F(MemMapTest, MapAnonymousEmpty32bit) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 182 | CommonInit(); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 183 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 184 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 185 | nullptr, |
| 186 | kPageSize, |
| 187 | PROT_READ | PROT_WRITE, |
| 188 | true, |
| 189 | false, |
| 190 | &error_msg)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 191 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
| 192 | ASSERT_TRUE(error_msg.empty()); |
| 193 | ASSERT_LT(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), 1ULL << 32); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 194 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 195 | TEST_F(MemMapTest, MapFile32Bit) { |
| 196 | CommonInit(); |
| 197 | std::string error_msg; |
| 198 | ScratchFile scratch_file; |
| 199 | constexpr size_t kMapSize = kPageSize; |
| 200 | std::unique_ptr<uint8_t[]> data(new uint8_t[kMapSize]()); |
| 201 | ASSERT_TRUE(scratch_file.GetFile()->WriteFully(&data[0], kMapSize)); |
| 202 | std::unique_ptr<MemMap> map(MemMap::MapFile(/*byte_count*/kMapSize, |
| 203 | PROT_READ, |
| 204 | MAP_PRIVATE, |
| 205 | scratch_file.GetFd(), |
| 206 | /*start*/0, |
| 207 | /*low_4gb*/true, |
| 208 | scratch_file.GetFilename().c_str(), |
| 209 | &error_msg)); |
| 210 | ASSERT_TRUE(map != nullptr) << error_msg; |
| 211 | ASSERT_TRUE(error_msg.empty()); |
| 212 | ASSERT_EQ(map->Size(), kMapSize); |
| 213 | ASSERT_LT(reinterpret_cast<uintptr_t>(BaseBegin(map.get())), 1ULL << 32); |
| 214 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 215 | #endif |
| 216 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 217 | TEST_F(MemMapTest, MapAnonymousExactAddr) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 218 | CommonInit(); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 219 | std::string error_msg; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 220 | // Find a valid address. |
| 221 | uint8_t* valid_address = GetValidMapAddress(kPageSize, /*low_4gb*/false); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 222 | // Map at an address that should work, which should succeed. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 223 | std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0", |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 224 | valid_address, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 225 | kPageSize, |
| 226 | PROT_READ | PROT_WRITE, |
| 227 | false, |
| 228 | false, |
| 229 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 230 | ASSERT_TRUE(map0.get() != nullptr) << error_msg; |
| 231 | ASSERT_TRUE(error_msg.empty()); |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 232 | ASSERT_TRUE(map0->BaseBegin() == valid_address); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 233 | // Map at an unspecified address, which should succeed. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 234 | std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 235 | nullptr, |
| 236 | kPageSize, |
| 237 | PROT_READ | PROT_WRITE, |
| 238 | false, |
| 239 | false, |
| 240 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 241 | ASSERT_TRUE(map1.get() != nullptr) << error_msg; |
| 242 | ASSERT_TRUE(error_msg.empty()); |
| 243 | ASSERT_TRUE(map1->BaseBegin() != nullptr); |
| 244 | // Attempt to map at the same address, which should fail. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 245 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 246 | reinterpret_cast<uint8_t*>(map1->BaseBegin()), |
| 247 | kPageSize, |
| 248 | PROT_READ | PROT_WRITE, |
| 249 | false, |
| 250 | false, |
| 251 | &error_msg)); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 252 | ASSERT_TRUE(map2.get() == nullptr) << error_msg; |
| 253 | ASSERT_TRUE(!error_msg.empty()); |
| 254 | } |
| 255 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 256 | TEST_F(MemMapTest, RemapAtEnd) { |
| 257 | RemapAtEndTest(false); |
| 258 | } |
| 259 | |
| 260 | #ifdef __LP64__ |
| 261 | TEST_F(MemMapTest, RemapAtEnd32bit) { |
| 262 | RemapAtEndTest(true); |
| 263 | } |
| 264 | #endif |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 265 | |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 266 | TEST_F(MemMapTest, MapAnonymousExactAddr32bitHighAddr) { |
Roland Levillain | 14306b8 | 2016-01-20 12:13:57 +0000 | [diff] [blame] | 267 | // Some MIPS32 hardware (namely the Creator Ci20 development board) |
| 268 | // cannot allocate in the 2GB-4GB region. |
| 269 | TEST_DISABLED_FOR_MIPS(); |
| 270 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 271 | CommonInit(); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 272 | // This test may not work under valgrind. |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 273 | if (RUNNING_ON_MEMORY_TOOL == 0) { |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 274 | constexpr size_t size = 0x100000; |
| 275 | // Try all addresses starting from 2GB to 4GB. |
| 276 | size_t start_addr = 2 * GB; |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 277 | std::string error_msg; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 278 | std::unique_ptr<MemMap> map; |
| 279 | for (; start_addr <= std::numeric_limits<uint32_t>::max() - size; start_addr += size) { |
| 280 | map.reset(MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr", |
| 281 | reinterpret_cast<uint8_t*>(start_addr), |
| 282 | size, |
| 283 | PROT_READ | PROT_WRITE, |
| 284 | /*low_4gb*/true, |
| 285 | false, |
| 286 | &error_msg)); |
| 287 | if (map != nullptr) { |
| 288 | break; |
| 289 | } |
| 290 | } |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 291 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
Roland Levillain | 8d02644 | 2016-01-19 17:30:33 +0000 | [diff] [blame] | 292 | ASSERT_GE(reinterpret_cast<uintptr_t>(map->End()), 2u * GB); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 293 | ASSERT_TRUE(error_msg.empty()); |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 294 | ASSERT_EQ(BaseBegin(map.get()), reinterpret_cast<void*>(start_addr)); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 295 | } |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | TEST_F(MemMapTest, MapAnonymousOverflow) { |
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; |
| 301 | uintptr_t ptr = 0; |
| 302 | ptr -= kPageSize; // Now it's close to the top. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 303 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousOverflow", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 304 | reinterpret_cast<uint8_t*>(ptr), |
| 305 | 2 * kPageSize, // brings it over the top. |
| 306 | PROT_READ | PROT_WRITE, |
| 307 | false, |
| 308 | false, |
| 309 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 310 | ASSERT_EQ(nullptr, map.get()); |
| 311 | ASSERT_FALSE(error_msg.empty()); |
| 312 | } |
| 313 | |
| 314 | #ifdef __LP64__ |
| 315 | TEST_F(MemMapTest, MapAnonymousLow4GBExpectedTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 316 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 317 | std::string error_msg; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 318 | std::unique_ptr<MemMap> map( |
| 319 | MemMap::MapAnonymous("MapAnonymousLow4GBExpectedTooHigh", |
| 320 | reinterpret_cast<uint8_t*>(UINT64_C(0x100000000)), |
| 321 | kPageSize, |
| 322 | PROT_READ | PROT_WRITE, |
| 323 | true, |
| 324 | false, |
| 325 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 326 | ASSERT_EQ(nullptr, map.get()); |
| 327 | ASSERT_FALSE(error_msg.empty()); |
| 328 | } |
| 329 | |
| 330 | TEST_F(MemMapTest, MapAnonymousLow4GBRangeTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 331 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 332 | std::string error_msg; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 333 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousLow4GBRangeTooHigh", |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 334 | reinterpret_cast<uint8_t*>(0xF0000000), |
| 335 | 0x20000000, |
| 336 | PROT_READ | PROT_WRITE, |
| 337 | true, |
| 338 | false, |
| 339 | &error_msg)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 340 | ASSERT_EQ(nullptr, map.get()); |
| 341 | ASSERT_FALSE(error_msg.empty()); |
| 342 | } |
| 343 | #endif |
| 344 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 345 | TEST_F(MemMapTest, MapAnonymousReuse) { |
| 346 | CommonInit(); |
| 347 | std::string error_msg; |
| 348 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymousReserve", |
| 349 | nullptr, |
| 350 | 0x20000, |
| 351 | PROT_READ | PROT_WRITE, |
| 352 | false, |
| 353 | false, |
| 354 | &error_msg)); |
| 355 | ASSERT_NE(nullptr, map.get()); |
| 356 | ASSERT_TRUE(error_msg.empty()); |
| 357 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymousReused", |
| 358 | reinterpret_cast<uint8_t*>(map->BaseBegin()), |
| 359 | 0x10000, |
| 360 | PROT_READ | PROT_WRITE, |
| 361 | false, |
| 362 | true, |
| 363 | &error_msg)); |
| 364 | ASSERT_NE(nullptr, map2.get()); |
| 365 | ASSERT_TRUE(error_msg.empty()); |
| 366 | } |
| 367 | |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 368 | TEST_F(MemMapTest, CheckNoGaps) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 369 | CommonInit(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 370 | std::string error_msg; |
| 371 | constexpr size_t kNumPages = 3; |
| 372 | // Map a 3-page mem map. |
| 373 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous("MapAnonymous0", |
| 374 | nullptr, |
| 375 | kPageSize * kNumPages, |
| 376 | PROT_READ | PROT_WRITE, |
| 377 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 378 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 379 | &error_msg)); |
| 380 | ASSERT_TRUE(map.get() != nullptr) << error_msg; |
| 381 | ASSERT_TRUE(error_msg.empty()); |
| 382 | // Record the base address. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 383 | uint8_t* map_base = reinterpret_cast<uint8_t*>(map->BaseBegin()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 384 | // Unmap it. |
| 385 | map.reset(); |
| 386 | |
| 387 | // Map at the same address, but in page-sized separate mem maps, |
| 388 | // assuming the space at the address is still available. |
| 389 | std::unique_ptr<MemMap> map0(MemMap::MapAnonymous("MapAnonymous0", |
| 390 | map_base, |
| 391 | kPageSize, |
| 392 | PROT_READ | PROT_WRITE, |
| 393 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 394 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 395 | &error_msg)); |
| 396 | ASSERT_TRUE(map0.get() != nullptr) << error_msg; |
| 397 | ASSERT_TRUE(error_msg.empty()); |
| 398 | std::unique_ptr<MemMap> map1(MemMap::MapAnonymous("MapAnonymous1", |
| 399 | map_base + kPageSize, |
| 400 | kPageSize, |
| 401 | PROT_READ | PROT_WRITE, |
| 402 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 403 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 404 | &error_msg)); |
| 405 | ASSERT_TRUE(map1.get() != nullptr) << error_msg; |
| 406 | ASSERT_TRUE(error_msg.empty()); |
| 407 | std::unique_ptr<MemMap> map2(MemMap::MapAnonymous("MapAnonymous2", |
| 408 | map_base + kPageSize * 2, |
| 409 | kPageSize, |
| 410 | PROT_READ | PROT_WRITE, |
| 411 | false, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 412 | false, |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 413 | &error_msg)); |
| 414 | ASSERT_TRUE(map2.get() != nullptr) << error_msg; |
| 415 | ASSERT_TRUE(error_msg.empty()); |
| 416 | |
| 417 | // One-map cases. |
| 418 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map0.get())); |
| 419 | ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map1.get())); |
| 420 | ASSERT_TRUE(MemMap::CheckNoGaps(map2.get(), map2.get())); |
| 421 | |
| 422 | // Two or three-map cases. |
| 423 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map1.get())); |
| 424 | ASSERT_TRUE(MemMap::CheckNoGaps(map1.get(), map2.get())); |
| 425 | ASSERT_TRUE(MemMap::CheckNoGaps(map0.get(), map2.get())); |
| 426 | |
| 427 | // Unmap the middle one. |
| 428 | map1.reset(); |
| 429 | |
| 430 | // Should return false now that there's a gap in the middle. |
| 431 | ASSERT_FALSE(MemMap::CheckNoGaps(map0.get(), map2.get())); |
| 432 | } |
| 433 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 434 | } // namespace art |