TaggedAnonymousMemory.cpp (3060B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifdef XP_LINUX 8 9 # include "mozilla/TaggedAnonymousMemory.h" 10 11 # include <sys/types.h> 12 # include <sys/mman.h> 13 # include <sys/prctl.h> 14 # include <sys/syscall.h> 15 # include <unistd.h> 16 # include <stdint.h> 17 18 # include "mozilla/Assertions.h" 19 20 // These constants are copied from <sys/prctl.h>, because the headers 21 // used for building may not have them even though the running kernel 22 // supports them. 23 # ifndef PR_SET_VMA 24 # define PR_SET_VMA 0x53564d41 25 # endif 26 # ifndef PR_SET_VMA_ANON_NAME 27 # define PR_SET_VMA_ANON_NAME 0 28 # endif 29 30 namespace mozilla { 31 32 // Returns 0 for success and -1 (with errno) for error. 33 static int TagAnonymousMemoryAligned(const void* aPtr, size_t aLength, 34 const char* aTag) { 35 return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, 36 reinterpret_cast<unsigned long>(aPtr), aLength, 37 reinterpret_cast<unsigned long>(aTag)); 38 } 39 40 // On some architectures, it's possible for the page size to be larger 41 // than the PAGE_SIZE we were compiled with. This computes the 42 // equivalent of PAGE_MASK. 43 static uintptr_t GetPageMask() { 44 static uintptr_t mask = 0; 45 46 if (mask == 0) { 47 uintptr_t pageSize = sysconf(_SC_PAGESIZE); 48 mask = ~(pageSize - 1); 49 MOZ_ASSERT((pageSize & (pageSize - 1)) == 0, 50 "Page size must be a power of 2!"); 51 } 52 return mask; 53 } 54 55 } // namespace mozilla 56 57 void MozTagAnonymousMemory(const void* aPtr, size_t aLength, const char* aTag) { 58 // The kernel will round up the end of the range to the next page 59 // boundary if it's not aligned (comments indicate this behavior is 60 // based on that of madvise), but it will reject the request if the 61 // start is not aligned. We therefore round down the start address 62 // and adjust the length accordingly. 63 uintptr_t addr = reinterpret_cast<uintptr_t>(aPtr); 64 uintptr_t end = addr + aLength; 65 uintptr_t addrRounded = addr & mozilla::GetPageMask(); 66 const void* ptrRounded = reinterpret_cast<const void*>(addrRounded); 67 68 // Ignore the return value. TagAnonymousMemoryAligned will harmlessly fail on 69 // kernels without CONFIG_ANON_VMA_NAME. 70 mozilla::TagAnonymousMemoryAligned(ptrRounded, end - addrRounded, aTag); 71 } 72 73 void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt, int aFlags, 74 int aFd, off_t aOffset, const char* aTag) { 75 void* mapped = mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset); 76 if ((aFlags & MAP_ANONYMOUS) == MAP_ANONYMOUS && mapped != MAP_FAILED) { 77 // Ignore the return value. TagAnonymousMemoryAligned will harmlessly fail 78 // on kernels without CONFIG_ANON_VMA_NAME. 79 mozilla::TagAnonymousMemoryAligned(mapped, aLength, aTag); 80 } 81 return mapped; 82 } 83 84 #endif // XP_LINUX