| 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 |
#ifndef jit_ProcessExecutableMemory_h |
| 8 |
#define jit_ProcessExecutableMemory_h |
| 9 |
|
| 10 |
#include "util/Poison.h" |
| 11 |
|
| 12 |
namespace js { |
| 13 |
namespace jit { |
| 14 |
|
| 15 |
// Limit on the number of bytes of executable memory to prevent JIT spraying |
| 16 |
// attacks. |
| 17 |
#if JS_BITS_PER_WORD == 32 |
| 18 |
static const size_t MaxCodeBytesPerProcess = 140 * 1024 * 1024; |
| 19 |
#else |
| 20 |
// This is the largest number which satisfies various alignment static |
| 21 |
// asserts that is <= INT32_MAX. If we ever want to increase this, we need to |
| 22 |
// ensure RtlAddGrowableFunctionTable does the right thing because |
| 23 |
// RUNTIME_FUNCTION::EndAddress is a (32-bit) DWORD. |
| 24 |
static const size_t MaxCodeBytesPerProcess = 2044 * 1024 * 1024; |
| 25 |
#endif |
| 26 |
|
| 27 |
// Limit on the number of bytes of code memory per buffer. This limit comes |
| 28 |
// about because we encode an unresolved relative unconditional branch during |
| 29 |
// assembly as a branch instruction that carries the absolute offset of the next |
| 30 |
// branch instruction in the chain of branches that all reference the same |
| 31 |
// unresolved label. For this architecture to work, no branch instruction may |
| 32 |
// lie at an offset greater than the maximum forward branch distance. This is |
| 33 |
// true on both ARM and ARM64. |
| 34 |
// |
| 35 |
// Notably, even though we know that the offsets thus encoded are always |
| 36 |
// positive offsets, we use only the positive part of the signed range of the |
| 37 |
// branch offset. |
| 38 |
// |
| 39 |
// On ARM-32, we are limited by BOffImm::IsInRange(), which checks that the |
| 40 |
// offset is no greater than 2^25-4 in the offset's 26-bit signed field. |
| 41 |
// |
| 42 |
// On ARM-64, we are limited by Instruction::ImmBranchMaxForwardOffset(), which |
| 43 |
// checks that the offset is no greater than 2^27-4 in the offset's 28-bit |
| 44 |
// signed field. |
| 45 |
// |
| 46 |
// On MIPS, there are no limitations because the assembler has to implement |
| 47 |
// jump chaining to be effective at all (jump offsets are quite small). |
| 48 |
// |
| 49 |
// On x86 and x64, there are no limitations here because the assembler |
| 50 |
// MOZ_CRASHes if the 32-bit offset is exceeded. |
| 51 |
|
| 52 |
#if defined(JS_CODEGEN_ARM) |
| 53 |
static const size_t MaxCodeBytesPerBuffer = (1 << 25) - 4; |
| 54 |
#elif defined(JS_CODEGEN_ARM64) |
| 55 |
static const size_t MaxCodeBytesPerBuffer = (1 << 27) - 4; |
| 56 |
#else |
| 57 |
static const size_t MaxCodeBytesPerBuffer = MaxCodeBytesPerProcess; |
| 58 |
#endif |
| 59 |
|
| 60 |
// Executable code is allocated in 64K chunks. ExecutableAllocator uses pools |
| 61 |
// that are at least this big. Code we allocate does not necessarily have 64K |
| 62 |
// alignment though. |
| 63 |
static const size_t ExecutableCodePageSize = 64 * 1024; |
| 64 |
|
| 65 |
enum class ProtectionSetting { |
| 66 |
Writable, |
| 67 |
Executable, |
| 68 |
}; |
| 69 |
|
| 70 |
/// Whether the instruction cache must be flushed |
| 71 |
|
| 72 |
enum class MustFlushICache { No, Yes }; |
| 73 |
|
| 74 |
[[nodiscard]] extern bool ReprotectRegion(void* start, size_t size, |
| 75 |
ProtectionSetting protection, |
| 76 |
MustFlushICache flushICache); |
| 77 |
|
| 78 |
// Functions called at process start-up/shutdown to initialize/release the |
| 79 |
// executable memory region. |
| 80 |
[[nodiscard]] extern bool InitProcessExecutableMemory(); |
| 81 |
extern void ReleaseProcessExecutableMemory(); |
| 82 |
|
| 83 |
// Allocate/deallocate executable pages. |
| 84 |
extern void* AllocateExecutableMemory(size_t bytes, |
| 85 |
ProtectionSetting protection, |
| 86 |
MemCheckKind checkKind); |
| 87 |
extern void DeallocateExecutableMemory(void* addr, size_t bytes); |
| 88 |
|
| 89 |
// Returns true if we can allocate a few more MB of executable code without |
| 90 |
// hitting our code limit. This function can be used to stop compiling things |
| 91 |
// that are optional (like Baseline and Ion code) when we're about to reach the |
| 92 |
// limit, so we are less likely to OOM or crash. Note that the limit is |
| 93 |
// per-process, so other threads can also allocate code after we call this |
| 94 |
// function. |
| 95 |
extern bool CanLikelyAllocateMoreExecutableMemory(); |
| 96 |
|
| 97 |
// Returns a rough guess of how much executable memory remains available, |
| 98 |
// rounded down to MB limit. Note this can fluctuate as other threads within |
| 99 |
// the process allocate executable memory. |
| 100 |
extern size_t LikelyAvailableExecutableMemory(); |
| 101 |
|
| 102 |
// Returns whether |p| is stored in the executable code buffer. |
| 103 |
extern bool AddressIsInExecutableMemory(const void* p); |
| 104 |
|
| 105 |
// RWX page permissions are not supported on Apple Silicon. We have to use this |
| 106 |
// RAII class to temporarily mark JIT memory as writable for the current thread |
| 107 |
// with pthread_jit_write_protect_np. This class is a no-op on other platforms |
| 108 |
// (except for some debug assertions). |
| 109 |
class MOZ_RAII AutoMarkJitCodeWritableForThread { |
| 110 |
#ifdef DEBUG |
| 111 |
void checkConstructor(); |
| 112 |
void checkDestructor(); |
| 113 |
#else |
| 114 |
void checkConstructor() {} |
| 115 |
void checkDestructor() {} |
| 116 |
#endif |
| 117 |
|
| 118 |
#ifdef JS_USE_APPLE_FAST_WX |
| 119 |
void markExecutable(bool executable); |
| 120 |
#else |
| 121 |
void markExecutable(bool executable) {} |
| 122 |
#endif |
| 123 |
|
| 124 |
public: |
| 125 |
AutoMarkJitCodeWritableForThread() { |
| 126 |
markExecutable(false); |
| 127 |
checkConstructor(); |
| 128 |
} |
| 129 |
~AutoMarkJitCodeWritableForThread() { |
| 130 |
markExecutable(true); |
| 131 |
checkDestructor(); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
} // namespace jit |
| 136 |
} // namespace js |
| 137 |
|
| 138 |
#endif // jit_ProcessExecutableMemory_h |