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 "mozilla/Attributes.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 static const size_t MaxCodeBytesPerProcess = 1 * 1024 * 1024 * 1024;
21 #endif
22 
23 // Executable code is allocated in 64K chunks. ExecutableAllocator uses pools
24 // that are at least this big. Code we allocate does not necessarily have 64K
25 // alignment though.
26 static const size_t ExecutableCodePageSize = 64 * 1024;
27 
28 enum class ProtectionSetting {
29   Protected,  // Not readable, writable, or executable.
30   Writable,
31   Executable,
32 };
33 
34 extern MOZ_MUST_USE bool ReprotectRegion(void* start, size_t size,
35                                          ProtectionSetting protection);
36 
37 // Functions called at process start-up/shutdown to initialize/release the
38 // executable memory region.
39 extern MOZ_MUST_USE bool InitProcessExecutableMemory();
40 extern void ReleaseProcessExecutableMemory();
41 
42 // Allocate/deallocate executable pages.
43 extern void* AllocateExecutableMemory(size_t bytes,
44                                       ProtectionSetting protection);
45 extern void DeallocateExecutableMemory(void* addr, size_t bytes);
46 
47 // Returns true if we can allocate a few more MB of executable code without
48 // hitting our code limit. This function can be used to stop compiling things
49 // that are optional (like Baseline and Ion code) when we're about to reach the
50 // limit, so we are less likely to OOM or crash. Note that the limit is
51 // per-process, so other threads can also allocate code after we call this
52 // function.
53 extern bool CanLikelyAllocateMoreExecutableMemory();
54 
55 // Returns a rough guess of how much executable memory remains available,
56 // rounded down to MB limit.  Note this can fluctuate as other threads within
57 // the process allocate executable memory.
58 extern size_t LikelyAvailableExecutableMemory();
59 
60 }  // namespace jit
61 }  // namespace js
62 
63 #endif  // jit_ProcessExecutableMemory_h
64