1db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom/* 2db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Copyright (C) 2008 The Android Open Source Project 3db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * 4db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 5db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * you may not use this file except in compliance with the License. 6db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * You may obtain a copy of the License at 7db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * 8db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 9db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * 10db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Unless required by applicable law or agreed to in writing, software 11db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 12db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * See the License for the specific language governing permissions and 14db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * limitations under the License. 15db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom */ 16db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_MEM_MAP_H_ 18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_MEM_MAP_H_ 19db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 200dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe#include <stddef.h> 210dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe#include <sys/types.h> 223eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 233eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi#include <map> 245573c37e795668eca81a8488078f798d977685c3Igor Murashkin#include <mutex> 250dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe#include <string> 261c23e1edb7361bbaec6e57fca86d8d3797960ad2Mathieu Chartier 270dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe#include "android-base/thread_annotations.h" 28db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 29db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstromnamespace art { 30db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 31651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe#if defined(__LP64__) && (defined(__aarch64__) || defined(__mips__) || defined(__APPLE__)) 32c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#define USE_ART_LOW_4G_ALLOCATOR 1 33c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#else 34651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe#if defined(__LP64__) && !defined(__x86_64__) 35651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe#error "Unrecognized 64-bit architecture." 36651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe#endif 37c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#define USE_ART_LOW_4G_ALLOCATOR 0 38c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#endif 39c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers 40c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#ifdef __linux__ 41c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogersstatic constexpr bool kMadviseZeroes = true; 42ca97ada4ec14373242de2452a6d0c16804007d66Alex Light#define HAVE_MREMAP_SYSCALL true 43c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#else 44c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogersstatic constexpr bool kMadviseZeroes = false; 45ca97ada4ec14373242de2452a6d0c16804007d66Alex Light// We cannot ever perform MemMap::ReplaceWith on non-linux hosts since the syscall is not 46ca97ada4ec14373242de2452a6d0c16804007d66Alex Light// present. 47ca97ada4ec14373242de2452a6d0c16804007d66Alex Light#define HAVE_MREMAP_SYSCALL false 48c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#endif 49c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers 50db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom// Used to keep track of mmap segments. 51d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// 52d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// On 64b systems not supporting MAP_32BIT, the implementation of MemMap will do a linear scan 53d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// for free pages. For security, the start of this scan should be randomized. This requires a 54d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// dynamic initializer. 55d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// For this to work, it is paramount that there are no other static initializers that access MemMap. 56d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// Otherwise, calls might see uninitialized values. 57db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstromclass MemMap { 58db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom public: 59ca97ada4ec14373242de2452a6d0c16804007d66Alex Light static constexpr bool kCanReplaceMapping = HAVE_MREMAP_SYSCALL; 60ca97ada4ec14373242de2452a6d0c16804007d66Alex Light 61ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // Replace the data in this memmmap with the data in the memmap pointed to by source. The caller 62ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // relinquishes ownership of the source mmap. 63ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // 64ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // For the call to be successful: 65ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * The range [dest->Begin, dest->Begin() + source->Size()] must not overlap with 66ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // [source->Begin(), source->End()]. 67ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * Neither source nor dest may be 'reused' mappings (they must own all the pages associated 68ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // with them. 69ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * kCanReplaceMapping must be true. 70ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * Neither source nor dest may use manual redzones. 71ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * Both source and dest must have the same offset from the nearest page boundary. 72ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * mremap must succeed when called on the mappings. 73ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // 74ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // If this call succeeds it will return true and: 75ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * Deallocate *source 76ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * Sets *source to nullptr 77ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * The protection of this will remain the same. 78ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * The size of this will be the size of the source 79ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // * The data in this will be the data from source. 80ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // 81ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // If this call fails it will return false and make no changes to *source or this. The ownership 82ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // of the source mmap is returned to the caller. 83ca97ada4ec14373242de2452a6d0c16804007d66Alex Light bool ReplaceWith(/*in-out*/MemMap** source, /*out*/std::string* error); 84ca97ada4ec14373242de2452a6d0c16804007d66Alex Light 85ecd3a6fc6e29d7dc4cb825c5282a8054ac52b8cfElliott Hughes // Request an anonymous region of length 'byte_count' and a requested base address. 862cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // Use null as the requested base address if you don't care. 875c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko // "reuse" allows re-mapping an address range from an existing mapping. 886c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes // 896c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes // The word "anonymous" in this context means "not backed by a file". The supplied 90a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray // 'name' will be used -- on systems that support it -- to give the mapping 916c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes // a name. 924a289ed61242964b921434de7d375f46480472a1Brian Carlstrom // 932cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // On success, returns returns a MemMap instance. On failure, returns null. 94a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray static MemMap* MapAnonymous(const char* name, 9542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier uint8_t* addr, 9642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier size_t byte_count, 9742bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int prot, 9842bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool low_4gb, 9942bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool reuse, 100a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray std::string* error_msg, 10158a73d2542f95dfd0535ceaa00eedefe198074a1Nicolas Geoffray bool use_ashmem = true); 102db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 1031baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky // Create placeholder for a region allocated by direct call to mmap. 1041baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky // This is useful when we do not have control over the code calling mmap, 1051baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky // but when we still want to keep track of it in the list. 1061baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky // The region is not considered to be owned and will not be unmmaped. 1071baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky static MemMap* MapDummy(const char* name, uint8_t* addr, size_t byte_count); 1081baabf0726eb285284e0c908ccba9f209b399faeDavid Srbecky 109db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom // Map part of a file, taking care of non-page aligned offsets. The 110db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom // "start" offset is absolute, not relative. 111db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom // 1122cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // On success, returns returns a MemMap instance. On failure, returns null. 11342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier static MemMap* MapFile(size_t byte_count, 11442bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int prot, 11542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int flags, 11642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int fd, 11742bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier off_t start, 11842bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool low_4gb, 11942bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier const char* filename, 12042bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier std::string* error_msg) { 12142bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier return MapFileAtAddress(nullptr, 12242bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier byte_count, 12342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier prot, 12442bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier flags, 12542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier fd, 12642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier start, 12742bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier /*low_4gb*/low_4gb, 12842bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier /*reuse*/false, 12942bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier filename, 13042bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier error_msg); 1314a289ed61242964b921434de7d375f46480472a1Brian Carlstrom } 1324a289ed61242964b921434de7d375f46480472a1Brian Carlstrom 133ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // Map part of a file, taking care of non-page aligned offsets. The "start" offset is absolute, 134ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // not relative. This version allows requesting a specific address for the base of the mapping. 135ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // "reuse" allows us to create a view into an existing mapping where we do not take ownership of 136ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // the memory. If error_msg is null then we do not print /proc/maps to the log if 137ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // MapFileAtAddress fails. This helps improve performance of the fail case since reading and 138ebe2dfcb1346d2010787ebbb15ae2965fcd2b223Mathieu Chartier // printing /proc/maps takes several milliseconds in the worst case. 1394a289ed61242964b921434de7d375f46480472a1Brian Carlstrom // 1402cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // On success, returns returns a MemMap instance. On failure, returns null. 14142bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier static MemMap* MapFileAtAddress(uint8_t* addr, 14242bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier size_t byte_count, 14342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int prot, 14442bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int flags, 14542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int fd, 14642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier off_t start, 14742bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool low_4gb, 14842bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool reuse, 14942bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier const char* filename, 1508d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers std::string* error_msg); 151db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 1522cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // Releases the memory mapping. 1531b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr ~MemMap() REQUIRES(!MemMap::mem_maps_lock_); 154db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 1550d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom const std::string& GetName() const { 1560d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom return name_; 1570d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom } 1580d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom 1599bdf108885a27ba05fae8501725649574d7c491bVladimir Marko bool Sync(); 1609bdf108885a27ba05fae8501725649574d7c491bVladimir Marko 161d88fa26892ad5ddebb7ab3407cdc574c54ed8258Logan Chien bool Protect(int prot); 162d88fa26892ad5ddebb7ab3407cdc574c54ed8258Logan Chien 163c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers void MadviseDontNeedAndZero(); 164c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers 1651c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers int GetProtect() const { 1661c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers return prot_; 1671c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers } 1681c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers 16913735955f39b3b304c37d2b2840663c131262c18Ian Rogers uint8_t* Begin() const { 17030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers return begin_; 171db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom } 172db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 17330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers size_t Size() const { 17430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers return size_; 175db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom } 176db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 177379d09fe3c3feb7c2a2fb5a3623689b5ace7e79bMathieu Chartier // Resize the mem-map by unmapping pages at the end. Currently only supports shrinking. 178379d09fe3c3feb7c2a2fb5a3623689b5ace7e79bMathieu Chartier void SetSize(size_t new_size); 179379d09fe3c3feb7c2a2fb5a3623689b5ace7e79bMathieu Chartier 18013735955f39b3b304c37d2b2840663c131262c18Ian Rogers uint8_t* End() const { 1812fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier return Begin() + Size(); 1822fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier } 1832fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier 1840d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom void* BaseBegin() const { 1850d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom return base_begin_; 1860d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom } 1870d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom 1880d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom size_t BaseSize() const { 1890d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom return base_size_; 1900d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom } 1910d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom 1920d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom void* BaseEnd() const { 19313735955f39b3b304c37d2b2840663c131262c18Ian Rogers return reinterpret_cast<uint8_t*>(BaseBegin()) + BaseSize(); 1940d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom } 1950d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom 1962fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier bool HasAddress(const void* addr) const { 1972fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier return Begin() <= addr && addr < End(); 198b765be0d656c3073402693aeaf64e95a0e49f218Brian Carlstrom } 199b765be0d656c3073402693aeaf64e95a0e49f218Brian Carlstrom 200fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi // Unmap the pages at end and remap them to create another memory map. 20142bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier MemMap* RemapAtEnd(uint8_t* new_end, 20242bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier const char* tail_name, 20342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int tail_prot, 204a25dce9b452ba17ef7cef768926c884177a3025eNicolas Geoffray std::string* error_msg, 205dbd05fe1a6ed2c3e23c9f6b372dd439ad59e777bOrion Hodson bool use_ashmem = true); 206cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier 2073eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static bool CheckNoGaps(MemMap* begin_map, MemMap* end_map) 2081b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(!MemMap::mem_maps_lock_); 20917a924abde2b0f1f37f6008b451a0a75190c71ffVladimir Marko static void DumpMaps(std::ostream& os, bool terse = false) 2101b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(!MemMap::mem_maps_lock_); 2113eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 2121b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr // Init and Shutdown are NOT thread safe. 2131b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr // Both may be called multiple times and MemMap objects may be created any 2141b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr // time after the first call to Init and before the first call to Shutodwn. 2151b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr static void Init() REQUIRES(!MemMap::mem_maps_lock_); 2161b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr static void Shutdown() REQUIRES(!MemMap::mem_maps_lock_); 2176e88ef6b604a7a945a466784580c42e6554c1289Mathieu Chartier 2186edb9aecdbe8b31f7ae1d35f21d013631337a424Hiroshi Yamauchi // If the map is PROT_READ, try to read each page of the map to check it is in fact readable (not 2196edb9aecdbe8b31f7ae1d35f21d013631337a424Hiroshi Yamauchi // faulting). This is used to diagnose a bug b/19894268 where mprotect doesn't seem to be working 2206edb9aecdbe8b31f7ae1d35f21d013631337a424Hiroshi Yamauchi // intermittently. 2216edb9aecdbe8b31f7ae1d35f21d013631337a424Hiroshi Yamauchi void TryReadable(); 2226edb9aecdbe8b31f7ae1d35f21d013631337a424Hiroshi Yamauchi 2233c3c4a1da1e8c03e78813d175a9974fb9f1097eaHiroshi Yamauchi // Align the map by unmapping the unaligned parts at the lower and the higher ends. 2243c3c4a1da1e8c03e78813d175a9974fb9f1097eaHiroshi Yamauchi void AlignBy(size_t size); 2253c3c4a1da1e8c03e78813d175a9974fb9f1097eaHiroshi Yamauchi 2260dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe // For annotation reasons. 2270dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe static std::mutex* GetMemMapsLock() RETURN_CAPABILITY(mem_maps_lock_) { 2280dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe return nullptr; 2290dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe } 2300dfc315a096bf5678cb5182771e3fdcca8ea4433Andreas Gampe 231db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom private: 23242bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier MemMap(const std::string& name, 23342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier uint8_t* begin, 23442bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier size_t size, 23542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier void* base_begin, 23642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier size_t base_size, 23742bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int prot, 23842bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier bool reuse, 2391b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr size_t redzone_size = 0) REQUIRES(!MemMap::mem_maps_lock_); 2403eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi 24117a924abde2b0f1f37f6008b451a0a75190c71ffVladimir Marko static void DumpMapsLocked(std::ostream& os, bool terse) 2421b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(MemMap::mem_maps_lock_); 2433eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static bool HasMemMap(MemMap* map) 2441b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(MemMap::mem_maps_lock_); 2453eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi static MemMap* GetLargestMemMapAt(void* address) 2461b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(MemMap::mem_maps_lock_); 247e58991b3b2282b5761f1a6023a16c803e1c4eb45Mathieu Chartier static bool ContainedWithinExistingMap(uint8_t* ptr, size_t size, std::string* error_msg) 2481b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr REQUIRES(!MemMap::mem_maps_lock_); 249db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 25042bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier // Internal version of mmap that supports low 4gb emulation. 25142bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier static void* MapInternal(void* addr, 25242bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier size_t length, 25342bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int prot, 25442bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int flags, 25542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier int fd, 25642bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier off_t offset, 257651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe bool low_4gb) 258651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe REQUIRES(!MemMap::mem_maps_lock_); 259651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe static void* MapInternalArtLow4GBAllocator(size_t length, 260651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe int prot, 261651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe int flags, 262651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe int fd, 263651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe off_t offset) 264651ba599961ff0cdc36e83d3d58b1744d37ee9f7Andreas Gampe REQUIRES(!MemMap::mem_maps_lock_); 26542bddcec51e71d206f6d3b30a881ee6c1d50a63cMathieu Chartier 266a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo const std::string name_; 2673c3c4a1da1e8c03e78813d175a9974fb9f1097eaHiroshi Yamauchi uint8_t* begin_; // Start of data. May be changed by AlignBy. 268cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier size_t size_; // Length of data. 269db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 2703c3c4a1da1e8c03e78813d175a9974fb9f1097eaHiroshi Yamauchi void* base_begin_; // Page-aligned base address. May be changed by AlignBy. 271fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi size_t base_size_; // Length of mapping. May be changed by RemapAtEnd (ie Zygote). 2721c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers int prot_; // Protection of the map. 273fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi 274a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo // When reuse_ is true, this is just a view of an existing mapping 275a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo // and we do not take ownership and are not responsible for 276a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo // unmapping. 277a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo const bool reuse_; 278a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo 279ca97ada4ec14373242de2452a6d0c16804007d66Alex Light // When already_unmapped_ is true the destructor will not call munmap. 280ca97ada4ec14373242de2452a6d0c16804007d66Alex Light bool already_unmapped_; 281ca97ada4ec14373242de2452a6d0c16804007d66Alex Light 2821e13374baf7dfaf442ffbf9809c37c131d681eafEvgenii Stepanov const size_t redzone_size_; 2831e13374baf7dfaf442ffbf9809c37c131d681eafEvgenii Stepanov 284c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#if USE_ART_LOW_4G_ALLOCATOR 285c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers static uintptr_t next_mem_pos_; // Next memory location to check for low_4g extent. 2868dba5aaaffc0bc2b2580bf02f0d9095c00d26a17Stuart Monteith#endif 2878dba5aaaffc0bc2b2580bf02f0d9095c00d26a17Stuart Monteith 2881b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr static std::mutex* mem_maps_lock_; 2891b14fb8bc459c3f91ed9fc1ff2e6c011c3d0a0f9David Sehr 290fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi friend class MemMapTest; // To allow access to base_begin_ and base_size_. 291db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom}; 2926e6078ab06e16bb4032b363316360f3b76685e30Mathieu Chartier 2930d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstromstd::ostream& operator<<(std::ostream& os, const MemMap& mem_map); 294db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 2956e6078ab06e16bb4032b363316360f3b76685e30Mathieu Chartier// Zero and release pages if possible, no requirements on alignments. 2966e6078ab06e16bb4032b363316360f3b76685e30Mathieu Chartiervoid ZeroAndReleasePages(void* address, size_t length); 2976e6078ab06e16bb4032b363316360f3b76685e30Mathieu Chartier 298db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom} // namespace art 299db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom 300fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_MEM_MAP_H_ 301