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