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 | |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 22 | #include <random> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 24 | #include "base/common_art_test.h" |
| 25 | #include "common_runtime_test.h" // For TEST_DISABLED_FOR_MIPS |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 26 | #include "memory_tool.h" |
| 27 | #include "unix_file/fd_file.h" |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 31 | class MemMapTest : public CommonArtTest { |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 32 | public: |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 33 | static bool IsAddressMapped(void* addr) { |
| 34 | bool res = msync(addr, 1, MS_SYNC) == 0; |
| 35 | if (!res && errno != ENOMEM) { |
| 36 | PLOG(FATAL) << "Unexpected error occurred on msync"; |
| 37 | } |
| 38 | return res; |
| 39 | } |
| 40 | |
| 41 | static std::vector<uint8_t> RandomData(size_t size) { |
| 42 | std::random_device rd; |
| 43 | std::uniform_int_distribution<uint8_t> dist; |
| 44 | std::vector<uint8_t> res; |
| 45 | res.resize(size); |
| 46 | for (size_t i = 0; i < size; i++) { |
| 47 | res[i] = dist(rd); |
| 48 | } |
| 49 | return res; |
| 50 | } |
| 51 | |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 52 | static uint8_t* GetValidMapAddress(size_t size, bool low_4gb) { |
| 53 | // Find a valid map address and unmap it before returning. |
| 54 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 55 | MemMap map = MemMap::MapAnonymous("temp", |
| 56 | /* addr */ nullptr, |
| 57 | size, |
| 58 | PROT_READ, |
| 59 | low_4gb, |
| 60 | /* reuse */ false, |
| 61 | &error_msg); |
| 62 | CHECK(map.IsValid()); |
| 63 | return map.Begin(); |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 66 | static void RemapAtEndTest(bool low_4gb) { |
| 67 | std::string error_msg; |
| 68 | // Cast the page size to size_t. |
| 69 | const size_t page_size = static_cast<size_t>(kPageSize); |
| 70 | // Map a two-page memory region. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 71 | MemMap m0 = MemMap::MapAnonymous("MemMapTest_RemapAtEndTest_map0", |
| 72 | /* addr */ nullptr, |
| 73 | 2 * page_size, |
| 74 | PROT_READ | PROT_WRITE, |
| 75 | low_4gb, |
| 76 | /* reuse */ false, |
| 77 | &error_msg); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 78 | // Check its state and write to it. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 79 | ASSERT_TRUE(m0.IsValid()); |
| 80 | uint8_t* base0 = m0.Begin(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 81 | ASSERT_TRUE(base0 != nullptr) << error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 82 | size_t size0 = m0.Size(); |
| 83 | EXPECT_EQ(m0.Size(), 2 * page_size); |
| 84 | EXPECT_EQ(m0.BaseBegin(), base0); |
| 85 | EXPECT_EQ(m0.BaseSize(), size0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 86 | memset(base0, 42, 2 * page_size); |
| 87 | // Remap the latter half into a second MemMap. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 88 | MemMap m1 = m0.RemapAtEnd(base0 + page_size, |
| 89 | "MemMapTest_RemapAtEndTest_map1", |
| 90 | PROT_READ | PROT_WRITE, |
| 91 | &error_msg); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 92 | // Check the states of the two maps. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 93 | EXPECT_EQ(m0.Begin(), base0) << error_msg; |
| 94 | EXPECT_EQ(m0.Size(), page_size); |
| 95 | EXPECT_EQ(m0.BaseBegin(), base0); |
| 96 | EXPECT_EQ(m0.BaseSize(), page_size); |
| 97 | uint8_t* base1 = m1.Begin(); |
| 98 | size_t size1 = m1.Size(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 99 | EXPECT_EQ(base1, base0 + page_size); |
| 100 | EXPECT_EQ(size1, page_size); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 101 | EXPECT_EQ(m1.BaseBegin(), base1); |
| 102 | EXPECT_EQ(m1.BaseSize(), size1); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 103 | // Write to the second region. |
| 104 | memset(base1, 43, page_size); |
| 105 | // Check the contents of the two regions. |
| 106 | for (size_t i = 0; i < page_size; ++i) { |
| 107 | EXPECT_EQ(base0[i], 42); |
| 108 | } |
| 109 | for (size_t i = 0; i < page_size; ++i) { |
| 110 | EXPECT_EQ(base1[i], 43); |
| 111 | } |
| 112 | // Unmap the first region. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 113 | m0.Reset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 114 | // Make sure the second region is still accessible after the first |
| 115 | // region is unmapped. |
| 116 | for (size_t i = 0; i < page_size; ++i) { |
| 117 | EXPECT_EQ(base1[i], 43); |
| 118 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 119 | MemMap m2 = m1.RemapAtEnd(m1.Begin(), |
| 120 | "MemMapTest_RemapAtEndTest_map1", |
| 121 | PROT_READ | PROT_WRITE, |
| 122 | &error_msg); |
| 123 | ASSERT_TRUE(m2.IsValid()) << error_msg; |
| 124 | ASSERT_FALSE(m1.IsValid()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 125 | } |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 126 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 127 | void CommonInit() { |
| 128 | MemMap::Init(); |
| 129 | } |
| 130 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 131 | #if defined(__LP64__) && !defined(__x86_64__) |
| 132 | static uintptr_t GetLinearScanPos() { |
| 133 | return MemMap::next_mem_pos_; |
| 134 | } |
| 135 | #endif |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 136 | }; |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 137 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 138 | #if defined(__LP64__) && !defined(__x86_64__) |
| 139 | |
| 140 | #ifdef __BIONIC__ |
| 141 | extern uintptr_t CreateStartPos(uint64_t input); |
| 142 | #endif |
| 143 | |
| 144 | TEST_F(MemMapTest, Start) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 145 | CommonInit(); |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 146 | uintptr_t start = GetLinearScanPos(); |
| 147 | EXPECT_LE(64 * KB, start); |
| 148 | EXPECT_LT(start, static_cast<uintptr_t>(ART_BASE_ADDRESS)); |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 149 | #ifdef __BIONIC__ |
| 150 | // Test a couple of values. Make sure they are different. |
| 151 | uintptr_t last = 0; |
| 152 | for (size_t i = 0; i < 100; ++i) { |
| 153 | uintptr_t random_start = CreateStartPos(i * kPageSize); |
| 154 | EXPECT_NE(last, random_start); |
| 155 | last = random_start; |
| 156 | } |
| 157 | |
| 158 | // Even on max, should be below ART_BASE_ADDRESS. |
| 159 | EXPECT_LT(CreateStartPos(~0), static_cast<uintptr_t>(ART_BASE_ADDRESS)); |
| 160 | #endif |
| 161 | // End of test. |
| 162 | } |
| 163 | #endif |
| 164 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 165 | // We need mremap to be able to test ReplaceMapping at all |
| 166 | #if HAVE_MREMAP_SYSCALL |
| 167 | TEST_F(MemMapTest, ReplaceMapping_SameSize) { |
| 168 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 169 | MemMap dest = MemMap::MapAnonymous("MapAnonymousEmpty-atomic-replace-dest", |
| 170 | /* addr */ nullptr, |
| 171 | kPageSize, |
| 172 | PROT_READ, |
| 173 | /* low_4gb */ false, |
| 174 | /* reuse */ false, |
| 175 | &error_msg); |
| 176 | ASSERT_TRUE(dest.IsValid()); |
| 177 | MemMap source = MemMap::MapAnonymous("MapAnonymous-atomic-replace-source", |
| 178 | /* addr */ nullptr, |
| 179 | kPageSize, |
| 180 | PROT_WRITE | PROT_READ, |
| 181 | /* low_4gb */ false, |
| 182 | /* reuse */ false, |
| 183 | &error_msg); |
| 184 | ASSERT_TRUE(source.IsValid()); |
| 185 | void* source_addr = source.Begin(); |
| 186 | void* dest_addr = dest.Begin(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 187 | ASSERT_TRUE(IsAddressMapped(source_addr)); |
| 188 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
| 189 | |
| 190 | std::vector<uint8_t> data = RandomData(kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 191 | memcpy(source.Begin(), data.data(), data.size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 192 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 193 | ASSERT_TRUE(dest.ReplaceWith(&source, &error_msg)) << error_msg; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 194 | |
| 195 | ASSERT_FALSE(IsAddressMapped(source_addr)); |
| 196 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 197 | ASSERT_FALSE(source.IsValid()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 198 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 199 | ASSERT_EQ(dest.Size(), static_cast<size_t>(kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 200 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 201 | ASSERT_EQ(memcmp(dest.Begin(), data.data(), dest.Size()), 0); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(MemMapTest, ReplaceMapping_MakeLarger) { |
| 205 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 206 | MemMap dest = MemMap::MapAnonymous("MapAnonymousEmpty-atomic-replace-dest", |
| 207 | /* addr */ nullptr, |
| 208 | 5 * kPageSize, // Need to make it larger |
| 209 | // initially so we know |
| 210 | // there won't be mappings |
| 211 | // in the way we we move |
| 212 | // source. |
| 213 | PROT_READ, |
| 214 | /* low_4gb */ false, |
| 215 | /* reuse */ false, |
| 216 | &error_msg); |
| 217 | ASSERT_TRUE(dest.IsValid()); |
| 218 | MemMap source = MemMap::MapAnonymous("MapAnonymous-atomic-replace-source", |
| 219 | /* addr */ nullptr, |
| 220 | 3 * kPageSize, |
| 221 | PROT_WRITE | PROT_READ, |
| 222 | /* low_4gb */ false, |
| 223 | /* reuse */ false, |
| 224 | &error_msg); |
| 225 | ASSERT_TRUE(source.IsValid()); |
| 226 | uint8_t* source_addr = source.Begin(); |
| 227 | uint8_t* dest_addr = dest.Begin(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 228 | ASSERT_TRUE(IsAddressMapped(source_addr)); |
| 229 | |
| 230 | // Fill the source with random data. |
| 231 | std::vector<uint8_t> data = RandomData(3 * kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 232 | memcpy(source.Begin(), data.data(), data.size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 233 | |
| 234 | // Make the dest smaller so that we know we'll have space. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 235 | dest.SetSize(kPageSize); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 236 | |
| 237 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
| 238 | ASSERT_FALSE(IsAddressMapped(dest_addr + 2 * kPageSize)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 239 | ASSERT_EQ(dest.Size(), static_cast<size_t>(kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 240 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 241 | ASSERT_TRUE(dest.ReplaceWith(&source, &error_msg)) << error_msg; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 242 | |
| 243 | ASSERT_FALSE(IsAddressMapped(source_addr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 244 | ASSERT_EQ(dest.Size(), static_cast<size_t>(3 * kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 245 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
| 246 | ASSERT_TRUE(IsAddressMapped(dest_addr + 2 * kPageSize)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 247 | ASSERT_FALSE(source.IsValid()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 248 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 249 | ASSERT_EQ(memcmp(dest.Begin(), data.data(), dest.Size()), 0); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_F(MemMapTest, ReplaceMapping_MakeSmaller) { |
| 253 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 254 | MemMap dest = MemMap::MapAnonymous("MapAnonymousEmpty-atomic-replace-dest", |
| 255 | /* addr */ nullptr, |
| 256 | 3 * kPageSize, |
| 257 | PROT_READ, |
| 258 | /* low_4gb */ false, |
| 259 | /* reuse */ false, |
| 260 | &error_msg); |
| 261 | ASSERT_TRUE(dest.IsValid()); |
| 262 | MemMap source = MemMap::MapAnonymous("MapAnonymous-atomic-replace-source", |
| 263 | /* addr */ nullptr, |
| 264 | kPageSize, |
| 265 | PROT_WRITE | PROT_READ, |
| 266 | /* low_4gb */ false, |
| 267 | /* reuse */ false, |
| 268 | &error_msg); |
| 269 | ASSERT_TRUE(source.IsValid()); |
| 270 | uint8_t* source_addr = source.Begin(); |
| 271 | uint8_t* dest_addr = dest.Begin(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 272 | ASSERT_TRUE(IsAddressMapped(source_addr)); |
| 273 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
| 274 | ASSERT_TRUE(IsAddressMapped(dest_addr + 2 * kPageSize)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 275 | ASSERT_EQ(dest.Size(), static_cast<size_t>(3 * kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 276 | |
| 277 | std::vector<uint8_t> data = RandomData(kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 278 | memcpy(source.Begin(), data.data(), kPageSize); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 279 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 280 | ASSERT_TRUE(dest.ReplaceWith(&source, &error_msg)) << error_msg; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 281 | |
| 282 | ASSERT_FALSE(IsAddressMapped(source_addr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 283 | ASSERT_EQ(dest.Size(), static_cast<size_t>(kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 284 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
| 285 | ASSERT_FALSE(IsAddressMapped(dest_addr + 2 * kPageSize)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 286 | ASSERT_FALSE(source.IsValid()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 287 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 288 | ASSERT_EQ(memcmp(dest.Begin(), data.data(), dest.Size()), 0); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | TEST_F(MemMapTest, ReplaceMapping_FailureOverlap) { |
| 292 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 293 | MemMap dest = |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 294 | MemMap::MapAnonymous( |
| 295 | "MapAnonymousEmpty-atomic-replace-dest", |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 296 | /* addr */ nullptr, |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 297 | 3 * kPageSize, // Need to make it larger initially so we know there won't be mappings in |
| 298 | // the way we we move source. |
| 299 | PROT_READ | PROT_WRITE, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 300 | /* low_4gb */ false, |
| 301 | /* reuse */ false, |
| 302 | &error_msg); |
| 303 | ASSERT_TRUE(dest.IsValid()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 304 | // Resize down to 1 page so we can remap the rest. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 305 | dest.SetSize(kPageSize); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 306 | // Create source from the last 2 pages |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 307 | MemMap source = MemMap::MapAnonymous("MapAnonymous-atomic-replace-source", |
| 308 | dest.Begin() + kPageSize, |
| 309 | 2 * kPageSize, |
| 310 | PROT_WRITE | PROT_READ, |
| 311 | /* low_4gb */ false, |
| 312 | /* reuse */ false, |
| 313 | &error_msg); |
| 314 | ASSERT_TRUE(source.IsValid()); |
| 315 | ASSERT_EQ(dest.Begin() + kPageSize, source.Begin()); |
| 316 | uint8_t* source_addr = source.Begin(); |
| 317 | uint8_t* dest_addr = dest.Begin(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 318 | ASSERT_TRUE(IsAddressMapped(source_addr)); |
| 319 | |
| 320 | // Fill the source and dest with random data. |
| 321 | std::vector<uint8_t> data = RandomData(2 * kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 322 | memcpy(source.Begin(), data.data(), data.size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 323 | std::vector<uint8_t> dest_data = RandomData(kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 324 | memcpy(dest.Begin(), dest_data.data(), dest_data.size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 325 | |
| 326 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 327 | ASSERT_EQ(dest.Size(), static_cast<size_t>(kPageSize)); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 328 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 329 | ASSERT_FALSE(dest.ReplaceWith(&source, &error_msg)) << error_msg; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 330 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 331 | ASSERT_TRUE(IsAddressMapped(source_addr)); |
| 332 | ASSERT_TRUE(IsAddressMapped(dest_addr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 333 | ASSERT_EQ(source.Size(), data.size()); |
| 334 | ASSERT_EQ(dest.Size(), dest_data.size()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 335 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 336 | ASSERT_EQ(memcmp(source.Begin(), data.data(), data.size()), 0); |
| 337 | ASSERT_EQ(memcmp(dest.Begin(), dest_data.data(), dest_data.size()), 0); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 338 | } |
| 339 | #endif // HAVE_MREMAP_SYSCALL |
| 340 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 341 | TEST_F(MemMapTest, MapAnonymousEmpty) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 342 | CommonInit(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 343 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 344 | MemMap map = MemMap::MapAnonymous("MapAnonymousEmpty", |
| 345 | /* addr */ nullptr, |
| 346 | 0, |
| 347 | PROT_READ, |
| 348 | /* low_4gb */ false, |
| 349 | /* reuse */ false, |
| 350 | &error_msg); |
| 351 | ASSERT_FALSE(map.IsValid()) << error_msg; |
| 352 | ASSERT_FALSE(error_msg.empty()); |
| 353 | |
| 354 | error_msg.clear(); |
| 355 | map = MemMap::MapAnonymous("MapAnonymousNonEmpty", |
| 356 | /* addr */ nullptr, |
| 357 | kPageSize, |
| 358 | PROT_READ | PROT_WRITE, |
| 359 | /* low_4gb */ false, |
| 360 | /* reuse */ false, |
| 361 | &error_msg); |
| 362 | ASSERT_TRUE(map.IsValid()) << error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 363 | ASSERT_TRUE(error_msg.empty()); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Mathieu Chartier | 486932a | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 366 | TEST_F(MemMapTest, MapAnonymousFailNullError) { |
| 367 | CommonInit(); |
| 368 | // Test that we don't crash with a null error_str when mapping at an invalid location. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 369 | MemMap map = MemMap::MapAnonymous("MapAnonymousInvalid", |
| 370 | reinterpret_cast<uint8_t*>(kPageSize), |
| 371 | 0x20000, |
| 372 | PROT_READ | PROT_WRITE, |
| 373 | /* low_4gb */ false, |
| 374 | /* reuse */ false, |
| 375 | nullptr); |
| 376 | ASSERT_FALSE(map.IsValid()); |
Mathieu Chartier | 486932a | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 379 | #ifdef __LP64__ |
| 380 | TEST_F(MemMapTest, MapAnonymousEmpty32bit) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 381 | CommonInit(); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 382 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 383 | MemMap map = MemMap::MapAnonymous("MapAnonymousEmpty", |
| 384 | /* addr */ nullptr, |
| 385 | 0, |
| 386 | PROT_READ, |
| 387 | /* low_4gb */ true, |
| 388 | /* reuse */ false, |
| 389 | &error_msg); |
| 390 | ASSERT_FALSE(map.IsValid()) << error_msg; |
| 391 | ASSERT_FALSE(error_msg.empty()); |
| 392 | |
| 393 | error_msg.clear(); |
| 394 | map = MemMap::MapAnonymous("MapAnonymousNonEmpty", |
| 395 | /* addr */ nullptr, |
| 396 | kPageSize, |
| 397 | PROT_READ | PROT_WRITE, |
| 398 | /* low_4gb */ true, |
| 399 | /* reuse */ false, |
| 400 | &error_msg); |
| 401 | ASSERT_TRUE(map.IsValid()) << error_msg; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 402 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 403 | ASSERT_LT(reinterpret_cast<uintptr_t>(map.BaseBegin()), 1ULL << 32); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 404 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 405 | TEST_F(MemMapTest, MapFile32Bit) { |
| 406 | CommonInit(); |
| 407 | std::string error_msg; |
| 408 | ScratchFile scratch_file; |
| 409 | constexpr size_t kMapSize = kPageSize; |
| 410 | std::unique_ptr<uint8_t[]> data(new uint8_t[kMapSize]()); |
| 411 | ASSERT_TRUE(scratch_file.GetFile()->WriteFully(&data[0], kMapSize)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 412 | MemMap map = MemMap::MapFile(/*byte_count*/kMapSize, |
| 413 | PROT_READ, |
| 414 | MAP_PRIVATE, |
| 415 | scratch_file.GetFd(), |
| 416 | /*start*/0, |
| 417 | /*low_4gb*/true, |
| 418 | scratch_file.GetFilename().c_str(), |
| 419 | &error_msg); |
| 420 | ASSERT_TRUE(map.IsValid()) << error_msg; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 421 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 422 | ASSERT_EQ(map.Size(), kMapSize); |
| 423 | ASSERT_LT(reinterpret_cast<uintptr_t>(map.BaseBegin()), 1ULL << 32); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 424 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 425 | #endif |
| 426 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 427 | TEST_F(MemMapTest, MapAnonymousExactAddr) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 428 | CommonInit(); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 429 | std::string error_msg; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 430 | // Find a valid address. |
| 431 | uint8_t* valid_address = GetValidMapAddress(kPageSize, /*low_4gb*/false); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 432 | // Map at an address that should work, which should succeed. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 433 | MemMap map0 = MemMap::MapAnonymous("MapAnonymous0", |
| 434 | valid_address, |
| 435 | kPageSize, |
| 436 | PROT_READ | PROT_WRITE, |
| 437 | /* low_4gb */ false, |
| 438 | /* reuse */ false, |
| 439 | &error_msg); |
| 440 | ASSERT_TRUE(map0.IsValid()) << error_msg; |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 441 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 442 | ASSERT_TRUE(map0.BaseBegin() == valid_address); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 443 | // Map at an unspecified address, which should succeed. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 444 | MemMap map1 = MemMap::MapAnonymous("MapAnonymous1", |
| 445 | /* addr */ nullptr, |
| 446 | kPageSize, |
| 447 | PROT_READ | PROT_WRITE, |
| 448 | /* low_4gb */ false, |
| 449 | /* reuse */ false, |
| 450 | &error_msg); |
| 451 | ASSERT_TRUE(map1.IsValid()) << error_msg; |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 452 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 453 | ASSERT_TRUE(map1.BaseBegin() != nullptr); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 454 | // Attempt to map at the same address, which should fail. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 455 | MemMap map2 = MemMap::MapAnonymous("MapAnonymous2", |
| 456 | reinterpret_cast<uint8_t*>(map1.BaseBegin()), |
| 457 | kPageSize, |
| 458 | PROT_READ | PROT_WRITE, |
| 459 | /* low_4gb */ false, |
| 460 | /* reuse */ false, |
| 461 | &error_msg); |
| 462 | ASSERT_FALSE(map2.IsValid()) << error_msg; |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 463 | ASSERT_TRUE(!error_msg.empty()); |
| 464 | } |
| 465 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 466 | TEST_F(MemMapTest, RemapAtEnd) { |
| 467 | RemapAtEndTest(false); |
| 468 | } |
| 469 | |
| 470 | #ifdef __LP64__ |
| 471 | TEST_F(MemMapTest, RemapAtEnd32bit) { |
| 472 | RemapAtEndTest(true); |
| 473 | } |
| 474 | #endif |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 475 | |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 476 | TEST_F(MemMapTest, MapAnonymousExactAddr32bitHighAddr) { |
Roland Levillain | 14306b8 | 2016-01-20 12:13:57 +0000 | [diff] [blame] | 477 | // Some MIPS32 hardware (namely the Creator Ci20 development board) |
| 478 | // cannot allocate in the 2GB-4GB region. |
| 479 | TEST_DISABLED_FOR_MIPS(); |
| 480 | |
Roland Levillain | 0b0d3b4 | 2018-06-14 13:55:49 +0100 | [diff] [blame] | 481 | // This test does not work under AddressSanitizer. |
| 482 | // Historical note: This test did not work under Valgrind either. |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 483 | TEST_DISABLED_FOR_MEMORY_TOOL(); |
| 484 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 485 | CommonInit(); |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 486 | constexpr size_t size = 0x100000; |
| 487 | // Try all addresses starting from 2GB to 4GB. |
| 488 | size_t start_addr = 2 * GB; |
| 489 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 490 | MemMap map; |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 491 | for (; start_addr <= std::numeric_limits<uint32_t>::max() - size; start_addr += size) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 492 | map = MemMap::MapAnonymous("MapAnonymousExactAddr32bitHighAddr", |
| 493 | reinterpret_cast<uint8_t*>(start_addr), |
| 494 | size, |
| 495 | PROT_READ | PROT_WRITE, |
| 496 | /*low_4gb*/ true, |
| 497 | /* reuse */ false, |
| 498 | &error_msg); |
| 499 | if (map.IsValid()) { |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 500 | break; |
Mathieu Chartier | 16d29f8 | 2015-11-10 10:32:52 -0800 | [diff] [blame] | 501 | } |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 502 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 503 | ASSERT_TRUE(map.IsValid()) << error_msg; |
| 504 | ASSERT_GE(reinterpret_cast<uintptr_t>(map.End()), 2u * GB); |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 505 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 506 | ASSERT_EQ(map.BaseBegin(), reinterpret_cast<void*>(start_addr)); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | TEST_F(MemMapTest, MapAnonymousOverflow) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 510 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 511 | std::string error_msg; |
| 512 | uintptr_t ptr = 0; |
| 513 | ptr -= kPageSize; // Now it's close to the top. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 514 | MemMap map = MemMap::MapAnonymous("MapAnonymousOverflow", |
| 515 | reinterpret_cast<uint8_t*>(ptr), |
| 516 | 2 * kPageSize, // brings it over the top. |
| 517 | PROT_READ | PROT_WRITE, |
| 518 | /* low_4gb */ false, |
| 519 | /* reuse */ false, |
| 520 | &error_msg); |
| 521 | ASSERT_FALSE(map.IsValid()); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 522 | ASSERT_FALSE(error_msg.empty()); |
| 523 | } |
| 524 | |
| 525 | #ifdef __LP64__ |
| 526 | TEST_F(MemMapTest, MapAnonymousLow4GBExpectedTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 527 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 528 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 529 | MemMap map = |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 530 | MemMap::MapAnonymous("MapAnonymousLow4GBExpectedTooHigh", |
| 531 | reinterpret_cast<uint8_t*>(UINT64_C(0x100000000)), |
| 532 | kPageSize, |
| 533 | PROT_READ | PROT_WRITE, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 534 | /* low_4gb */ true, |
| 535 | /* reuse */ false, |
| 536 | &error_msg); |
| 537 | ASSERT_FALSE(map.IsValid()); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 538 | ASSERT_FALSE(error_msg.empty()); |
| 539 | } |
| 540 | |
| 541 | TEST_F(MemMapTest, MapAnonymousLow4GBRangeTooHigh) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 542 | CommonInit(); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 543 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 544 | MemMap map = MemMap::MapAnonymous("MapAnonymousLow4GBRangeTooHigh", |
| 545 | reinterpret_cast<uint8_t*>(0xF0000000), |
| 546 | 0x20000000, |
| 547 | PROT_READ | PROT_WRITE, |
| 548 | /* low_4gb */ true, |
| 549 | /* reuse */ false, |
| 550 | &error_msg); |
| 551 | ASSERT_FALSE(map.IsValid()); |
Qiming Shi | 84d49cc | 2014-04-24 15:38:41 +0800 | [diff] [blame] | 552 | ASSERT_FALSE(error_msg.empty()); |
| 553 | } |
| 554 | #endif |
| 555 | |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 556 | TEST_F(MemMapTest, MapAnonymousReuse) { |
| 557 | CommonInit(); |
| 558 | std::string error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 559 | MemMap map = MemMap::MapAnonymous("MapAnonymousReserve", |
| 560 | nullptr, |
| 561 | 0x20000, |
| 562 | PROT_READ | PROT_WRITE, |
| 563 | /* low_4gb */ false, |
| 564 | /* reuse */ false, |
| 565 | &error_msg); |
| 566 | ASSERT_TRUE(map.IsValid()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 567 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 568 | MemMap map2 = MemMap::MapAnonymous("MapAnonymousReused", |
| 569 | reinterpret_cast<uint8_t*>(map.BaseBegin()), |
| 570 | 0x10000, |
| 571 | PROT_READ | PROT_WRITE, |
| 572 | /* low_4gb */ false, |
| 573 | /* reuse */ true, |
| 574 | &error_msg); |
| 575 | ASSERT_TRUE(map2.IsValid()); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 576 | ASSERT_TRUE(error_msg.empty()); |
| 577 | } |
| 578 | |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 579 | TEST_F(MemMapTest, CheckNoGaps) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 580 | CommonInit(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 581 | std::string error_msg; |
| 582 | constexpr size_t kNumPages = 3; |
| 583 | // Map a 3-page mem map. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 584 | MemMap map = MemMap::MapAnonymous("MapAnonymous0", |
| 585 | /* addr */ nullptr, |
| 586 | kPageSize * kNumPages, |
| 587 | PROT_READ | PROT_WRITE, |
| 588 | /* low_4gb */ false, |
| 589 | /* reuse */ false, |
| 590 | &error_msg); |
| 591 | ASSERT_TRUE(map.IsValid()) << error_msg; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 592 | ASSERT_TRUE(error_msg.empty()); |
| 593 | // Record the base address. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 594 | uint8_t* map_base = reinterpret_cast<uint8_t*>(map.BaseBegin()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 595 | // Unmap it. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 596 | map.Reset(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 597 | |
| 598 | // Map at the same address, but in page-sized separate mem maps, |
| 599 | // assuming the space at the address is still available. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 600 | MemMap map0 = MemMap::MapAnonymous("MapAnonymous0", |
| 601 | map_base, |
| 602 | kPageSize, |
| 603 | PROT_READ | PROT_WRITE, |
| 604 | /* low_4gb */ false, |
| 605 | /* reuse */ false, |
| 606 | &error_msg); |
| 607 | ASSERT_TRUE(map0.IsValid()) << error_msg; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 608 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 609 | MemMap map1 = MemMap::MapAnonymous("MapAnonymous1", |
| 610 | map_base + kPageSize, |
| 611 | kPageSize, |
| 612 | PROT_READ | PROT_WRITE, |
| 613 | /* low_4gb */ false, |
| 614 | /* reuse */ false, |
| 615 | &error_msg); |
| 616 | ASSERT_TRUE(map1.IsValid()) << error_msg; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 617 | ASSERT_TRUE(error_msg.empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 618 | MemMap map2 = MemMap::MapAnonymous("MapAnonymous2", |
| 619 | map_base + kPageSize * 2, |
| 620 | kPageSize, |
| 621 | PROT_READ | PROT_WRITE, |
| 622 | /* low_4gb */ false, |
| 623 | /* reuse */ false, |
| 624 | &error_msg); |
| 625 | ASSERT_TRUE(map2.IsValid()) << error_msg; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 626 | ASSERT_TRUE(error_msg.empty()); |
| 627 | |
| 628 | // One-map cases. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 629 | ASSERT_TRUE(MemMap::CheckNoGaps(map0, map0)); |
| 630 | ASSERT_TRUE(MemMap::CheckNoGaps(map1, map1)); |
| 631 | ASSERT_TRUE(MemMap::CheckNoGaps(map2, map2)); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 632 | |
| 633 | // Two or three-map cases. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 634 | ASSERT_TRUE(MemMap::CheckNoGaps(map0, map1)); |
| 635 | ASSERT_TRUE(MemMap::CheckNoGaps(map1, map2)); |
| 636 | ASSERT_TRUE(MemMap::CheckNoGaps(map0, map2)); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 637 | |
| 638 | // Unmap the middle one. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 639 | map1.Reset(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 640 | |
| 641 | // Should return false now that there's a gap in the middle. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 642 | ASSERT_FALSE(MemMap::CheckNoGaps(map0, map2)); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 645 | TEST_F(MemMapTest, AlignBy) { |
| 646 | CommonInit(); |
| 647 | std::string error_msg; |
| 648 | // Cast the page size to size_t. |
| 649 | const size_t page_size = static_cast<size_t>(kPageSize); |
| 650 | // Map a region. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 651 | MemMap m0 = MemMap::MapAnonymous("MemMapTest_AlignByTest_map0", |
| 652 | /* addr */ nullptr, |
| 653 | 14 * page_size, |
| 654 | PROT_READ | PROT_WRITE, |
| 655 | /* low_4gb */ false, |
| 656 | /* reuse */ false, |
| 657 | &error_msg); |
| 658 | ASSERT_TRUE(m0.IsValid()); |
| 659 | uint8_t* base0 = m0.Begin(); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 660 | ASSERT_TRUE(base0 != nullptr) << error_msg; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 661 | ASSERT_EQ(m0.Size(), 14 * page_size); |
| 662 | ASSERT_EQ(m0.BaseBegin(), base0); |
| 663 | ASSERT_EQ(m0.BaseSize(), m0.Size()); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 664 | |
| 665 | // Break it into several regions by using RemapAtEnd. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 666 | MemMap m1 = m0.RemapAtEnd(base0 + 3 * page_size, |
| 667 | "MemMapTest_AlignByTest_map1", |
| 668 | PROT_READ | PROT_WRITE, |
| 669 | &error_msg); |
| 670 | uint8_t* base1 = m1.Begin(); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 671 | ASSERT_TRUE(base1 != nullptr) << error_msg; |
| 672 | ASSERT_EQ(base1, base0 + 3 * page_size); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 673 | ASSERT_EQ(m0.Size(), 3 * page_size); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 674 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 675 | MemMap m2 = m1.RemapAtEnd(base1 + 4 * page_size, |
| 676 | "MemMapTest_AlignByTest_map2", |
| 677 | PROT_READ | PROT_WRITE, |
| 678 | &error_msg); |
| 679 | uint8_t* base2 = m2.Begin(); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 680 | ASSERT_TRUE(base2 != nullptr) << error_msg; |
| 681 | ASSERT_EQ(base2, base1 + 4 * page_size); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 682 | ASSERT_EQ(m1.Size(), 4 * page_size); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 683 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 684 | MemMap m3 = m2.RemapAtEnd(base2 + 3 * page_size, |
| 685 | "MemMapTest_AlignByTest_map1", |
| 686 | PROT_READ | PROT_WRITE, |
| 687 | &error_msg); |
| 688 | uint8_t* base3 = m3.Begin(); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 689 | ASSERT_TRUE(base3 != nullptr) << error_msg; |
| 690 | ASSERT_EQ(base3, base2 + 3 * page_size); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 691 | ASSERT_EQ(m2.Size(), 3 * page_size); |
| 692 | ASSERT_EQ(m3.Size(), 4 * page_size); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 693 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 694 | uint8_t* end0 = base0 + m0.Size(); |
| 695 | uint8_t* end1 = base1 + m1.Size(); |
| 696 | uint8_t* end2 = base2 + m2.Size(); |
| 697 | uint8_t* end3 = base3 + m3.Size(); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 698 | |
| 699 | ASSERT_EQ(static_cast<size_t>(end3 - base0), 14 * page_size); |
| 700 | |
| 701 | if (IsAlignedParam(base0, 2 * page_size)) { |
| 702 | ASSERT_FALSE(IsAlignedParam(base1, 2 * page_size)); |
| 703 | ASSERT_FALSE(IsAlignedParam(base2, 2 * page_size)); |
| 704 | ASSERT_TRUE(IsAlignedParam(base3, 2 * page_size)); |
| 705 | ASSERT_TRUE(IsAlignedParam(end3, 2 * page_size)); |
| 706 | } else { |
| 707 | ASSERT_TRUE(IsAlignedParam(base1, 2 * page_size)); |
| 708 | ASSERT_TRUE(IsAlignedParam(base2, 2 * page_size)); |
| 709 | ASSERT_FALSE(IsAlignedParam(base3, 2 * page_size)); |
| 710 | ASSERT_FALSE(IsAlignedParam(end3, 2 * page_size)); |
| 711 | } |
| 712 | |
| 713 | // Align by 2 * page_size; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 714 | m0.AlignBy(2 * page_size); |
| 715 | m1.AlignBy(2 * page_size); |
| 716 | m2.AlignBy(2 * page_size); |
| 717 | m3.AlignBy(2 * page_size); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 718 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 719 | EXPECT_TRUE(IsAlignedParam(m0.Begin(), 2 * page_size)); |
| 720 | EXPECT_TRUE(IsAlignedParam(m1.Begin(), 2 * page_size)); |
| 721 | EXPECT_TRUE(IsAlignedParam(m2.Begin(), 2 * page_size)); |
| 722 | EXPECT_TRUE(IsAlignedParam(m3.Begin(), 2 * page_size)); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 723 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 724 | EXPECT_TRUE(IsAlignedParam(m0.Begin() + m0.Size(), 2 * page_size)); |
| 725 | EXPECT_TRUE(IsAlignedParam(m1.Begin() + m1.Size(), 2 * page_size)); |
| 726 | EXPECT_TRUE(IsAlignedParam(m2.Begin() + m2.Size(), 2 * page_size)); |
| 727 | EXPECT_TRUE(IsAlignedParam(m3.Begin() + m3.Size(), 2 * page_size)); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 728 | |
| 729 | if (IsAlignedParam(base0, 2 * page_size)) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 730 | EXPECT_EQ(m0.Begin(), base0); |
| 731 | EXPECT_EQ(m0.Begin() + m0.Size(), end0 - page_size); |
| 732 | EXPECT_EQ(m1.Begin(), base1 + page_size); |
| 733 | EXPECT_EQ(m1.Begin() + m1.Size(), end1 - page_size); |
| 734 | EXPECT_EQ(m2.Begin(), base2 + page_size); |
| 735 | EXPECT_EQ(m2.Begin() + m2.Size(), end2); |
| 736 | EXPECT_EQ(m3.Begin(), base3); |
| 737 | EXPECT_EQ(m3.Begin() + m3.Size(), end3); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 738 | } else { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame^] | 739 | EXPECT_EQ(m0.Begin(), base0 + page_size); |
| 740 | EXPECT_EQ(m0.Begin() + m0.Size(), end0); |
| 741 | EXPECT_EQ(m1.Begin(), base1); |
| 742 | EXPECT_EQ(m1.Begin() + m1.Size(), end1); |
| 743 | EXPECT_EQ(m2.Begin(), base2); |
| 744 | EXPECT_EQ(m2.Begin() + m2.Size(), end2 - page_size); |
| 745 | EXPECT_EQ(m3.Begin(), base3 + page_size); |
| 746 | EXPECT_EQ(m3.Begin() + m3.Size(), end3 - page_size); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 750 | } // namespace art |