| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "wtf/PageAllocator.h" | 31 #include "wtf/PageAllocator.h" |
| 32 | 32 |
| 33 #include "wtf/AddressSpaceRandomization.h" | 33 #include "wtf/AddressSpaceRandomization.h" |
| 34 #include "wtf/Assertions.h" | 34 #include "wtf/Assertions.h" |
| 35 | 35 |
| 36 #include <limits.h> | 36 #include <limits.h> |
| 37 | 37 |
| 38 #if OS(POSIX) | 38 #if OS(POSIX) |
| 39 | 39 |
| 40 #include <errno.h> |
| 40 #include <sys/mman.h> | 41 #include <sys/mman.h> |
| 41 | 42 |
| 42 #ifndef MADV_FREE | 43 #ifndef MADV_FREE |
| 43 #define MADV_FREE MADV_DONTNEED | 44 #define MADV_FREE MADV_DONTNEED |
| 44 #endif | 45 #endif |
| 45 | 46 |
| 46 #ifndef MAP_ANONYMOUS | 47 #ifndef MAP_ANONYMOUS |
| 47 #define MAP_ANONYMOUS MAP_ANON | 48 #define MAP_ANONYMOUS MAP_ANON |
| 48 #endif | 49 #endif |
| 49 | 50 |
| 50 // On POSIX memmap uses a nearby address if the hint address is blocked. | 51 // On POSIX memmap uses a nearby address if the hint address is blocked. |
| 51 static const bool kHintIsAdvisory = true; | 52 static const bool kHintIsAdvisory = true; |
| 53 static uint32_t allocPageErrorCode = 0; |
| 52 | 54 |
| 53 #elif OS(WIN) | 55 #elif OS(WIN) |
| 54 | 56 |
| 55 #include <windows.h> | 57 #include <windows.h> |
| 56 | 58 |
| 57 // VirtualAlloc will fail if allocation at the hint address is blocked. | 59 // VirtualAlloc will fail if allocation at the hint address is blocked. |
| 58 static const bool kHintIsAdvisory = false; | 60 static const bool kHintIsAdvisory = false; |
| 61 static uint32_t allocPageErrorCode = ERROR_SUCCESS; |
| 59 | 62 |
| 60 #else | 63 #else |
| 61 #error Unknown OS | 64 #error Unknown OS |
| 62 #endif // OS(POSIX) | 65 #endif // OS(POSIX) |
| 63 | 66 |
| 64 namespace WTF { | 67 namespace WTF { |
| 65 | 68 |
| 66 // This internal function wraps the OS-specific page allocation call. The | 69 // This internal function wraps the OS-specific page allocation call. The |
| 67 // behavior of the hint address is determined by the kHintIsAdvisory constant. | 70 // behavior of the hint address is determined by the kHintIsAdvisory constant. |
| 68 // If true, a non-zero hint is advisory and the returned address may differ from | 71 // If true, a non-zero hint is advisory and the returned address may differ from |
| 69 // the hint. If false, the hint is mandatory and a successful allocation will | 72 // the hint. If false, the hint is mandatory and a successful allocation will |
| 70 // not differ from the hint. | 73 // not differ from the hint. |
| 71 static void* systemAllocPages(void* hint, size_t len, PageAccessibilityConfigura
tion pageAccessibility) | 74 static void* systemAllocPages(void* hint, size_t len, PageAccessibilityConfigura
tion pageAccessibility) |
| 72 { | 75 { |
| 73 ASSERT(!(len & kPageAllocationGranularityOffsetMask)); | 76 ASSERT(!(len & kPageAllocationGranularityOffsetMask)); |
| 74 ASSERT(!(reinterpret_cast<uintptr_t>(hint) & kPageAllocationGranularityOffse
tMask)); | 77 ASSERT(!(reinterpret_cast<uintptr_t>(hint) & kPageAllocationGranularityOffse
tMask)); |
| 75 void* ret; | 78 void* ret; |
| 76 #if OS(WIN) | 79 #if OS(WIN) |
| 77 DWORD accessFlag = pageAccessibility == PageAccessible ? PAGE_READWRITE : PA
GE_NOACCESS; | 80 DWORD accessFlag = pageAccessibility == PageAccessible ? PAGE_READWRITE : PA
GE_NOACCESS; |
| 78 ret = VirtualAlloc(hint, len, MEM_RESERVE | MEM_COMMIT, accessFlag); | 81 ret = VirtualAlloc(hint, len, MEM_RESERVE | MEM_COMMIT, accessFlag); |
| 82 allocPageErrorCode = !ret ? GetLastError() : ERROR_SUCCESS; |
| 79 #else | 83 #else |
| 80 int accessFlag = pageAccessibility == PageAccessible ? (PROT_READ | PROT_WRI
TE) : PROT_NONE; | 84 int accessFlag = pageAccessibility == PageAccessible ? (PROT_READ | PROT_WRI
TE) : PROT_NONE; |
| 81 ret = mmap(hint, len, accessFlag, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | 85 ret = mmap(hint, len, accessFlag, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 82 if (ret == MAP_FAILED) | 86 if (ret == MAP_FAILED) { |
| 87 allocPageErrorCode = errno; |
| 83 ret = 0; | 88 ret = 0; |
| 89 } else { |
| 90 allocPageErrorCode = 0; |
| 91 } |
| 84 #endif | 92 #endif |
| 85 return ret; | 93 return ret; |
| 86 } | 94 } |
| 87 | 95 |
| 88 // Trims base to given length and alignment. Windows returns null on failure and
frees base. | 96 // Trims base to given length and alignment. Windows returns null on failure and
frees base. |
| 89 static void* trimMapping(void *base, size_t baseLen, size_t trimLen, uintptr_t a
lign, PageAccessibilityConfiguration pageAccessibility) | 97 static void* trimMapping(void *base, size_t baseLen, size_t trimLen, uintptr_t a
lign, PageAccessibilityConfiguration pageAccessibility) |
| 90 { | 98 { |
| 91 size_t preSlack = reinterpret_cast<uintptr_t>(base) & (align - 1); | 99 size_t preSlack = reinterpret_cast<uintptr_t>(base) & (align - 1); |
| 92 if (preSlack) | 100 if (preSlack) |
| 93 preSlack = align - preSlack; | 101 preSlack = align - preSlack; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 if (discardVirtualMemory) | 262 if (discardVirtualMemory) |
| 255 ret = discardVirtualMemory(addr, len); | 263 ret = discardVirtualMemory(addr, len); |
| 256 // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
failure. | 264 // DiscardVirtualMemory is buggy in Win10 SP0, so fall back to MEM_RESET on
failure. |
| 257 if (ret) { | 265 if (ret) { |
| 258 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); | 266 void* ret = VirtualAlloc(addr, len, MEM_RESET, PAGE_READWRITE); |
| 259 RELEASE_ASSERT(ret); | 267 RELEASE_ASSERT(ret); |
| 260 } | 268 } |
| 261 #endif | 269 #endif |
| 262 } | 270 } |
| 263 | 271 |
| 272 uint32_t getAllocPageErrorCode() |
| 273 { |
| 274 return allocPageErrorCode; |
| 275 } |
| 276 |
| 264 } // namespace WTF | 277 } // namespace WTF |
| 265 | 278 |
| OLD | NEW |