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
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #if !defined(ANDROID_OS) && !defined(__Fuchsia__) && !defined(_WIN32)
22 #include <sys/resource.h>
23 #endif
24
25 #if defined(__linux__)
26 #include <sys/prctl.h>
27 #endif
28
29 #include <map>
30 #include <memory>
31 #include <sstream>
32
33 #include "android-base/stringprintf.h"
34 #include "android-base/unique_fd.h"
35
36 #include "allocator.h"
37 #include "bit_utils.h"
38 #include "globals.h"
39 #include "logging.h" // For VLOG_IS_ON.
40 #include "memory_tool.h"
41 #include "mman.h" // For the PROT_* and MAP_* constants.
42 #include "utils.h"
43
44 #ifndef MAP_ANONYMOUS
45 #define MAP_ANONYMOUS MAP_ANON
46 #endif
47
48 namespace art {
49
50 using android::base::StringPrintf;
51 using android::base::unique_fd;
52
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
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;
61
62 // A map containing unique strings used for indentifying anonymous mappings
63 static std::map<std::string, int> debugStrMap GUARDED_BY(MemMap::GetMemMapsLock());
64
65 // Retrieve iterator to a `gMaps` entry that is known to exist.
GetGMapsEntry(const MemMap & map)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
operator <<(std::ostream & os,const Maps & mem_maps)80 std::ostream& operator<<(std::ostream& os, const Maps& mem_maps) {
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
91 std::mutex* MemMap::mem_maps_lock_ = nullptr;
92
93 #if USE_ART_LOW_4G_ALLOCATOR
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.
97 static constexpr uintptr_t LOW_MEM_START = 64 * KB;
98
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 //
116 // arc4random as an entropy source is exposed in Bionic, but not in glibc. When we
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__
CreateStartPos(uint64_t input)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
GenerateNextMemPos()136 static uintptr_t GenerateNextMemPos() {
137 #ifdef __BIONIC__
138 uint64_t random_data;
139 arc4random_buf(&random_data, sizeof(random_data));
140 return CreateStartPos(random_data);
141 #else
142 // No arc4random on host, see above.
143 return LOW_MEM_START;
144 #endif
145 }
146
147 // Initialize linear scan to random position.
148 uintptr_t MemMap::next_mem_pos_ = GenerateNextMemPos();
149 #endif
150
151 // Return true if the address range is contained in a single memory map by either reading
152 // the gMaps variable or the /proc/self/map entry.
ContainedWithinExistingMap(uint8_t * ptr,size_t size,std::string * error_msg)153 bool MemMap::ContainedWithinExistingMap(uint8_t* ptr, size_t size, std::string* error_msg) {
154 uintptr_t begin = reinterpret_cast<uintptr_t>(ptr);
155 uintptr_t end = begin + size;
156
157 {
158 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
159 for (auto& pair : *gMaps) {
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
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 }
173 return false;
174 }
175
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 //
180 // If the expected_ptr is null, nothing is checked beyond the fact
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.
CheckMapRequest(uint8_t * expected_ptr,void * actual_ptr,size_t byte_count,std::string * error_msg)185 bool MemMap::CheckMapRequest(uint8_t* expected_ptr, void* actual_ptr, size_t byte_count,
186 std::string* error_msg) {
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;
192 }
193
194 uintptr_t actual = reinterpret_cast<uintptr_t>(actual_ptr);
195 uintptr_t expected = reinterpret_cast<uintptr_t>(expected_ptr);
196
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.
202 int result = TargetMUnmap(actual_ptr, byte_count);
203 if (result == -1) {
204 PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count);
205 }
206
207 if (error_msg != nullptr) {
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.
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
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);
225 *error_msg = os.str();
226 }
227 return false;
228 }
229
CheckReservation(uint8_t * expected_ptr,size_t byte_count,const char * name,const MemMap & reservation,std::string * error_msg)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
257 #if USE_ART_LOW_4G_ALLOCATOR
TryMemMapLow4GB(void * ptr,size_t page_aligned_byte_count,int prot,int flags,int fd,off_t offset)258 void* MemMap::TryMemMapLow4GB(void* ptr,
259 size_t page_aligned_byte_count,
260 int prot,
261 int flags,
262 int fd,
263 off_t offset) {
264 void* actual = TargetMMap(ptr, page_aligned_byte_count, prot, flags, fd, offset);
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) {
269 TargetMUnmap(actual, page_aligned_byte_count);
270 actual = MAP_FAILED;
271 }
272 }
273 return actual;
274 }
275 #endif
276
SetDebugName(void * map_ptr,const char * name,size_t size)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
MapAnonymous(const char * name,uint8_t * addr,size_t byte_count,int prot,bool low_4gb,bool reuse,MemMap * reservation,std::string * error_msg,bool use_debug_name)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,
310 /*inout*/MemMap* reservation,
311 /*out*/std::string* error_msg,
312 bool use_debug_name) {
313 #ifndef __LP64__
314 UNUSED(low_4gb);
315 #endif
316 if (byte_count == 0) {
317 *error_msg = "Empty MemMap requested.";
318 return Invalid();
319 }
320 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
321
322 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
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.
326 CHECK(addr != nullptr);
327 DCHECK(reservation == nullptr);
328
329 DCHECK(ContainedWithinExistingMap(addr, byte_count, error_msg)) << *error_msg;
330 flags |= MAP_FIXED;
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;
337 }
338
339 unique_fd fd;
340
341 // We need to store and potentially set an error number for pretty printing of errors
342 int saved_errno = 0;
343
344 void* actual = MapInternal(addr,
345 page_aligned_byte_count,
346 prot,
347 flags,
348 fd.get(),
349 0,
350 low_4gb);
351 saved_errno = errno;
352
353 if (actual == MAP_FAILED) {
354 if (error_msg != nullptr) {
355 if (kIsDebugBuild || VLOG_IS_ON(oat)) {
356 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
357 }
358
359 *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s. "
360 "See process maps in the log.",
361 addr,
362 page_aligned_byte_count,
363 prot,
364 flags,
365 fd.get(),
366 strerror(saved_errno));
367 }
368 return Invalid();
369 }
370 if (!CheckMapRequest(addr, actual, page_aligned_byte_count, error_msg)) {
371 return Invalid();
372 }
373
374 if (use_debug_name) {
375 SetDebugName(actual, name, page_aligned_byte_count);
376 }
377
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 }
383 return MemMap(name,
384 reinterpret_cast<uint8_t*>(actual),
385 byte_count,
386 actual,
387 page_aligned_byte_count,
388 prot,
389 reuse);
390 }
391
MapDummy(const char * name,uint8_t * addr,size_t byte_count)392 MemMap MemMap::MapDummy(const char* name, uint8_t* addr, size_t byte_count) {
393 if (byte_count == 0) {
394 return Invalid();
395 }
396 const size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
397 return MemMap(name, addr, byte_count, addr, page_aligned_byte_count, 0, /* reuse= */ true);
398 }
399
400 template<typename A, typename B>
PointerDiff(A * a,B * 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
ReplaceWith(MemMap * source,std::string * error)405 bool MemMap::ReplaceWith(MemMap* source, /*out*/std::string* error) {
406 #if !HAVE_MREMAP_SYSCALL
407 UNUSED(source);
408 *error = "Cannot perform atomic replace because we are missing the required mremap syscall";
409 return false;
410 #else // !HAVE_MREMAP_SYSCALL
411 CHECK(source != nullptr);
412 CHECK(source->IsValid());
413 if (!MemMap::kCanReplaceMapping) {
414 *error = "Unable to perform atomic replace due to runtime environment!";
415 return false;
416 }
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
468 // Invalidate *source, don't unmap it though since it is already gone.
469 size_t source_size = source->size_;
470 source->Invalidate();
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
MapFileAtAddress(uint8_t * expected_ptr,size_t byte_count,int prot,int flags,int fd,off_t start,bool low_4gb,const char * filename,bool reuse,MemMap * reservation,std::string * error_msg)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,
488 const char* filename,
489 bool reuse,
490 /*inout*/MemMap* reservation,
491 /*out*/std::string* error_msg) {
492 CHECK_NE(0, prot);
493 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
494
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.
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.
500 CHECK(expected_ptr != nullptr);
501 DCHECK(reservation == nullptr);
502 DCHECK(error_msg != nullptr);
503 DCHECK(ContainedWithinExistingMap(expected_ptr, byte_count, error_msg))
504 << ((error_msg != nullptr) ? *error_msg : std::string());
505 flags |= MAP_FIXED;
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;
512 } else {
513 CHECK_EQ(0, flags & MAP_FIXED);
514 // Don't bother checking for an overlapping region here. We'll
515 // check this if required after the fact inside CheckMapRequest.
516 }
517
518 if (byte_count == 0) {
519 *error_msg = "Empty MemMap requested";
520 return Invalid();
521 }
522 // Adjust 'offset' to be page-aligned as required by mmap.
523 int page_offset = start % kPageSize;
524 off_t page_aligned_offset = start - page_offset;
525 // Adjust 'byte_count' to be page-aligned as we will map this anyway.
526 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize);
527 // The 'expected_ptr' is modified (if specified, ie non-null) to be page aligned to the file but
528 // not necessarily to virtual memory. mmap will page align 'expected' for us.
529 uint8_t* page_aligned_expected =
530 (expected_ptr == nullptr) ? nullptr : (expected_ptr - page_offset);
531
532 size_t redzone_size = 0;
533 if (kRunningOnMemoryTool && kMemoryToolAddsRedzones && expected_ptr == nullptr) {
534 redzone_size = kPageSize;
535 page_aligned_byte_count += redzone_size;
536 }
537
538 uint8_t* actual = reinterpret_cast<uint8_t*>(MapInternal(page_aligned_expected,
539 page_aligned_byte_count,
540 prot,
541 flags,
542 fd,
543 page_aligned_offset,
544 low_4gb));
545 if (actual == MAP_FAILED) {
546 if (error_msg != nullptr) {
547 auto saved_errno = errno;
548
549 if (kIsDebugBuild || VLOG_IS_ON(oat)) {
550 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
551 }
552
553 *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64
554 ") of file '%s' failed: %s. See process maps in the log.",
555 page_aligned_expected, page_aligned_byte_count, prot, flags, fd,
556 static_cast<int64_t>(page_aligned_offset), filename,
557 strerror(saved_errno));
558 }
559 return Invalid();
560 }
561 if (!CheckMapRequest(expected_ptr, actual, page_aligned_byte_count, error_msg)) {
562 return Invalid();
563 }
564 if (redzone_size != 0) {
565 const uint8_t *real_start = actual + page_offset;
566 const uint8_t *real_end = actual + page_offset + byte_count;
567 const uint8_t *mapping_end = actual + page_aligned_byte_count;
568
569 MEMORY_TOOL_MAKE_NOACCESS(actual, real_start - actual);
570 MEMORY_TOOL_MAKE_NOACCESS(real_end, mapping_end - real_end);
571 page_aligned_byte_count -= redzone_size;
572 }
573
574 if (reservation != nullptr) {
575 // Re-mapping was successful, transfer the ownership of the memory to the new MemMap.
576 DCHECK_EQ(actual, reservation->Begin());
577 reservation->ReleaseReservedMemory(byte_count);
578 }
579 return MemMap(filename,
580 actual + page_offset,
581 byte_count,
582 actual,
583 page_aligned_byte_count,
584 prot,
585 reuse,
586 redzone_size);
587 }
588
MemMap(MemMap && other)589 MemMap::MemMap(MemMap&& other) noexcept
590 : MemMap() {
591 swap(other);
592 }
593
~MemMap()594 MemMap::~MemMap() {
595 Reset();
596 }
597
DoReset()598 void MemMap::DoReset() {
599 DCHECK(IsValid());
600
601 // Unlike Valgrind, AddressSanitizer requires that all manually poisoned memory is unpoisoned
602 // before it is returned to the system.
603 if (redzone_size_ != 0) {
604 MEMORY_TOOL_MAKE_UNDEFINED(
605 reinterpret_cast<char*>(base_begin_) + base_size_ - redzone_size_,
606 redzone_size_);
607 }
608
609 if (!reuse_) {
610 MEMORY_TOOL_MAKE_UNDEFINED(base_begin_, base_size_);
611 if (!already_unmapped_) {
612 int result = TargetMUnmap(base_begin_, base_size_);
613 if (result == -1) {
614 PLOG(FATAL) << "munmap failed";
615 }
616 }
617 }
618
619 Invalidate();
620 }
621
ResetInForkedProcess()622 void MemMap::ResetInForkedProcess() {
623 // This should be called on a map that has MADV_DONTFORK.
624 // The kernel has already unmapped this.
625 already_unmapped_ = true;
626 Reset();
627 }
628
Invalidate()629 void MemMap::Invalidate() {
630 DCHECK(IsValid());
631
632 // Remove it from gMaps.
633 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
634 auto it = GetGMapsEntry(*this);
635 gMaps->erase(it);
636
637 // Mark it as invalid.
638 base_size_ = 0u;
639 DCHECK(!IsValid());
640 }
641
swap(MemMap & other)642 void MemMap::swap(MemMap& other) {
643 if (IsValid() || other.IsValid()) {
644 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
645 DCHECK(gMaps != nullptr);
646 auto this_it = IsValid() ? GetGMapsEntry(*this) : gMaps->end();
647 auto other_it = other.IsValid() ? GetGMapsEntry(other) : gMaps->end();
648 if (IsValid()) {
649 DCHECK(this_it != gMaps->end());
650 DCHECK_EQ(this_it->second, this);
651 this_it->second = &other;
652 }
653 if (other.IsValid()) {
654 DCHECK(other_it != gMaps->end());
655 DCHECK_EQ(other_it->second, &other);
656 other_it->second = this;
657 }
658 // Swap members with the `mem_maps_lock_` held so that `base_begin_` matches
659 // with the `gMaps` key when other threads try to use `gMaps`.
660 SwapMembers(other);
661 } else {
662 SwapMembers(other);
663 }
664 }
665
SwapMembers(MemMap & other)666 void MemMap::SwapMembers(MemMap& other) {
667 name_.swap(other.name_);
668 std::swap(begin_, other.begin_);
669 std::swap(size_, other.size_);
670 std::swap(base_begin_, other.base_begin_);
671 std::swap(base_size_, other.base_size_);
672 std::swap(prot_, other.prot_);
673 std::swap(reuse_, other.reuse_);
674 std::swap(already_unmapped_, other.already_unmapped_);
675 std::swap(redzone_size_, other.redzone_size_);
676 }
677
MemMap(const std::string & name,uint8_t * begin,size_t size,void * base_begin,size_t base_size,int prot,bool reuse,size_t redzone_size)678 MemMap::MemMap(const std::string& name, uint8_t* begin, size_t size, void* base_begin,
679 size_t base_size, int prot, bool reuse, size_t redzone_size)
680 : name_(name), begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size),
681 prot_(prot), reuse_(reuse), already_unmapped_(false), redzone_size_(redzone_size) {
682 if (size_ == 0) {
683 CHECK(begin_ == nullptr);
684 CHECK(base_begin_ == nullptr);
685 CHECK_EQ(base_size_, 0U);
686 } else {
687 CHECK(begin_ != nullptr);
688 CHECK(base_begin_ != nullptr);
689 CHECK_NE(base_size_, 0U);
690
691 // Add it to gMaps.
692 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
693 DCHECK(gMaps != nullptr);
694 gMaps->insert(std::make_pair(base_begin_, this));
695 }
696 }
697
RemapAtEnd(uint8_t * new_end,const char * tail_name,int tail_prot,std::string * error_msg,bool use_debug_name)698 MemMap MemMap::RemapAtEnd(uint8_t* new_end,
699 const char* tail_name,
700 int tail_prot,
701 std::string* error_msg,
702 bool use_debug_name) {
703 return RemapAtEnd(new_end,
704 tail_name,
705 tail_prot,
706 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
707 /* fd= */ -1,
708 /* offset= */ 0,
709 error_msg,
710 use_debug_name);
711 }
712
RemapAtEnd(uint8_t * new_end,const char * tail_name,int tail_prot,int flags,int fd,off_t offset,std::string * error_msg,bool use_debug_name)713 MemMap MemMap::RemapAtEnd(uint8_t* new_end,
714 const char* tail_name,
715 int tail_prot,
716 int flags,
717 int fd,
718 off_t offset,
719 std::string* error_msg,
720 bool use_debug_name) {
721 DCHECK_GE(new_end, Begin());
722 DCHECK_LE(new_end, End());
723 DCHECK_LE(begin_ + size_, reinterpret_cast<uint8_t*>(base_begin_) + base_size_);
724 DCHECK_ALIGNED(begin_, kPageSize);
725 DCHECK_ALIGNED(base_begin_, kPageSize);
726 DCHECK_ALIGNED(reinterpret_cast<uint8_t*>(base_begin_) + base_size_, kPageSize);
727 DCHECK_ALIGNED(new_end, kPageSize);
728 uint8_t* old_end = begin_ + size_;
729 uint8_t* old_base_end = reinterpret_cast<uint8_t*>(base_begin_) + base_size_;
730 uint8_t* new_base_end = new_end;
731 DCHECK_LE(new_base_end, old_base_end);
732 if (new_base_end == old_base_end) {
733 return Invalid();
734 }
735 size_t new_size = new_end - reinterpret_cast<uint8_t*>(begin_);
736 size_t new_base_size = new_base_end - reinterpret_cast<uint8_t*>(base_begin_);
737 DCHECK_LE(begin_ + new_size, reinterpret_cast<uint8_t*>(base_begin_) + new_base_size);
738 size_t tail_size = old_end - new_end;
739 uint8_t* tail_base_begin = new_base_end;
740 size_t tail_base_size = old_base_end - new_base_end;
741 DCHECK_EQ(tail_base_begin + tail_base_size, old_base_end);
742 DCHECK_ALIGNED(tail_base_size, kPageSize);
743
744 MEMORY_TOOL_MAKE_UNDEFINED(tail_base_begin, tail_base_size);
745 // Note: Do not explicitly unmap the tail region, mmap() with MAP_FIXED automatically
746 // removes old mappings for the overlapping region. This makes the operation atomic
747 // and prevents other threads from racing to allocate memory in the requested region.
748 uint8_t* actual = reinterpret_cast<uint8_t*>(TargetMMap(tail_base_begin,
749 tail_base_size,
750 tail_prot,
751 flags,
752 fd,
753 offset));
754 if (actual == MAP_FAILED) {
755 *error_msg = StringPrintf("map(%p, %zd, 0x%x, 0x%x, %d, 0) failed: %s. See process "
756 "maps in the log.", tail_base_begin, tail_base_size, tail_prot, flags,
757 fd, strerror(errno));
758 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
759 return Invalid();
760 }
761 // Update *this.
762 if (new_base_size == 0u) {
763 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
764 auto it = GetGMapsEntry(*this);
765 gMaps->erase(it);
766 }
767
768 if (use_debug_name) {
769 SetDebugName(actual, tail_name, tail_base_size);
770 }
771
772 size_ = new_size;
773 base_size_ = new_base_size;
774 // Return the new mapping.
775 return MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot, false);
776 }
777
TakeReservedMemory(size_t byte_count)778 MemMap MemMap::TakeReservedMemory(size_t byte_count) {
779 uint8_t* begin = Begin();
780 ReleaseReservedMemory(byte_count); // Performs necessary DCHECK()s on this reservation.
781 size_t base_size = RoundUp(byte_count, kPageSize);
782 return MemMap(name_, begin, byte_count, begin, base_size, prot_, /* reuse= */ false);
783 }
784
ReleaseReservedMemory(size_t byte_count)785 void MemMap::ReleaseReservedMemory(size_t byte_count) {
786 // Check the reservation mapping.
787 DCHECK(IsValid());
788 DCHECK(!reuse_);
789 DCHECK(!already_unmapped_);
790 DCHECK_EQ(redzone_size_, 0u);
791 DCHECK_EQ(begin_, base_begin_);
792 DCHECK_EQ(size_, base_size_);
793 DCHECK_ALIGNED(begin_, kPageSize);
794 DCHECK_ALIGNED(size_, kPageSize);
795
796 // Check and round up the `byte_count`.
797 DCHECK_NE(byte_count, 0u);
798 DCHECK_LE(byte_count, size_);
799 byte_count = RoundUp(byte_count, kPageSize);
800
801 if (byte_count == size_) {
802 Invalidate();
803 } else {
804 // Shrink the reservation MemMap and update its `gMaps` entry.
805 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
806 auto it = GetGMapsEntry(*this);
807 auto node = gMaps->extract(it);
808 begin_ += byte_count;
809 size_ -= byte_count;
810 base_begin_ = begin_;
811 base_size_ = size_;
812 node.key() = base_begin_;
813 gMaps->insert(std::move(node));
814 }
815 }
816
MadviseDontNeedAndZero()817 void MemMap::MadviseDontNeedAndZero() {
818 if (base_begin_ != nullptr || base_size_ != 0) {
819 if (!kMadviseZeroes) {
820 memset(base_begin_, 0, base_size_);
821 }
822 #ifdef _WIN32
823 // It is benign not to madvise away the pages here.
824 PLOG(WARNING) << "MemMap::MadviseDontNeedAndZero does not madvise on Windows.";
825 #else
826 int result = madvise(base_begin_, base_size_, MADV_DONTNEED);
827 if (result == -1) {
828 PLOG(WARNING) << "madvise failed";
829 }
830 #endif
831 }
832 }
833
MadviseDontFork()834 int MemMap::MadviseDontFork() {
835 #if defined(__linux__)
836 if (base_begin_ != nullptr || base_size_ != 0) {
837 return madvise(base_begin_, base_size_, MADV_DONTFORK);
838 }
839 #endif
840 return -1;
841 }
842
Sync()843 bool MemMap::Sync() {
844 #ifdef _WIN32
845 // TODO: add FlushViewOfFile support.
846 PLOG(ERROR) << "MemMap::Sync unsupported on Windows.";
847 return false;
848 #else
849 // Historical note: To avoid Valgrind errors, we temporarily lifted the lower-end noaccess
850 // protection before passing it to msync() when `redzone_size_` was non-null, as Valgrind
851 // only accepts page-aligned base address, and excludes the higher-end noaccess protection
852 // from the msync range. b/27552451.
853 return msync(BaseBegin(), BaseSize(), MS_SYNC) == 0;
854 #endif
855 }
856
Protect(int prot)857 bool MemMap::Protect(int prot) {
858 if (base_begin_ == nullptr && base_size_ == 0) {
859 prot_ = prot;
860 return true;
861 }
862
863 #ifndef _WIN32
864 if (mprotect(base_begin_, base_size_, prot) == 0) {
865 prot_ = prot;
866 return true;
867 }
868 #endif
869
870 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", "
871 << prot << ") failed";
872 return false;
873 }
874
CheckNoGaps(MemMap & begin_map,MemMap & end_map)875 bool MemMap::CheckNoGaps(MemMap& begin_map, MemMap& end_map) {
876 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
877 CHECK(begin_map.IsValid());
878 CHECK(end_map.IsValid());
879 CHECK(HasMemMap(begin_map));
880 CHECK(HasMemMap(end_map));
881 CHECK_LE(begin_map.BaseBegin(), end_map.BaseBegin());
882 MemMap* map = &begin_map;
883 while (map->BaseBegin() != end_map.BaseBegin()) {
884 MemMap* next_map = GetLargestMemMapAt(map->BaseEnd());
885 if (next_map == nullptr) {
886 // Found a gap.
887 return false;
888 }
889 map = next_map;
890 }
891 return true;
892 }
893
DumpMaps(std::ostream & os,bool terse)894 void MemMap::DumpMaps(std::ostream& os, bool terse) {
895 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
896 DumpMapsLocked(os, terse);
897 }
898
DumpMapsLocked(std::ostream & os,bool terse)899 void MemMap::DumpMapsLocked(std::ostream& os, bool terse) {
900 const auto& mem_maps = *gMaps;
901 if (!terse) {
902 os << mem_maps;
903 return;
904 }
905
906 // Terse output example:
907 // [MemMap: 0x409be000+0x20P~0x11dP+0x20P~0x61cP+0x20P prot=0x3 LinearAlloc]
908 // [MemMap: 0x451d6000+0x6bP(3) prot=0x3 large object space allocation]
909 // The details:
910 // "+0x20P" means 0x20 pages taken by a single mapping,
911 // "~0x11dP" means a gap of 0x11d pages,
912 // "+0x6bP(3)" means 3 mappings one after another, together taking 0x6b pages.
913 os << "MemMap:" << std::endl;
914 for (auto it = mem_maps.begin(), maps_end = mem_maps.end(); it != maps_end;) {
915 MemMap* map = it->second;
916 void* base = it->first;
917 CHECK_EQ(base, map->BaseBegin());
918 os << "[MemMap: " << base;
919 ++it;
920 // Merge consecutive maps with the same protect flags and name.
921 constexpr size_t kMaxGaps = 9;
922 size_t num_gaps = 0;
923 size_t num = 1u;
924 size_t size = map->BaseSize();
925 CHECK_ALIGNED(size, kPageSize);
926 void* end = map->BaseEnd();
927 while (it != maps_end &&
928 it->second->GetProtect() == map->GetProtect() &&
929 it->second->GetName() == map->GetName() &&
930 (it->second->BaseBegin() == end || num_gaps < kMaxGaps)) {
931 if (it->second->BaseBegin() != end) {
932 ++num_gaps;
933 os << "+0x" << std::hex << (size / kPageSize) << "P";
934 if (num != 1u) {
935 os << "(" << std::dec << num << ")";
936 }
937 size_t gap =
938 reinterpret_cast<uintptr_t>(it->second->BaseBegin()) - reinterpret_cast<uintptr_t>(end);
939 CHECK_ALIGNED(gap, kPageSize);
940 os << "~0x" << std::hex << (gap / kPageSize) << "P";
941 num = 0u;
942 size = 0u;
943 }
944 CHECK_ALIGNED(it->second->BaseSize(), kPageSize);
945 ++num;
946 size += it->second->BaseSize();
947 end = it->second->BaseEnd();
948 ++it;
949 }
950 os << "+0x" << std::hex << (size / kPageSize) << "P";
951 if (num != 1u) {
952 os << "(" << std::dec << num << ")";
953 }
954 os << " prot=0x" << std::hex << map->GetProtect() << " " << map->GetName() << "]" << std::endl;
955 }
956 }
957
HasMemMap(MemMap & map)958 bool MemMap::HasMemMap(MemMap& map) {
959 void* base_begin = map.BaseBegin();
960 for (auto it = gMaps->lower_bound(base_begin), end = gMaps->end();
961 it != end && it->first == base_begin; ++it) {
962 if (it->second == &map) {
963 return true;
964 }
965 }
966 return false;
967 }
968
GetLargestMemMapAt(void * address)969 MemMap* MemMap::GetLargestMemMapAt(void* address) {
970 size_t largest_size = 0;
971 MemMap* largest_map = nullptr;
972 DCHECK(gMaps != nullptr);
973 for (auto it = gMaps->lower_bound(address), end = gMaps->end();
974 it != end && it->first == address; ++it) {
975 MemMap* map = it->second;
976 CHECK(map != nullptr);
977 if (largest_size < map->BaseSize()) {
978 largest_size = map->BaseSize();
979 largest_map = map;
980 }
981 }
982 return largest_map;
983 }
984
Init()985 void MemMap::Init() {
986 if (mem_maps_lock_ != nullptr) {
987 // dex2oat calls MemMap::Init twice since its needed before the runtime is created.
988 return;
989 }
990 mem_maps_lock_ = new std::mutex();
991 // Not for thread safety, but for the annotation that gMaps is GUARDED_BY(mem_maps_lock_).
992 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
993 DCHECK(gMaps == nullptr);
994 gMaps = new Maps;
995
996 TargetMMapInit();
997 }
998
Shutdown()999 void MemMap::Shutdown() {
1000 if (mem_maps_lock_ == nullptr) {
1001 // If MemMap::Shutdown is called more than once, there is no effect.
1002 return;
1003 }
1004 {
1005 // Not for thread safety, but for the annotation that gMaps is GUARDED_BY(mem_maps_lock_).
1006 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
1007 DCHECK(gMaps != nullptr);
1008 delete gMaps;
1009 gMaps = nullptr;
1010 }
1011 delete mem_maps_lock_;
1012 mem_maps_lock_ = nullptr;
1013 }
1014
SetSize(size_t new_size)1015 void MemMap::SetSize(size_t new_size) {
1016 CHECK_LE(new_size, size_);
1017 size_t new_base_size = RoundUp(new_size + static_cast<size_t>(PointerDiff(Begin(), BaseBegin())),
1018 kPageSize);
1019 if (new_base_size == base_size_) {
1020 size_ = new_size;
1021 return;
1022 }
1023 CHECK_LT(new_base_size, base_size_);
1024 MEMORY_TOOL_MAKE_UNDEFINED(
1025 reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BaseBegin()) +
1026 new_base_size),
1027 base_size_ - new_base_size);
1028 CHECK_EQ(TargetMUnmap(reinterpret_cast<void*>(
1029 reinterpret_cast<uintptr_t>(BaseBegin()) + new_base_size),
1030 base_size_ - new_base_size), 0)
1031 << new_base_size << " " << base_size_;
1032 base_size_ = new_base_size;
1033 size_ = new_size;
1034 }
1035
MapInternalArtLow4GBAllocator(size_t length,int prot,int flags,int fd,off_t offset)1036 void* MemMap::MapInternalArtLow4GBAllocator(size_t length,
1037 int prot,
1038 int flags,
1039 int fd,
1040 off_t offset) {
1041 #if USE_ART_LOW_4G_ALLOCATOR
1042 void* actual = MAP_FAILED;
1043
1044 bool first_run = true;
1045
1046 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
1047 for (uintptr_t ptr = next_mem_pos_; ptr < 4 * GB; ptr += kPageSize) {
1048 // Use gMaps as an optimization to skip over large maps.
1049 // Find the first map which is address > ptr.
1050 auto it = gMaps->upper_bound(reinterpret_cast<void*>(ptr));
1051 if (it != gMaps->begin()) {
1052 auto before_it = it;
1053 --before_it;
1054 // Start at the end of the map before the upper bound.
1055 ptr = std::max(ptr, reinterpret_cast<uintptr_t>(before_it->second->BaseEnd()));
1056 CHECK_ALIGNED(ptr, kPageSize);
1057 }
1058 while (it != gMaps->end()) {
1059 // How much space do we have until the next map?
1060 size_t delta = reinterpret_cast<uintptr_t>(it->first) - ptr;
1061 // If the space may be sufficient, break out of the loop.
1062 if (delta >= length) {
1063 break;
1064 }
1065 // Otherwise, skip to the end of the map.
1066 ptr = reinterpret_cast<uintptr_t>(it->second->BaseEnd());
1067 CHECK_ALIGNED(ptr, kPageSize);
1068 ++it;
1069 }
1070
1071 // Try to see if we get lucky with this address since none of the ART maps overlap.
1072 actual = TryMemMapLow4GB(reinterpret_cast<void*>(ptr), length, prot, flags, fd, offset);
1073 if (actual != MAP_FAILED) {
1074 next_mem_pos_ = reinterpret_cast<uintptr_t>(actual) + length;
1075 return actual;
1076 }
1077
1078 if (4U * GB - ptr < length) {
1079 // Not enough memory until 4GB.
1080 if (first_run) {
1081 // Try another time from the bottom;
1082 ptr = LOW_MEM_START - kPageSize;
1083 first_run = false;
1084 continue;
1085 } else {
1086 // Second try failed.
1087 break;
1088 }
1089 }
1090
1091 uintptr_t tail_ptr;
1092
1093 // Check pages are free.
1094 bool safe = true;
1095 for (tail_ptr = ptr; tail_ptr < ptr + length; tail_ptr += kPageSize) {
1096 if (msync(reinterpret_cast<void*>(tail_ptr), kPageSize, 0) == 0) {
1097 safe = false;
1098 break;
1099 } else {
1100 DCHECK_EQ(errno, ENOMEM);
1101 }
1102 }
1103
1104 next_mem_pos_ = tail_ptr; // update early, as we break out when we found and mapped a region
1105
1106 if (safe == true) {
1107 actual = TryMemMapLow4GB(reinterpret_cast<void*>(ptr), length, prot, flags, fd, offset);
1108 if (actual != MAP_FAILED) {
1109 return actual;
1110 }
1111 } else {
1112 // Skip over last page.
1113 ptr = tail_ptr;
1114 }
1115 }
1116
1117 if (actual == MAP_FAILED) {
1118 LOG(ERROR) << "Could not find contiguous low-memory space.";
1119 errno = ENOMEM;
1120 }
1121 return actual;
1122 #else
1123 UNUSED(length, prot, flags, fd, offset);
1124 LOG(FATAL) << "Unreachable";
1125 UNREACHABLE();
1126 #endif
1127 }
1128
MapInternal(void * addr,size_t length,int prot,int flags,int fd,off_t offset,bool low_4gb)1129 void* MemMap::MapInternal(void* addr,
1130 size_t length,
1131 int prot,
1132 int flags,
1133 int fd,
1134 off_t offset,
1135 bool low_4gb) {
1136 #ifdef __LP64__
1137 // When requesting low_4g memory and having an expectation, the requested range should fit into
1138 // 4GB.
1139 if (low_4gb && (
1140 // Start out of bounds.
1141 (reinterpret_cast<uintptr_t>(addr) >> 32) != 0 ||
1142 // End out of bounds. For simplicity, this will fail for the last page of memory.
1143 ((reinterpret_cast<uintptr_t>(addr) + length) >> 32) != 0)) {
1144 LOG(ERROR) << "The requested address space (" << addr << ", "
1145 << reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) + length)
1146 << ") cannot fit in low_4gb";
1147 return MAP_FAILED;
1148 }
1149 #else
1150 UNUSED(low_4gb);
1151 #endif
1152 DCHECK_ALIGNED(length, kPageSize);
1153 // TODO:
1154 // A page allocator would be a useful abstraction here, as
1155 // 1) It is doubtful that MAP_32BIT on x86_64 is doing the right job for us
1156 void* actual = MAP_FAILED;
1157 #if USE_ART_LOW_4G_ALLOCATOR
1158 // MAP_32BIT only available on x86_64.
1159 if (low_4gb && addr == nullptr) {
1160 // The linear-scan allocator has an issue when executable pages are denied (e.g., by selinux
1161 // policies in sensitive processes). In that case, the error code will still be ENOMEM. So
1162 // the allocator will scan all low 4GB twice, and still fail. This is *very* slow.
1163 //
1164 // To avoid the issue, always map non-executable first, and mprotect if necessary.
1165 const int orig_prot = prot;
1166 const int prot_non_exec = prot & ~PROT_EXEC;
1167 actual = MapInternalArtLow4GBAllocator(length, prot_non_exec, flags, fd, offset);
1168
1169 if (actual == MAP_FAILED) {
1170 return MAP_FAILED;
1171 }
1172
1173 // See if we need to remap with the executable bit now.
1174 if (orig_prot != prot_non_exec) {
1175 if (mprotect(actual, length, orig_prot) != 0) {
1176 PLOG(ERROR) << "Could not protect to requested prot: " << orig_prot;
1177 TargetMUnmap(actual, length);
1178 errno = ENOMEM;
1179 return MAP_FAILED;
1180 }
1181 }
1182 return actual;
1183 }
1184
1185 actual = TargetMMap(addr, length, prot, flags, fd, offset);
1186 #else
1187 #if defined(__LP64__)
1188 if (low_4gb && addr == nullptr) {
1189 flags |= MAP_32BIT;
1190 }
1191 #endif
1192 actual = TargetMMap(addr, length, prot, flags, fd, offset);
1193 #endif
1194 return actual;
1195 }
1196
operator <<(std::ostream & os,const MemMap & mem_map)1197 std::ostream& operator<<(std::ostream& os, const MemMap& mem_map) {
1198 os << StringPrintf("[MemMap: %p-%p prot=0x%x %s]",
1199 mem_map.BaseBegin(), mem_map.BaseEnd(), mem_map.GetProtect(),
1200 mem_map.GetName().c_str());
1201 return os;
1202 }
1203
TryReadable()1204 void MemMap::TryReadable() {
1205 if (base_begin_ == nullptr && base_size_ == 0) {
1206 return;
1207 }
1208 CHECK_NE(prot_ & PROT_READ, 0);
1209 volatile uint8_t* begin = reinterpret_cast<volatile uint8_t*>(base_begin_);
1210 volatile uint8_t* end = begin + base_size_;
1211 DCHECK(IsAligned<kPageSize>(begin));
1212 DCHECK(IsAligned<kPageSize>(end));
1213 // Read the first byte of each page. Use volatile to prevent the compiler from optimizing away the
1214 // reads.
1215 for (volatile uint8_t* ptr = begin; ptr < end; ptr += kPageSize) {
1216 // This read could fault if protection wasn't set correctly.
1217 uint8_t value = *ptr;
1218 UNUSED(value);
1219 }
1220 }
1221
ZeroAndReleasePages(void * address,size_t length)1222 void ZeroAndReleasePages(void* address, size_t length) {
1223 if (length == 0) {
1224 return;
1225 }
1226 uint8_t* const mem_begin = reinterpret_cast<uint8_t*>(address);
1227 uint8_t* const mem_end = mem_begin + length;
1228 uint8_t* const page_begin = AlignUp(mem_begin, kPageSize);
1229 uint8_t* const page_end = AlignDown(mem_end, kPageSize);
1230 if (!kMadviseZeroes || page_begin >= page_end) {
1231 // No possible area to madvise.
1232 std::fill(mem_begin, mem_end, 0);
1233 } else {
1234 // Spans one or more pages.
1235 DCHECK_LE(mem_begin, page_begin);
1236 DCHECK_LE(page_begin, page_end);
1237 DCHECK_LE(page_end, mem_end);
1238 std::fill(mem_begin, page_begin, 0);
1239 #ifdef _WIN32
1240 LOG(WARNING) << "ZeroAndReleasePages does not madvise on Windows.";
1241 #else
1242 CHECK_NE(madvise(page_begin, page_end - page_begin, MADV_DONTNEED), -1) << "madvise failed";
1243 #endif
1244 std::fill(page_end, mem_end, 0);
1245 }
1246 }
1247
AlignBy(size_t size)1248 void MemMap::AlignBy(size_t size) {
1249 CHECK_EQ(begin_, base_begin_) << "Unsupported";
1250 CHECK_EQ(size_, base_size_) << "Unsupported";
1251 CHECK_GT(size, static_cast<size_t>(kPageSize));
1252 CHECK_ALIGNED(size, kPageSize);
1253 CHECK(!reuse_);
1254 if (IsAlignedParam(reinterpret_cast<uintptr_t>(base_begin_), size) &&
1255 IsAlignedParam(base_size_, size)) {
1256 // Already aligned.
1257 return;
1258 }
1259 uint8_t* base_begin = reinterpret_cast<uint8_t*>(base_begin_);
1260 uint8_t* base_end = base_begin + base_size_;
1261 uint8_t* aligned_base_begin = AlignUp(base_begin, size);
1262 uint8_t* aligned_base_end = AlignDown(base_end, size);
1263 CHECK_LE(base_begin, aligned_base_begin);
1264 CHECK_LE(aligned_base_end, base_end);
1265 size_t aligned_base_size = aligned_base_end - aligned_base_begin;
1266 CHECK_LT(aligned_base_begin, aligned_base_end)
1267 << "base_begin = " << reinterpret_cast<void*>(base_begin)
1268 << " base_end = " << reinterpret_cast<void*>(base_end);
1269 CHECK_GE(aligned_base_size, size);
1270 // Unmap the unaligned parts.
1271 if (base_begin < aligned_base_begin) {
1272 MEMORY_TOOL_MAKE_UNDEFINED(base_begin, aligned_base_begin - base_begin);
1273 CHECK_EQ(TargetMUnmap(base_begin, aligned_base_begin - base_begin), 0)
1274 << "base_begin=" << reinterpret_cast<void*>(base_begin)
1275 << " aligned_base_begin=" << reinterpret_cast<void*>(aligned_base_begin);
1276 }
1277 if (aligned_base_end < base_end) {
1278 MEMORY_TOOL_MAKE_UNDEFINED(aligned_base_end, base_end - aligned_base_end);
1279 CHECK_EQ(TargetMUnmap(aligned_base_end, base_end - aligned_base_end), 0)
1280 << "base_end=" << reinterpret_cast<void*>(base_end)
1281 << " aligned_base_end=" << reinterpret_cast<void*>(aligned_base_end);
1282 }
1283 std::lock_guard<std::mutex> mu(*mem_maps_lock_);
1284 if (base_begin < aligned_base_begin) {
1285 auto it = GetGMapsEntry(*this);
1286 auto node = gMaps->extract(it);
1287 node.key() = aligned_base_begin;
1288 gMaps->insert(std::move(node));
1289 }
1290 base_begin_ = aligned_base_begin;
1291 base_size_ = aligned_base_size;
1292 begin_ = aligned_base_begin;
1293 size_ = aligned_base_size;
1294 DCHECK(gMaps != nullptr);
1295 }
1296
1297 } // namespace art
1298