Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mem_map.h" |
| 18 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Josh Gao | 0389cd5 | 2015-09-16 16:27:00 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 22 | #if !defined(ANDROID_OS) && !defined(__Fuchsia__) |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 23 | #include <sys/resource.h> |
| 24 | #endif |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 25 | |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 26 | #if defined(__linux__) |
| 27 | #include <sys/prctl.h> |
| 28 | #endif |
| 29 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 30 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 31 | #include <memory> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 32 | #include <sstream> |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 33 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 35 | #include "android-base/unique_fd.h" |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 36 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 37 | #include "allocator.h" |
| 38 | #include "bit_utils.h" |
| 39 | #include "globals.h" |
| 40 | #include "logging.h" // For VLOG_IS_ON. |
| 41 | #include "memory_tool.h" |
| 42 | #include "utils.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 43 | |
Ian Rogers | d6b6865 | 2014-06-23 14:07:03 -0700 | [diff] [blame] | 44 | #ifndef MAP_ANONYMOUS |
| 45 | #define MAP_ANONYMOUS MAP_ANON |
| 46 | #endif |
| 47 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 48 | namespace art { |
| 49 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 50 | using android::base::StringPrintf; |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 51 | using android::base::unique_fd; |
| 52 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 53 | template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>> |
| 54 | using AllocationTrackingMultiMap = |
| 55 | std::multimap<Key, T, Compare, TrackingAllocator<std::pair<const Key, T>, kTag>>; |
| 56 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 57 | using Maps = AllocationTrackingMultiMap<void*, MemMap*, kAllocatorTagMaps>; |
| 58 | |
| 59 | // All the non-empty MemMaps. Use a multimap as we do a reserve-and-divide (eg ElfMap::Load()). |
| 60 | static Maps* gMaps GUARDED_BY(MemMap::GetMemMapsLock()) = nullptr; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 61 | |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 62 | // A map containing unique strings used for indentifying anonymous mappings |
| 63 | static std::map<std::string, int> debugStrMap GUARDED_BY(MemMap::GetMemMapsLock()); |
| 64 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 65 | // Retrieve iterator to a `gMaps` entry that is known to exist. |
| 66 | Maps::iterator GetGMapsEntry(const MemMap& map) REQUIRES(MemMap::GetMemMapsLock()) { |
| 67 | DCHECK(map.IsValid()); |
| 68 | DCHECK(gMaps != nullptr); |
| 69 | for (auto it = gMaps->lower_bound(map.BaseBegin()), end = gMaps->end(); |
| 70 | it != end && it->first == map.BaseBegin(); |
| 71 | ++it) { |
| 72 | if (it->second == &map) { |
| 73 | return it; |
| 74 | } |
| 75 | } |
| 76 | LOG(FATAL) << "MemMap not found"; |
| 77 | UNREACHABLE(); |
| 78 | } |
| 79 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 80 | std::ostream& operator<<(std::ostream& os, const Maps& mem_maps) { |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 81 | os << "MemMap:" << std::endl; |
| 82 | for (auto it = mem_maps.begin(); it != mem_maps.end(); ++it) { |
| 83 | void* base = it->first; |
| 84 | MemMap* map = it->second; |
| 85 | CHECK_EQ(base, map->BaseBegin()); |
| 86 | os << *map << std::endl; |
| 87 | } |
| 88 | return os; |
| 89 | } |
| 90 | |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 91 | std::mutex* MemMap::mem_maps_lock_ = nullptr; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 92 | |
Ian Rogers | c3ccc10 | 2014-06-25 11:52:14 -0700 | [diff] [blame] | 93 | #if USE_ART_LOW_4G_ALLOCATOR |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 94 | // Handling mem_map in 32b address range for 64b architectures that do not support MAP_32BIT. |
| 95 | |
| 96 | // The regular start of memory allocations. The first 64KB is protected by SELinux. |
Andreas Gampe | 6bd621a | 2014-05-16 17:28:58 -0700 | [diff] [blame] | 97 | static constexpr uintptr_t LOW_MEM_START = 64 * KB; |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 99 | // Generate random starting position. |
| 100 | // To not interfere with image position, take the image's address and only place it below. Current |
| 101 | // formula (sketch): |
| 102 | // |
| 103 | // ART_BASE_ADDR = 0001XXXXXXXXXXXXXXX |
| 104 | // ---------------------------------------- |
| 105 | // = 0000111111111111111 |
| 106 | // & ~(kPageSize - 1) =~0000000000000001111 |
| 107 | // ---------------------------------------- |
| 108 | // mask = 0000111111111110000 |
| 109 | // & random data = YYYYYYYYYYYYYYYYYYY |
| 110 | // ----------------------------------- |
| 111 | // tmp = 0000YYYYYYYYYYY0000 |
| 112 | // + LOW_MEM_START = 0000000000001000000 |
| 113 | // -------------------------------------- |
| 114 | // start |
| 115 | // |
Josh Gao | 0389cd5 | 2015-09-16 16:27:00 -0700 | [diff] [blame] | 116 | // arc4random as an entropy source is exposed in Bionic, but not in glibc. When we |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 117 | // do not have Bionic, simply start with LOW_MEM_START. |
| 118 | |
| 119 | // Function is standalone so it can be tested somewhat in mem_map_test.cc. |
| 120 | #ifdef __BIONIC__ |
| 121 | uintptr_t CreateStartPos(uint64_t input) { |
| 122 | CHECK_NE(0, ART_BASE_ADDRESS); |
| 123 | |
| 124 | // Start with all bits below highest bit in ART_BASE_ADDRESS. |
| 125 | constexpr size_t leading_zeros = CLZ(static_cast<uint32_t>(ART_BASE_ADDRESS)); |
| 126 | constexpr uintptr_t mask_ones = (1 << (31 - leading_zeros)) - 1; |
| 127 | |
| 128 | // Lowest (usually 12) bits are not used, as aligned by page size. |
| 129 | constexpr uintptr_t mask = mask_ones & ~(kPageSize - 1); |
| 130 | |
| 131 | // Mask input data. |
| 132 | return (input & mask) + LOW_MEM_START; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | static uintptr_t GenerateNextMemPos() { |
| 137 | #ifdef __BIONIC__ |
Josh Gao | 0389cd5 | 2015-09-16 16:27:00 -0700 | [diff] [blame] | 138 | uint64_t random_data; |
| 139 | arc4random_buf(&random_data, sizeof(random_data)); |
| 140 | return CreateStartPos(random_data); |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 141 | #else |
Josh Gao | 0389cd5 | 2015-09-16 16:27:00 -0700 | [diff] [blame] | 142 | // No arc4random on host, see above. |
Andreas Gampe | d8f26db | 2014-05-19 17:01:13 -0700 | [diff] [blame] | 143 | return LOW_MEM_START; |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | // Initialize linear scan to random position. |
| 148 | uintptr_t MemMap::next_mem_pos_ = GenerateNextMemPos(); |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
Mathieu Chartier | 24a0fc8 | 2015-10-13 16:38:52 -0700 | [diff] [blame] | 151 | // Return true if the address range is contained in a single memory map by either reading |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 152 | // the gMaps variable or the /proc/self/map entry. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 153 | bool MemMap::ContainedWithinExistingMap(uint8_t* ptr, size_t size, std::string* error_msg) { |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 154 | uintptr_t begin = reinterpret_cast<uintptr_t>(ptr); |
| 155 | uintptr_t end = begin + size; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 156 | |
| 157 | { |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 158 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 159 | for (auto& pair : *gMaps) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 160 | MemMap* const map = pair.second; |
| 161 | if (begin >= reinterpret_cast<uintptr_t>(map->Begin()) && |
| 162 | end <= reinterpret_cast<uintptr_t>(map->End())) { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 168 | if (error_msg != nullptr) { |
| 169 | PrintFileToLog("/proc/self/maps", LogSeverity::ERROR); |
| 170 | *error_msg = StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " does not overlap " |
| 171 | "any existing map. See process maps in the log.", begin, end); |
| 172 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 176 | // CheckMapRequest to validate a non-MAP_FAILED mmap result based on |
| 177 | // the expected value, calling munmap if validation fails, giving the |
| 178 | // reason in error_msg. |
| 179 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 180 | // If the expected_ptr is null, nothing is checked beyond the fact |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 181 | // that the actual_ptr is not MAP_FAILED. However, if expected_ptr is |
| 182 | // non-null, we check that pointer is the actual_ptr == expected_ptr, |
| 183 | // and if not, report in error_msg what the conflict mapping was if |
| 184 | // found, or a generic error in other cases. |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 185 | bool MemMap::CheckMapRequest(uint8_t* expected_ptr, void* actual_ptr, size_t byte_count, |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 186 | std::string* error_msg) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 187 | // Handled first by caller for more specific error messages. |
| 188 | CHECK(actual_ptr != MAP_FAILED); |
| 189 | |
| 190 | if (expected_ptr == nullptr) { |
| 191 | return true; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 192 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 193 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 194 | uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr); |
| 195 | uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 196 | |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 197 | if (expected_ptr == actual_ptr) { |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | // We asked for an address but didn't get what we wanted, all paths below here should fail. |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 202 | int result = TargetMUnmap(actual_ptr, byte_count); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 203 | if (result == -1) { |
| 204 | PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count); |
| 205 | } |
| 206 | |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 207 | if (error_msg != nullptr) { |
Mathieu Chartier | 83723ae | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 208 | // We call this here so that we can try and generate a full error |
| 209 | // message with the overlapping mapping. There's no guarantee that |
| 210 | // that there will be an overlap though, since |
| 211 | // - The kernel is not *required* to honor expected_ptr unless MAP_FIXED is |
| 212 | // true, even if there is no overlap |
| 213 | // - There might have been an overlap at the point of mmap, but the |
| 214 | // overlapping region has since been unmapped. |
David Sehr | 2300b2d | 2018-05-10 14:20:10 -0700 | [diff] [blame] | 215 | |
| 216 | // Tell the client the mappings that were in place at the time. |
| 217 | if (kIsDebugBuild) { |
| 218 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 219 | } |
| 220 | |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 221 | std::ostringstream os; |
| 222 | os << StringPrintf("Failed to mmap at expected address, mapped at " |
| 223 | "0x%08" PRIxPTR " instead of 0x%08" PRIxPTR, |
| 224 | actual, expected); |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 225 | *error_msg = os.str(); |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 226 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 227 | return false; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 230 | bool MemMap::CheckReservation(uint8_t* expected_ptr, |
| 231 | size_t byte_count, |
| 232 | const char* name, |
| 233 | const MemMap& reservation, |
| 234 | /*out*/std::string* error_msg) { |
| 235 | if (!reservation.IsValid()) { |
| 236 | *error_msg = StringPrintf("Invalid reservation for %s", name); |
| 237 | return false; |
| 238 | } |
| 239 | DCHECK_ALIGNED(reservation.Begin(), kPageSize); |
| 240 | if (reservation.Begin() != expected_ptr) { |
| 241 | *error_msg = StringPrintf("Bad image reservation start for %s: %p instead of %p", |
| 242 | name, |
| 243 | reservation.Begin(), |
| 244 | expected_ptr); |
| 245 | return false; |
| 246 | } |
| 247 | if (byte_count > reservation.Size()) { |
| 248 | *error_msg = StringPrintf("Insufficient reservation, required %zu, available %zu", |
| 249 | byte_count, |
| 250 | reservation.Size()); |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | |
Mathieu Chartier | 38c8221 | 2015-06-04 16:22:41 -0700 | [diff] [blame] | 257 | #if USE_ART_LOW_4G_ALLOCATOR |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 258 | void* MemMap::TryMemMapLow4GB(void* ptr, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 259 | size_t page_aligned_byte_count, |
| 260 | int prot, |
| 261 | int flags, |
| 262 | int fd, |
| 263 | off_t offset) { |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 264 | void* actual = TargetMMap(ptr, page_aligned_byte_count, prot, flags, fd, offset); |
Mathieu Chartier | 38c8221 | 2015-06-04 16:22:41 -0700 | [diff] [blame] | 265 | if (actual != MAP_FAILED) { |
| 266 | // Since we didn't use MAP_FIXED the kernel may have mapped it somewhere not in the low |
| 267 | // 4GB. If this is the case, unmap and retry. |
| 268 | if (reinterpret_cast<uintptr_t>(actual) + page_aligned_byte_count >= 4 * GB) { |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 269 | TargetMUnmap(actual, page_aligned_byte_count); |
Mathieu Chartier | 38c8221 | 2015-06-04 16:22:41 -0700 | [diff] [blame] | 270 | actual = MAP_FAILED; |
| 271 | } |
| 272 | } |
| 273 | return actual; |
| 274 | } |
| 275 | #endif |
| 276 | |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 277 | void MemMap::SetDebugName(void* map_ptr, const char* name, size_t size) { |
| 278 | // Debug naming is only used for Android target builds. For Linux targets, |
| 279 | // we'll still call prctl but it wont do anything till we upstream the prctl. |
| 280 | if (kIsTargetFuchsia || !kIsTargetBuild) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // lock as std::map is not thread-safe |
| 285 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
| 286 | |
| 287 | std::string debug_friendly_name("dalvik-"); |
| 288 | debug_friendly_name += name; |
| 289 | auto it = debugStrMap.find(debug_friendly_name); |
| 290 | |
| 291 | if (it == debugStrMap.end()) { |
| 292 | it = debugStrMap.insert(std::make_pair(std::move(debug_friendly_name), 1)).first; |
| 293 | } |
| 294 | |
| 295 | DCHECK(it != debugStrMap.end()); |
| 296 | #if defined(PR_SET_VMA) && defined(__linux__) |
| 297 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, size, it->first.c_str()); |
| 298 | #else |
| 299 | // Prevent variable unused compiler errors. |
| 300 | UNUSED(map_ptr, size); |
| 301 | #endif |
| 302 | } |
| 303 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 304 | MemMap MemMap::MapAnonymous(const char* name, |
| 305 | uint8_t* addr, |
| 306 | size_t byte_count, |
| 307 | int prot, |
| 308 | bool low_4gb, |
| 309 | bool reuse, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 310 | /*inout*/MemMap* reservation, |
| 311 | /*out*/std::string* error_msg, |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 312 | bool use_debug_name) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 313 | #ifndef __LP64__ |
| 314 | UNUSED(low_4gb); |
| 315 | #endif |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 316 | if (byte_count == 0) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 317 | *error_msg = "Empty MemMap requested."; |
| 318 | return Invalid(); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 319 | } |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 320 | size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 321 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 322 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 323 | if (reuse) { |
| 324 | // reuse means it is okay that it overlaps an existing page mapping. |
| 325 | // Only use this if you actually made the page reservation yourself. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 326 | CHECK(addr != nullptr); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 327 | DCHECK(reservation == nullptr); |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 328 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 329 | DCHECK(ContainedWithinExistingMap(addr, byte_count, error_msg)) << *error_msg; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 330 | flags |= MAP_FIXED; |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 331 | } else if (reservation != nullptr) { |
| 332 | CHECK(addr != nullptr); |
| 333 | if (!CheckReservation(addr, byte_count, name, *reservation, error_msg)) { |
| 334 | return MemMap::Invalid(); |
| 335 | } |
| 336 | flags |= MAP_FIXED; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 339 | unique_fd fd; |
| 340 | |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 341 | // We need to store and potentially set an error number for pretty printing of errors |
| 342 | int saved_errno = 0; |
| 343 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 344 | void* actual = MapInternal(addr, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 345 | page_aligned_byte_count, |
| 346 | prot, |
| 347 | flags, |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 348 | fd.get(), |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 349 | 0, |
| 350 | low_4gb); |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 351 | saved_errno = errno; |
Stuart Monteith | 8dba5aa | 2014-03-12 12:44:01 +0000 | [diff] [blame] | 352 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 353 | if (actual == MAP_FAILED) { |
Mathieu Chartier | 83723ae | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 354 | if (error_msg != nullptr) { |
Andreas Gampe | 7fa5578 | 2016-06-15 17:45:01 -0700 | [diff] [blame] | 355 | if (kIsDebugBuild || VLOG_IS_ON(oat)) { |
| 356 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 357 | } |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 358 | |
Mathieu Chartier | 83723ae | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 359 | *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s. " |
| 360 | "See process maps in the log.", |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 361 | addr, |
Mathieu Chartier | 83723ae | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 362 | page_aligned_byte_count, |
| 363 | prot, |
| 364 | flags, |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 365 | fd.get(), |
Mathieu Chartier | 83723ae | 2016-02-24 10:09:23 -0800 | [diff] [blame] | 366 | strerror(saved_errno)); |
| 367 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 368 | return Invalid(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 369 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 370 | if (!CheckMapRequest(addr, actual, page_aligned_byte_count, error_msg)) { |
| 371 | return Invalid(); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 372 | } |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 373 | |
| 374 | if (use_debug_name) { |
| 375 | SetDebugName(actual, name, page_aligned_byte_count); |
| 376 | } |
| 377 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 378 | if (reservation != nullptr) { |
| 379 | // Re-mapping was successful, transfer the ownership of the memory to the new MemMap. |
| 380 | DCHECK_EQ(actual, reservation->Begin()); |
| 381 | reservation->ReleaseReservedMemory(byte_count); |
| 382 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 383 | return MemMap(name, |
| 384 | reinterpret_cast<uint8_t*>(actual), |
| 385 | byte_count, |
| 386 | actual, |
| 387 | page_aligned_byte_count, |
| 388 | prot, |
| 389 | reuse); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 392 | MemMap MemMap::MapDummy(const char* name, uint8_t* addr, size_t byte_count) { |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 393 | if (byte_count == 0) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 394 | return Invalid(); |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 395 | } |
| 396 | const size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 397 | return MemMap(name, addr, byte_count, addr, page_aligned_byte_count, 0, true /* reuse */); |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 400 | template<typename A, typename B> |
| 401 | static ptrdiff_t PointerDiff(A* a, B* b) { |
| 402 | return static_cast<ptrdiff_t>(reinterpret_cast<intptr_t>(a) - reinterpret_cast<intptr_t>(b)); |
| 403 | } |
| 404 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 405 | bool MemMap::ReplaceWith(MemMap* source, /*out*/std::string* error) { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 406 | #if !HAVE_MREMAP_SYSCALL |
Mathieu Chartier | 7bd3cbd | 2018-08-22 14:15:51 -0700 | [diff] [blame] | 407 | UNUSED(source); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 408 | *error = "Cannot perform atomic replace because we are missing the required mremap syscall"; |
| 409 | return false; |
| 410 | #else // !HAVE_MREMAP_SYSCALL |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 411 | CHECK(source != nullptr); |
| 412 | CHECK(source->IsValid()); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 413 | if (!MemMap::kCanReplaceMapping) { |
| 414 | *error = "Unable to perform atomic replace due to runtime environment!"; |
| 415 | return false; |
| 416 | } |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 417 | // neither can be reuse. |
| 418 | if (source->reuse_ || reuse_) { |
| 419 | *error = "One or both mappings is not a real mmap!"; |
| 420 | return false; |
| 421 | } |
| 422 | // TODO Support redzones. |
| 423 | if (source->redzone_size_ != 0 || redzone_size_ != 0) { |
| 424 | *error = "source and dest have different redzone sizes"; |
| 425 | return false; |
| 426 | } |
| 427 | // Make sure they have the same offset from the actual mmap'd address |
| 428 | if (PointerDiff(BaseBegin(), Begin()) != PointerDiff(source->BaseBegin(), source->Begin())) { |
| 429 | *error = |
| 430 | "source starts at a different offset from the mmap. Cannot atomically replace mappings"; |
| 431 | return false; |
| 432 | } |
| 433 | // mremap doesn't allow the final [start, end] to overlap with the initial [start, end] (it's like |
| 434 | // memcpy but the check is explicit and actually done). |
| 435 | if (source->BaseBegin() > BaseBegin() && |
| 436 | reinterpret_cast<uint8_t*>(BaseBegin()) + source->BaseSize() > |
| 437 | reinterpret_cast<uint8_t*>(source->BaseBegin())) { |
| 438 | *error = "destination memory pages overlap with source memory pages"; |
| 439 | return false; |
| 440 | } |
| 441 | // Change the protection to match the new location. |
| 442 | int old_prot = source->GetProtect(); |
| 443 | if (!source->Protect(GetProtect())) { |
| 444 | *error = "Could not change protections for source to those required for dest."; |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Do the mremap. |
| 449 | void* res = mremap(/*old_address*/source->BaseBegin(), |
| 450 | /*old_size*/source->BaseSize(), |
| 451 | /*new_size*/source->BaseSize(), |
| 452 | /*flags*/MREMAP_MAYMOVE | MREMAP_FIXED, |
| 453 | /*new_address*/BaseBegin()); |
| 454 | if (res == MAP_FAILED) { |
| 455 | int saved_errno = errno; |
| 456 | // Wasn't able to move mapping. Change the protection of source back to the original one and |
| 457 | // return. |
| 458 | source->Protect(old_prot); |
| 459 | *error = std::string("Failed to mremap source to dest. Error was ") + strerror(saved_errno); |
| 460 | return false; |
| 461 | } |
| 462 | CHECK(res == BaseBegin()); |
| 463 | |
| 464 | // The new base_size is all the pages of the 'source' plus any remaining dest pages. We will unmap |
| 465 | // them later. |
| 466 | size_t new_base_size = std::max(source->base_size_, base_size_); |
| 467 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 468 | // Invalidate *source, don't unmap it though since it is already gone. |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 469 | size_t source_size = source->size_; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 470 | source->Invalidate(); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 471 | |
| 472 | size_ = source_size; |
| 473 | base_size_ = new_base_size; |
| 474 | // Reduce base_size if needed (this will unmap the extra pages). |
| 475 | SetSize(source_size); |
| 476 | |
| 477 | return true; |
| 478 | #endif // !HAVE_MREMAP_SYSCALL |
| 479 | } |
| 480 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 481 | MemMap MemMap::MapFileAtAddress(uint8_t* expected_ptr, |
| 482 | size_t byte_count, |
| 483 | int prot, |
| 484 | int flags, |
| 485 | int fd, |
| 486 | off_t start, |
| 487 | bool low_4gb, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 488 | const char* filename, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 489 | bool reuse, |
| 490 | /*inout*/MemMap* reservation, |
| 491 | /*out*/std::string* error_msg) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 492 | CHECK_NE(0, prot); |
| 493 | CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE)); |
Narayan Kamath | b89c3da | 2014-08-21 17:38:09 +0100 | [diff] [blame] | 494 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 495 | // Note that we do not allow MAP_FIXED unless reuse == true or we have an existing |
| 496 | // reservation, i.e we expect this mapping to be contained within an existing map. |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 497 | if (reuse) { |
| 498 | // reuse means it is okay that it overlaps an existing page mapping. |
| 499 | // Only use this if you actually made the page reservation yourself. |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 500 | CHECK(expected_ptr != nullptr); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 501 | DCHECK(reservation == nullptr); |
Mathieu Chartier | 66b1d57 | 2017-02-10 18:41:39 -0800 | [diff] [blame] | 502 | DCHECK(error_msg != nullptr); |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 503 | DCHECK(ContainedWithinExistingMap(expected_ptr, byte_count, error_msg)) |
| 504 | << ((error_msg != nullptr) ? *error_msg : std::string()); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 505 | flags |= MAP_FIXED; |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 506 | } else if (reservation != nullptr) { |
| 507 | DCHECK(error_msg != nullptr); |
| 508 | if (!CheckReservation(expected_ptr, byte_count, filename, *reservation, error_msg)) { |
| 509 | return Invalid(); |
| 510 | } |
| 511 | flags |= MAP_FIXED; |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 512 | } else { |
| 513 | CHECK_EQ(0, flags & MAP_FIXED); |
Narayan Kamath | b89c3da | 2014-08-21 17:38:09 +0100 | [diff] [blame] | 514 | // Don't bother checking for an overlapping region here. We'll |
| 515 | // check this if required after the fact inside CheckMapRequest. |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 518 | if (byte_count == 0) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 519 | return Invalid(); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 520 | } |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 521 | // Adjust 'offset' to be page-aligned as required by mmap. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 522 | int page_offset = start % kPageSize; |
| 523 | off_t page_aligned_offset = start - page_offset; |
Ian Rogers | f8adc60 | 2013-04-18 17:06:19 -0700 | [diff] [blame] | 524 | // Adjust 'byte_count' to be page-aligned as we will map this anyway. |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 525 | size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 526 | // The 'expected_ptr' is modified (if specified, ie non-null) to be page aligned to the file but |
| 527 | // not necessarily to virtual memory. mmap will page align 'expected' for us. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 528 | uint8_t* page_aligned_expected = |
| 529 | (expected_ptr == nullptr) ? nullptr : (expected_ptr - page_offset); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 530 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 531 | size_t redzone_size = 0; |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 532 | if (kRunningOnMemoryTool && kMemoryToolAddsRedzones && expected_ptr == nullptr) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 533 | redzone_size = kPageSize; |
| 534 | page_aligned_byte_count += redzone_size; |
| 535 | } |
| 536 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 537 | uint8_t* actual = reinterpret_cast<uint8_t*>(MapInternal(page_aligned_expected, |
| 538 | page_aligned_byte_count, |
| 539 | prot, |
| 540 | flags, |
| 541 | fd, |
| 542 | page_aligned_offset, |
| 543 | low_4gb)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 544 | if (actual == MAP_FAILED) { |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 545 | if (error_msg != nullptr) { |
| 546 | auto saved_errno = errno; |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 547 | |
Andreas Gampe | 7ec0904 | 2016-04-01 17:20:49 -0700 | [diff] [blame] | 548 | if (kIsDebugBuild || VLOG_IS_ON(oat)) { |
| 549 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
| 550 | } |
Brian Carlstrom | aa94cf3 | 2014-03-23 23:47:25 -0700 | [diff] [blame] | 551 | |
Mathieu Chartier | ebe2dfc | 2015-11-24 13:47:52 -0800 | [diff] [blame] | 552 | *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64 |
| 553 | ") of file '%s' failed: %s. See process maps in the log.", |
| 554 | page_aligned_expected, page_aligned_byte_count, prot, flags, fd, |
| 555 | static_cast<int64_t>(page_aligned_offset), filename, |
| 556 | strerror(saved_errno)); |
| 557 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 558 | return Invalid(); |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 559 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 560 | if (!CheckMapRequest(expected_ptr, actual, page_aligned_byte_count, error_msg)) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 561 | return Invalid(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 562 | } |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 563 | if (redzone_size != 0) { |
| 564 | const uint8_t *real_start = actual + page_offset; |
| 565 | const uint8_t *real_end = actual + page_offset + byte_count; |
| 566 | const uint8_t *mapping_end = actual + page_aligned_byte_count; |
| 567 | |
| 568 | MEMORY_TOOL_MAKE_NOACCESS(actual, real_start - actual); |
| 569 | MEMORY_TOOL_MAKE_NOACCESS(real_end, mapping_end - real_end); |
| 570 | page_aligned_byte_count -= redzone_size; |
| 571 | } |
| 572 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 573 | if (reservation != nullptr) { |
| 574 | // Re-mapping was successful, transfer the ownership of the memory to the new MemMap. |
| 575 | DCHECK_EQ(actual, reservation->Begin()); |
| 576 | reservation->ReleaseReservedMemory(byte_count); |
| 577 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 578 | return MemMap(filename, |
| 579 | actual + page_offset, |
| 580 | byte_count, |
| 581 | actual, |
| 582 | page_aligned_byte_count, |
| 583 | prot, |
| 584 | reuse, |
| 585 | redzone_size); |
| 586 | } |
| 587 | |
| 588 | MemMap::MemMap(MemMap&& other) |
| 589 | : MemMap() { |
| 590 | swap(other); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | MemMap::~MemMap() { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 594 | Reset(); |
| 595 | } |
| 596 | |
| 597 | void MemMap::DoReset() { |
| 598 | DCHECK(IsValid()); |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 599 | |
| 600 | // Unlike Valgrind, AddressSanitizer requires that all manually poisoned memory is unpoisoned |
| 601 | // before it is returned to the system. |
| 602 | if (redzone_size_ != 0) { |
| 603 | MEMORY_TOOL_MAKE_UNDEFINED( |
| 604 | reinterpret_cast<char*>(base_begin_) + base_size_ - redzone_size_, |
| 605 | redzone_size_); |
| 606 | } |
| 607 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 608 | if (!reuse_) { |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 609 | MEMORY_TOOL_MAKE_UNDEFINED(base_begin_, base_size_); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 610 | if (!already_unmapped_) { |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 611 | int result = TargetMUnmap(base_begin_, base_size_); |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 612 | if (result == -1) { |
| 613 | PLOG(FATAL) << "munmap failed"; |
| 614 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 615 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 616 | } |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 617 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 618 | Invalidate(); |
| 619 | } |
| 620 | |
| 621 | void MemMap::Invalidate() { |
| 622 | DCHECK(IsValid()); |
| 623 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 624 | // Remove it from gMaps. |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 625 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 626 | auto it = GetGMapsEntry(*this); |
| 627 | gMaps->erase(it); |
| 628 | |
| 629 | // Mark it as invalid. |
| 630 | base_size_ = 0u; |
| 631 | DCHECK(!IsValid()); |
| 632 | } |
| 633 | |
| 634 | void MemMap::swap(MemMap& other) { |
| 635 | if (IsValid() || other.IsValid()) { |
| 636 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
| 637 | DCHECK(gMaps != nullptr); |
| 638 | auto this_it = IsValid() ? GetGMapsEntry(*this) : gMaps->end(); |
| 639 | auto other_it = other.IsValid() ? GetGMapsEntry(other) : gMaps->end(); |
| 640 | if (IsValid()) { |
| 641 | DCHECK(this_it != gMaps->end()); |
| 642 | DCHECK_EQ(this_it->second, this); |
| 643 | this_it->second = &other; |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 644 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 645 | if (other.IsValid()) { |
| 646 | DCHECK(other_it != gMaps->end()); |
| 647 | DCHECK_EQ(other_it->second, &other); |
| 648 | other_it->second = this; |
| 649 | } |
| 650 | // Swap members with the `mem_maps_lock_` held so that `base_begin_` matches |
| 651 | // with the `gMaps` key when other threads try to use `gMaps`. |
| 652 | SwapMembers(other); |
| 653 | } else { |
| 654 | SwapMembers(other); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 655 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void MemMap::SwapMembers(MemMap& other) { |
| 659 | name_.swap(other.name_); |
| 660 | std::swap(begin_, other.begin_); |
| 661 | std::swap(size_, other.size_); |
| 662 | std::swap(base_begin_, other.base_begin_); |
| 663 | std::swap(base_size_, other.base_size_); |
| 664 | std::swap(prot_, other.prot_); |
| 665 | std::swap(reuse_, other.reuse_); |
| 666 | std::swap(already_unmapped_, other.already_unmapped_); |
| 667 | std::swap(redzone_size_, other.redzone_size_); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 670 | MemMap::MemMap(const std::string& name, uint8_t* begin, size_t size, void* base_begin, |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 671 | size_t base_size, int prot, bool reuse, size_t redzone_size) |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 672 | : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size), |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 673 | prot_(prot), reuse_(reuse), already_unmapped_(false), redzone_size_(redzone_size) { |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 674 | if (size_ == 0) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 675 | CHECK(begin_ == nullptr); |
| 676 | CHECK(base_begin_ == nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 677 | CHECK_EQ(base_size_, 0U); |
| 678 | } else { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 679 | CHECK(begin_ != nullptr); |
| 680 | CHECK(base_begin_ != nullptr); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 681 | CHECK_NE(base_size_, 0U); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 682 | |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 683 | // Add it to gMaps. |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 684 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 685 | DCHECK(gMaps != nullptr); |
| 686 | gMaps->insert(std::make_pair(base_begin_, this)); |
Brian Carlstrom | 9004cb6 | 2013-07-26 15:48:31 -0700 | [diff] [blame] | 687 | } |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 688 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 689 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 690 | MemMap MemMap::RemapAtEnd(uint8_t* new_end, |
| 691 | const char* tail_name, |
| 692 | int tail_prot, |
| 693 | std::string* error_msg, |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 694 | bool use_debug_name) { |
Orion Hodson | 0e904ff | 2018-09-24 08:40:54 +0100 | [diff] [blame^] | 695 | return RemapAtEnd(new_end, |
| 696 | tail_name, |
| 697 | tail_prot, |
| 698 | MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, |
| 699 | /* fd */ -1, |
| 700 | /* offset */ 0, |
| 701 | error_msg, |
| 702 | use_debug_name); |
| 703 | } |
| 704 | |
| 705 | MemMap MemMap::RemapAtEnd(uint8_t* new_end, |
| 706 | const char* tail_name, |
| 707 | int tail_prot, |
| 708 | int flags, |
| 709 | int fd, |
| 710 | off_t offset, |
| 711 | std::string* error_msg, |
| 712 | bool use_debug_name) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 713 | DCHECK_GE(new_end, Begin()); |
| 714 | DCHECK_LE(new_end, End()); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 715 | DCHECK_LE(begin_ + size_, reinterpret_cast<uint8_t*>(base_begin_) + base_size_); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 716 | DCHECK_ALIGNED(begin_, kPageSize); |
| 717 | DCHECK_ALIGNED(base_begin_, kPageSize); |
| 718 | DCHECK_ALIGNED(reinterpret_cast<uint8_t*>(base_begin_) + base_size_, kPageSize); |
| 719 | DCHECK_ALIGNED(new_end, kPageSize); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 720 | uint8_t* old_end = begin_ + size_; |
| 721 | uint8_t* old_base_end = reinterpret_cast<uint8_t*>(base_begin_) + base_size_; |
| 722 | uint8_t* new_base_end = new_end; |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 723 | DCHECK_LE(new_base_end, old_base_end); |
| 724 | if (new_base_end == old_base_end) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 725 | return Invalid(); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 726 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 727 | size_t new_size = new_end - reinterpret_cast<uint8_t*>(begin_); |
| 728 | size_t new_base_size = new_base_end - reinterpret_cast<uint8_t*>(base_begin_); |
| 729 | DCHECK_LE(begin_ + new_size, reinterpret_cast<uint8_t*>(base_begin_) + new_base_size); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 730 | size_t tail_size = old_end - new_end; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 731 | uint8_t* tail_base_begin = new_base_end; |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 732 | size_t tail_base_size = old_base_end - new_base_end; |
| 733 | DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 734 | DCHECK_ALIGNED(tail_base_size, kPageSize); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 735 | |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 736 | MEMORY_TOOL_MAKE_UNDEFINED(tail_base_begin, tail_base_size); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 737 | // Note: Do not explicitly unmap the tail region, mmap() with MAP_FIXED automatically |
| 738 | // removes old mappings for the overlapping region. This makes the operation atomic |
| 739 | // and prevents other threads from racing to allocate memory in the requested region. |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 740 | uint8_t* actual = reinterpret_cast<uint8_t*>(TargetMMap(tail_base_begin, |
| 741 | tail_base_size, |
| 742 | tail_prot, |
| 743 | flags, |
Orion Hodson | 0e904ff | 2018-09-24 08:40:54 +0100 | [diff] [blame^] | 744 | fd, |
| 745 | offset)); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 746 | if (actual == MAP_FAILED) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 747 | PrintFileToLog("/proc/self/maps", LogSeverity::WARNING); |
Orion Hodson | 0e904ff | 2018-09-24 08:40:54 +0100 | [diff] [blame^] | 748 | *error_msg = StringPrintf("map(%p, %zd, 0x%x, 0x%x, %d, 0) failed. See process " |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 749 | "maps in the log.", tail_base_begin, tail_base_size, tail_prot, flags, |
Orion Hodson | 0e904ff | 2018-09-24 08:40:54 +0100 | [diff] [blame^] | 750 | fd); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 751 | return Invalid(); |
Hiroshi Yamauchi | fd7e7f1 | 2013-10-22 14:17:48 -0700 | [diff] [blame] | 752 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 753 | // Update *this. |
| 754 | if (new_base_size == 0u) { |
| 755 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
| 756 | auto it = GetGMapsEntry(*this); |
| 757 | gMaps->erase(it); |
| 758 | } |
Joel Fernandes (Google) | 92597a8 | 2018-08-17 16:19:19 -0700 | [diff] [blame] | 759 | |
| 760 | if (use_debug_name) { |
| 761 | SetDebugName(actual, tail_name, tail_base_size); |
| 762 | } |
| 763 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 764 | size_ = new_size; |
| 765 | base_size_ = new_base_size; |
| 766 | // Return the new mapping. |
| 767 | return MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot, false); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 768 | } |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 769 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 770 | MemMap MemMap::TakeReservedMemory(size_t byte_count) { |
| 771 | uint8_t* begin = Begin(); |
| 772 | ReleaseReservedMemory(byte_count); // Performs necessary DCHECK()s on this reservation. |
| 773 | size_t base_size = RoundUp(byte_count, kPageSize); |
| 774 | return MemMap(name_, begin, byte_count, begin, base_size, prot_, /* reuse */ false); |
| 775 | } |
| 776 | |
| 777 | void MemMap::ReleaseReservedMemory(size_t byte_count) { |
| 778 | // Check the reservation mapping. |
| 779 | DCHECK(IsValid()); |
| 780 | DCHECK(!reuse_); |
| 781 | DCHECK(!already_unmapped_); |
| 782 | DCHECK_EQ(redzone_size_, 0u); |
| 783 | DCHECK_EQ(begin_, base_begin_); |
| 784 | DCHECK_EQ(size_, base_size_); |
| 785 | DCHECK_ALIGNED(begin_, kPageSize); |
| 786 | DCHECK_ALIGNED(size_, kPageSize); |
| 787 | |
| 788 | // Check and round up the `byte_count`. |
| 789 | DCHECK_NE(byte_count, 0u); |
| 790 | DCHECK_LE(byte_count, size_); |
| 791 | byte_count = RoundUp(byte_count, kPageSize); |
| 792 | |
| 793 | if (byte_count == size_) { |
| 794 | Invalidate(); |
| 795 | } else { |
| 796 | // Shrink the reservation MemMap and update its `gMaps` entry. |
| 797 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
| 798 | auto it = GetGMapsEntry(*this); |
| 799 | // TODO: When C++17 becomes available, use std::map<>::extract(), modify, insert. |
| 800 | gMaps->erase(it); |
| 801 | begin_ += byte_count; |
| 802 | size_ -= byte_count; |
| 803 | base_begin_ = begin_; |
| 804 | base_size_ = size_; |
| 805 | gMaps->emplace(base_begin_, this); |
| 806 | } |
| 807 | } |
| 808 | |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 809 | void MemMap::MadviseDontNeedAndZero() { |
| 810 | if (base_begin_ != nullptr || base_size_ != 0) { |
| 811 | if (!kMadviseZeroes) { |
| 812 | memset(base_begin_, 0, base_size_); |
| 813 | } |
| 814 | int result = madvise(base_begin_, base_size_, MADV_DONTNEED); |
| 815 | if (result == -1) { |
| 816 | PLOG(WARNING) << "madvise failed"; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 821 | bool MemMap::Sync() { |
Roland Levillain | 0b0d3b4 | 2018-06-14 13:55:49 +0100 | [diff] [blame] | 822 | // Historical note: To avoid Valgrind errors, we temporarily lifted the lower-end noaccess |
| 823 | // protection before passing it to msync() when `redzone_size_` was non-null, as Valgrind |
| 824 | // only accepts page-aligned base address, and excludes the higher-end noaccess protection |
| 825 | // from the msync range. b/27552451. |
| 826 | return msync(BaseBegin(), BaseSize(), MS_SYNC) == 0; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 829 | bool MemMap::Protect(int prot) { |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 830 | if (base_begin_ == nullptr && base_size_ == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 831 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 832 | return true; |
| 833 | } |
| 834 | |
| 835 | if (mprotect(base_begin_, base_size_, prot) == 0) { |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 836 | prot_ = prot; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 837 | return true; |
| 838 | } |
| 839 | |
Shih-wei Liao | a060ed9 | 2012-06-07 09:25:28 -0700 | [diff] [blame] | 840 | PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", " |
| 841 | << prot << ") failed"; |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 845 | bool MemMap::CheckNoGaps(MemMap& begin_map, MemMap& end_map) { |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 846 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 847 | CHECK(begin_map.IsValid()); |
| 848 | CHECK(end_map.IsValid()); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 849 | CHECK(HasMemMap(begin_map)); |
| 850 | CHECK(HasMemMap(end_map)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 851 | CHECK_LE(begin_map.BaseBegin(), end_map.BaseBegin()); |
| 852 | MemMap* map = &begin_map; |
| 853 | while (map->BaseBegin() != end_map.BaseBegin()) { |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 854 | MemMap* next_map = GetLargestMemMapAt(map->BaseEnd()); |
| 855 | if (next_map == nullptr) { |
| 856 | // Found a gap. |
| 857 | return false; |
| 858 | } |
| 859 | map = next_map; |
| 860 | } |
| 861 | return true; |
| 862 | } |
| 863 | |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 864 | void MemMap::DumpMaps(std::ostream& os, bool terse) { |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 865 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 866 | DumpMapsLocked(os, terse); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 869 | void MemMap::DumpMapsLocked(std::ostream& os, bool terse) { |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 870 | const auto& mem_maps = *gMaps; |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 871 | if (!terse) { |
| 872 | os << mem_maps; |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | // Terse output example: |
| 877 | // [MemMap: 0x409be000+0x20P~0x11dP+0x20P~0x61cP+0x20P prot=0x3 LinearAlloc] |
| 878 | // [MemMap: 0x451d6000+0x6bP(3) prot=0x3 large object space allocation] |
| 879 | // The details: |
| 880 | // "+0x20P" means 0x20 pages taken by a single mapping, |
| 881 | // "~0x11dP" means a gap of 0x11d pages, |
| 882 | // "+0x6bP(3)" means 3 mappings one after another, together taking 0x6b pages. |
| 883 | os << "MemMap:" << std::endl; |
| 884 | for (auto it = mem_maps.begin(), maps_end = mem_maps.end(); it != maps_end;) { |
| 885 | MemMap* map = it->second; |
| 886 | void* base = it->first; |
| 887 | CHECK_EQ(base, map->BaseBegin()); |
| 888 | os << "[MemMap: " << base; |
| 889 | ++it; |
| 890 | // Merge consecutive maps with the same protect flags and name. |
| 891 | constexpr size_t kMaxGaps = 9; |
| 892 | size_t num_gaps = 0; |
| 893 | size_t num = 1u; |
| 894 | size_t size = map->BaseSize(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 895 | CHECK_ALIGNED(size, kPageSize); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 896 | void* end = map->BaseEnd(); |
| 897 | while (it != maps_end && |
| 898 | it->second->GetProtect() == map->GetProtect() && |
| 899 | it->second->GetName() == map->GetName() && |
| 900 | (it->second->BaseBegin() == end || num_gaps < kMaxGaps)) { |
| 901 | if (it->second->BaseBegin() != end) { |
| 902 | ++num_gaps; |
| 903 | os << "+0x" << std::hex << (size / kPageSize) << "P"; |
| 904 | if (num != 1u) { |
| 905 | os << "(" << std::dec << num << ")"; |
| 906 | } |
| 907 | size_t gap = |
| 908 | reinterpret_cast<uintptr_t>(it->second->BaseBegin()) - reinterpret_cast<uintptr_t>(end); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 909 | CHECK_ALIGNED(gap, kPageSize); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 910 | os << "~0x" << std::hex << (gap / kPageSize) << "P"; |
| 911 | num = 0u; |
| 912 | size = 0u; |
| 913 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 914 | CHECK_ALIGNED(it->second->BaseSize(), kPageSize); |
Vladimir Marko | 17a924a | 2015-05-08 15:17:32 +0100 | [diff] [blame] | 915 | ++num; |
| 916 | size += it->second->BaseSize(); |
| 917 | end = it->second->BaseEnd(); |
| 918 | ++it; |
| 919 | } |
| 920 | os << "+0x" << std::hex << (size / kPageSize) << "P"; |
| 921 | if (num != 1u) { |
| 922 | os << "(" << std::dec << num << ")"; |
| 923 | } |
| 924 | os << " prot=0x" << std::hex << map->GetProtect() << " " << map->GetName() << "]" << std::endl; |
| 925 | } |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 928 | bool MemMap::HasMemMap(MemMap& map) { |
| 929 | void* base_begin = map.BaseBegin(); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 930 | for (auto it = gMaps->lower_bound(base_begin), end = gMaps->end(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 931 | it != end && it->first == base_begin; ++it) { |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 932 | if (it->second == &map) { |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 933 | return true; |
| 934 | } |
| 935 | } |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | MemMap* MemMap::GetLargestMemMapAt(void* address) { |
| 940 | size_t largest_size = 0; |
| 941 | MemMap* largest_map = nullptr; |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 942 | DCHECK(gMaps != nullptr); |
| 943 | for (auto it = gMaps->lower_bound(address), end = gMaps->end(); |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 944 | it != end && it->first == address; ++it) { |
| 945 | MemMap* map = it->second; |
| 946 | CHECK(map != nullptr); |
| 947 | if (largest_size < map->BaseSize()) { |
| 948 | largest_size = map->BaseSize(); |
| 949 | largest_map = map; |
| 950 | } |
| 951 | } |
| 952 | return largest_map; |
| 953 | } |
| 954 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 955 | void MemMap::Init() { |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 956 | if (mem_maps_lock_ != nullptr) { |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 957 | // dex2oat calls MemMap::Init twice since its needed before the runtime is created. |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 958 | return; |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 959 | } |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 960 | mem_maps_lock_ = new std::mutex(); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 961 | // Not for thread safety, but for the annotation that gMaps is GUARDED_BY(mem_maps_lock_). |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 962 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 963 | DCHECK(gMaps == nullptr); |
| 964 | gMaps = new Maps; |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 965 | |
| 966 | TargetMMapInit(); |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | void MemMap::Shutdown() { |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 970 | if (mem_maps_lock_ == nullptr) { |
| 971 | // If MemMap::Shutdown is called more than once, there is no effect. |
| 972 | return; |
| 973 | } |
| 974 | { |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 975 | // Not for thread safety, but for the annotation that gMaps is GUARDED_BY(mem_maps_lock_). |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 976 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 977 | DCHECK(gMaps != nullptr); |
| 978 | delete gMaps; |
| 979 | gMaps = nullptr; |
David Sehr | 1b14fb8 | 2017-02-01 10:42:11 -0800 | [diff] [blame] | 980 | } |
| 981 | delete mem_maps_lock_; |
| 982 | mem_maps_lock_ = nullptr; |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 985 | void MemMap::SetSize(size_t new_size) { |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 986 | CHECK_LE(new_size, size_); |
| 987 | size_t new_base_size = RoundUp(new_size + static_cast<size_t>(PointerDiff(Begin(), BaseBegin())), |
| 988 | kPageSize); |
| 989 | if (new_base_size == base_size_) { |
| 990 | size_ = new_size; |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 991 | return; |
| 992 | } |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 993 | CHECK_LT(new_base_size, base_size_); |
Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 994 | MEMORY_TOOL_MAKE_UNDEFINED( |
| 995 | reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BaseBegin()) + |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 996 | new_base_size), |
| 997 | base_size_ - new_base_size); |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 998 | CHECK_EQ(TargetMUnmap(reinterpret_cast<void*>( |
| 999 | reinterpret_cast<uintptr_t>(BaseBegin()) + new_base_size), |
| 1000 | base_size_ - new_base_size), 0) |
| 1001 | << new_base_size << " " << base_size_; |
Alex Light | ca97ada | 2018-02-02 09:25:31 -0800 | [diff] [blame] | 1002 | base_size_ = new_base_size; |
Mathieu Chartier | 379d09f | 2015-01-08 11:28:13 -0800 | [diff] [blame] | 1003 | size_ = new_size; |
| 1004 | } |
| 1005 | |
Andreas Gampe | 651ba59 | 2017-06-14 14:41:33 -0700 | [diff] [blame] | 1006 | void* MemMap::MapInternalArtLow4GBAllocator(size_t length, |
| 1007 | int prot, |
| 1008 | int flags, |
| 1009 | int fd, |
| 1010 | off_t offset) { |
| 1011 | #if USE_ART_LOW_4G_ALLOCATOR |
| 1012 | void* actual = MAP_FAILED; |
| 1013 | |
| 1014 | bool first_run = true; |
| 1015 | |
| 1016 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
| 1017 | for (uintptr_t ptr = next_mem_pos_; ptr < 4 * GB; ptr += kPageSize) { |
| 1018 | // Use gMaps as an optimization to skip over large maps. |
| 1019 | // Find the first map which is address > ptr. |
| 1020 | auto it = gMaps->upper_bound(reinterpret_cast<void*>(ptr)); |
| 1021 | if (it != gMaps->begin()) { |
| 1022 | auto before_it = it; |
| 1023 | --before_it; |
| 1024 | // Start at the end of the map before the upper bound. |
| 1025 | ptr = std::max(ptr, reinterpret_cast<uintptr_t>(before_it->second->BaseEnd())); |
| 1026 | CHECK_ALIGNED(ptr, kPageSize); |
| 1027 | } |
| 1028 | while (it != gMaps->end()) { |
| 1029 | // How much space do we have until the next map? |
| 1030 | size_t delta = reinterpret_cast<uintptr_t>(it->first) - ptr; |
| 1031 | // If the space may be sufficient, break out of the loop. |
| 1032 | if (delta >= length) { |
| 1033 | break; |
| 1034 | } |
| 1035 | // Otherwise, skip to the end of the map. |
| 1036 | ptr = reinterpret_cast<uintptr_t>(it->second->BaseEnd()); |
| 1037 | CHECK_ALIGNED(ptr, kPageSize); |
| 1038 | ++it; |
| 1039 | } |
| 1040 | |
| 1041 | // Try to see if we get lucky with this address since none of the ART maps overlap. |
| 1042 | actual = TryMemMapLow4GB(reinterpret_cast<void*>(ptr), length, prot, flags, fd, offset); |
| 1043 | if (actual != MAP_FAILED) { |
| 1044 | next_mem_pos_ = reinterpret_cast<uintptr_t>(actual) + length; |
| 1045 | return actual; |
| 1046 | } |
| 1047 | |
| 1048 | if (4U * GB - ptr < length) { |
| 1049 | // Not enough memory until 4GB. |
| 1050 | if (first_run) { |
| 1051 | // Try another time from the bottom; |
| 1052 | ptr = LOW_MEM_START - kPageSize; |
| 1053 | first_run = false; |
| 1054 | continue; |
| 1055 | } else { |
| 1056 | // Second try failed. |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | uintptr_t tail_ptr; |
| 1062 | |
| 1063 | // Check pages are free. |
| 1064 | bool safe = true; |
| 1065 | for (tail_ptr = ptr; tail_ptr < ptr + length; tail_ptr += kPageSize) { |
| 1066 | if (msync(reinterpret_cast<void*>(tail_ptr), kPageSize, 0) == 0) { |
| 1067 | safe = false; |
| 1068 | break; |
| 1069 | } else { |
| 1070 | DCHECK_EQ(errno, ENOMEM); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | next_mem_pos_ = tail_ptr; // update early, as we break out when we found and mapped a region |
| 1075 | |
| 1076 | if (safe == true) { |
| 1077 | actual = TryMemMapLow4GB(reinterpret_cast<void*>(ptr), length, prot, flags, fd, offset); |
| 1078 | if (actual != MAP_FAILED) { |
| 1079 | return actual; |
| 1080 | } |
| 1081 | } else { |
| 1082 | // Skip over last page. |
| 1083 | ptr = tail_ptr; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | if (actual == MAP_FAILED) { |
| 1088 | LOG(ERROR) << "Could not find contiguous low-memory space."; |
| 1089 | errno = ENOMEM; |
| 1090 | } |
| 1091 | return actual; |
| 1092 | #else |
| 1093 | UNUSED(length, prot, flags, fd, offset); |
| 1094 | LOG(FATAL) << "Unreachable"; |
| 1095 | UNREACHABLE(); |
| 1096 | #endif |
| 1097 | } |
| 1098 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1099 | void* MemMap::MapInternal(void* addr, |
| 1100 | size_t length, |
| 1101 | int prot, |
| 1102 | int flags, |
| 1103 | int fd, |
| 1104 | off_t offset, |
| 1105 | bool low_4gb) { |
| 1106 | #ifdef __LP64__ |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1107 | // When requesting low_4g memory and having an expectation, the requested range should fit into |
| 1108 | // 4GB. |
| 1109 | if (low_4gb && ( |
| 1110 | // Start out of bounds. |
| 1111 | (reinterpret_cast<uintptr_t>(addr) >> 32) != 0 || |
| 1112 | // End out of bounds. For simplicity, this will fail for the last page of memory. |
| 1113 | ((reinterpret_cast<uintptr_t>(addr) + length) >> 32) != 0)) { |
| 1114 | LOG(ERROR) << "The requested address space (" << addr << ", " |
| 1115 | << reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) + length) |
| 1116 | << ") cannot fit in low_4gb"; |
| 1117 | return MAP_FAILED; |
| 1118 | } |
| 1119 | #else |
| 1120 | UNUSED(low_4gb); |
| 1121 | #endif |
| 1122 | DCHECK_ALIGNED(length, kPageSize); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1123 | // TODO: |
| 1124 | // A page allocator would be a useful abstraction here, as |
| 1125 | // 1) It is doubtful that MAP_32BIT on x86_64 is doing the right job for us |
| 1126 | void* actual = MAP_FAILED; |
| 1127 | #if USE_ART_LOW_4G_ALLOCATOR |
| 1128 | // MAP_32BIT only available on x86_64. |
| 1129 | if (low_4gb && addr == nullptr) { |
Andreas Gampe | 651ba59 | 2017-06-14 14:41:33 -0700 | [diff] [blame] | 1130 | // The linear-scan allocator has an issue when executable pages are denied (e.g., by selinux |
| 1131 | // policies in sensitive processes). In that case, the error code will still be ENOMEM. So |
| 1132 | // the allocator will scan all low 4GB twice, and still fail. This is *very* slow. |
| 1133 | // |
| 1134 | // To avoid the issue, always map non-executable first, and mprotect if necessary. |
| 1135 | const int orig_prot = prot; |
| 1136 | const int prot_non_exec = prot & ~PROT_EXEC; |
| 1137 | actual = MapInternalArtLow4GBAllocator(length, prot_non_exec, flags, fd, offset); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1138 | |
| 1139 | if (actual == MAP_FAILED) { |
Andreas Gampe | 651ba59 | 2017-06-14 14:41:33 -0700 | [diff] [blame] | 1140 | return MAP_FAILED; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1141 | } |
Andreas Gampe | 651ba59 | 2017-06-14 14:41:33 -0700 | [diff] [blame] | 1142 | |
| 1143 | // See if we need to remap with the executable bit now. |
| 1144 | if (orig_prot != prot_non_exec) { |
| 1145 | if (mprotect(actual, length, orig_prot) != 0) { |
| 1146 | PLOG(ERROR) << "Could not protect to requested prot: " << orig_prot; |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 1147 | TargetMUnmap(actual, length); |
Andreas Gampe | 651ba59 | 2017-06-14 14:41:33 -0700 | [diff] [blame] | 1148 | errno = ENOMEM; |
| 1149 | return MAP_FAILED; |
| 1150 | } |
| 1151 | } |
| 1152 | return actual; |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 1155 | actual = TargetMMap(addr, length, prot, flags, fd, offset); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1156 | #else |
| 1157 | #if defined(__LP64__) |
| 1158 | if (low_4gb && addr == nullptr) { |
| 1159 | flags |= MAP_32BIT; |
| 1160 | } |
| 1161 | #endif |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 1162 | actual = TargetMMap(addr, length, prot, flags, fd, offset); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1163 | #endif |
| 1164 | return actual; |
| 1165 | } |
| 1166 | |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1167 | std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) { |
Hiroshi Yamauchi | 3eed93d | 2014-06-04 11:43:59 -0700 | [diff] [blame] | 1168 | os << StringPrintf("[MemMap: %p-%p prot=0x%x %s]", |
| 1169 | mem_map.BaseBegin(), mem_map.BaseEnd(), mem_map.GetProtect(), |
| 1170 | mem_map.GetName().c_str()); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 1171 | return os; |
| 1172 | } |
| 1173 | |
Hiroshi Yamauchi | 6edb9ae | 2016-02-08 14:18:21 -0800 | [diff] [blame] | 1174 | void MemMap::TryReadable() { |
| 1175 | if (base_begin_ == nullptr && base_size_ == 0) { |
| 1176 | return; |
| 1177 | } |
| 1178 | CHECK_NE(prot_ & PROT_READ, 0); |
| 1179 | volatile uint8_t* begin = reinterpret_cast<volatile uint8_t*>(base_begin_); |
| 1180 | volatile uint8_t* end = begin + base_size_; |
| 1181 | DCHECK(IsAligned<kPageSize>(begin)); |
| 1182 | DCHECK(IsAligned<kPageSize>(end)); |
| 1183 | // Read the first byte of each page. Use volatile to prevent the compiler from optimizing away the |
| 1184 | // reads. |
| 1185 | for (volatile uint8_t* ptr = begin; ptr < end; ptr += kPageSize) { |
| 1186 | // This read could fault if protection wasn't set correctly. |
| 1187 | uint8_t value = *ptr; |
| 1188 | UNUSED(value); |
| 1189 | } |
| 1190 | } |
| 1191 | |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 1192 | void ZeroAndReleasePages(void* address, size_t length) { |
Mathieu Chartier | 7c928f0 | 2017-06-05 17:23:44 -0700 | [diff] [blame] | 1193 | if (length == 0) { |
| 1194 | return; |
| 1195 | } |
Mathieu Chartier | 6e6078a | 2016-10-24 15:45:41 -0700 | [diff] [blame] | 1196 | uint8_t* const mem_begin = reinterpret_cast<uint8_t*>(address); |
| 1197 | uint8_t* const mem_end = mem_begin + length; |
| 1198 | uint8_t* const page_begin = AlignUp(mem_begin, kPageSize); |
| 1199 | uint8_t* const page_end = AlignDown(mem_end, kPageSize); |
| 1200 | if (!kMadviseZeroes || page_begin >= page_end) { |
| 1201 | // No possible area to madvise. |
| 1202 | std::fill(mem_begin, mem_end, 0); |
| 1203 | } else { |
| 1204 | // Spans one or more pages. |
| 1205 | DCHECK_LE(mem_begin, page_begin); |
| 1206 | DCHECK_LE(page_begin, page_end); |
| 1207 | DCHECK_LE(page_end, mem_end); |
| 1208 | std::fill(mem_begin, page_begin, 0); |
| 1209 | CHECK_NE(madvise(page_begin, page_end - page_begin, MADV_DONTNEED), -1) << "madvise failed"; |
| 1210 | std::fill(page_end, mem_end, 0); |
| 1211 | } |
| 1212 | } |
| 1213 | |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1214 | void MemMap::AlignBy(size_t size) { |
| 1215 | CHECK_EQ(begin_, base_begin_) << "Unsupported"; |
| 1216 | CHECK_EQ(size_, base_size_) << "Unsupported"; |
| 1217 | CHECK_GT(size, static_cast<size_t>(kPageSize)); |
| 1218 | CHECK_ALIGNED(size, kPageSize); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1219 | CHECK(!reuse_); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1220 | if (IsAlignedParam(reinterpret_cast<uintptr_t>(base_begin_), size) && |
| 1221 | IsAlignedParam(base_size_, size)) { |
| 1222 | // Already aligned. |
| 1223 | return; |
| 1224 | } |
| 1225 | uint8_t* base_begin = reinterpret_cast<uint8_t*>(base_begin_); |
| 1226 | uint8_t* base_end = base_begin + base_size_; |
| 1227 | uint8_t* aligned_base_begin = AlignUp(base_begin, size); |
| 1228 | uint8_t* aligned_base_end = AlignDown(base_end, size); |
| 1229 | CHECK_LE(base_begin, aligned_base_begin); |
| 1230 | CHECK_LE(aligned_base_end, base_end); |
| 1231 | size_t aligned_base_size = aligned_base_end - aligned_base_begin; |
| 1232 | CHECK_LT(aligned_base_begin, aligned_base_end) |
| 1233 | << "base_begin = " << reinterpret_cast<void*>(base_begin) |
| 1234 | << " base_end = " << reinterpret_cast<void*>(base_end); |
| 1235 | CHECK_GE(aligned_base_size, size); |
| 1236 | // Unmap the unaligned parts. |
| 1237 | if (base_begin < aligned_base_begin) { |
| 1238 | MEMORY_TOOL_MAKE_UNDEFINED(base_begin, aligned_base_begin - base_begin); |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 1239 | CHECK_EQ(TargetMUnmap(base_begin, aligned_base_begin - base_begin), 0) |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1240 | << "base_begin=" << reinterpret_cast<void*>(base_begin) |
| 1241 | << " aligned_base_begin=" << reinterpret_cast<void*>(aligned_base_begin); |
| 1242 | } |
| 1243 | if (aligned_base_end < base_end) { |
| 1244 | MEMORY_TOOL_MAKE_UNDEFINED(aligned_base_end, base_end - aligned_base_end); |
Steve Austin | 882ed6b | 2018-06-08 11:40:38 -0700 | [diff] [blame] | 1245 | CHECK_EQ(TargetMUnmap(aligned_base_end, base_end - aligned_base_end), 0) |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1246 | << "base_end=" << reinterpret_cast<void*>(base_end) |
| 1247 | << " aligned_base_end=" << reinterpret_cast<void*>(aligned_base_end); |
| 1248 | } |
| 1249 | std::lock_guard<std::mutex> mu(*mem_maps_lock_); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1250 | if (base_begin < aligned_base_begin) { |
| 1251 | auto it = GetGMapsEntry(*this); |
| 1252 | // TODO: When C++17 becomes available, use std::map<>::extract(), modify, insert. |
| 1253 | gMaps->erase(it); |
| 1254 | gMaps->insert(std::make_pair(aligned_base_begin, this)); |
| 1255 | } |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1256 | base_begin_ = aligned_base_begin; |
| 1257 | base_size_ = aligned_base_size; |
| 1258 | begin_ = aligned_base_begin; |
| 1259 | size_ = aligned_base_size; |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 1260 | DCHECK(gMaps != nullptr); |
Hiroshi Yamauchi | 3c3c4a1 | 2017-02-21 16:49:59 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 1263 | } // namespace art |