Debian Codegraph @

js/src/jit/ExecutableAllocator.h

raw
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 *
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef jit_ExecutableAllocator_h
29 #define jit_ExecutableAllocator_h
30
31 #include "mozilla/EnumeratedArray.h"
32
33 #include <limits>
34 #include <stddef.h> // for ptrdiff_t
35
36 #include "jit/ProcessExecutableMemory.h"
37 #include "js/AllocPolicy.h"
38 #include "js/HashTable.h"
39 #include "js/TypeDecls.h"
40 #include "js/Vector.h"
41
42 namespace JS {
43 struct CodeSizes;
44 } // namespace JS
45
46 namespace js {
47 namespace jit {
48
49 enum class CodeKind : uint8_t { Ion, Baseline, RegExp, Other, Count };
50
51 class ExecutableAllocator;
52
53 // These are reference-counted. A new one starts with a count of 1.
54 class ExecutablePool {
55 friend class ExecutableAllocator;
56
57 private:
58 struct Allocation {
59 char* pages;
60 size_t size;
61 };
62
63 ExecutableAllocator* m_allocator;
64 char* m_freePtr;
65 char* m_end;
66 Allocation m_allocation;
67
68 // Reference count for automatic reclamation.
69 unsigned m_refCount : 31;
70
71 // Flag that can be used by algorithms operating on pools.
72 bool m_mark : 1;
73
74 // Number of bytes currently allocated for each CodeKind.
75 mozilla::EnumeratedArray<CodeKind, size_t, size_t(CodeKind::Count)>
76 m_codeBytes;
77
78 public:
79 void release(bool willDestroy = false);
80 void release(size_t n, CodeKind kind);
81
82 void addRef();
83
84 ExecutablePool(ExecutableAllocator* allocator, Allocation a)
85 : m_allocator(allocator),
86 m_freePtr(a.pages),
87 m_end(m_freePtr + a.size),
88 m_allocation(a),
89 m_refCount(1),
90 m_mark(false) {
91 for (size_t& count : m_codeBytes) {
92 count = 0;
93 }
94 }
95
96 ~ExecutablePool();
97
98 void mark() {
99 MOZ_ASSERT(!m_mark);
100 m_mark = true;
101 }
102 void unmark() {
103 MOZ_ASSERT(m_mark);
104 m_mark = false;
105 }
106 bool isMarked() const { return m_mark; }
107
108 private:
109 ExecutablePool(const ExecutablePool&) = delete;
110 void operator=(const ExecutablePool&) = delete;
111
112 void* alloc(size_t n, CodeKind kind);
113
114 size_t available() const;
115
116 // Returns the number of bytes that are currently in use (referenced by
117 // live JitCode objects).
118 size_t usedCodeBytes() const {
119 size_t res = 0;
120 for (size_t count : m_codeBytes) {
121 res += count;
122 }
123 return res;
124 }
125 };
126
127 struct JitPoisonRange {
128 jit::ExecutablePool* pool;
129 void* start;
130 size_t size;
131
132 JitPoisonRange(jit::ExecutablePool* pool, void* start, size_t size)
133 : pool(pool), start(start), size(size) {}
134 };
135
136 using JitPoisonRangeVector = Vector<JitPoisonRange, 0, SystemAllocPolicy>;
137
138 class ExecutableAllocator {
139 public:
140 ExecutableAllocator() = default;
141 ~ExecutableAllocator();
142
143 void purge();
144
145 // alloc() returns a pointer to some memory, and also (by reference) a
146 // pointer to reference-counted pool. The caller owns a reference to the
147 // pool; i.e. alloc() increments the count before returning the object.
148 void* alloc(JSContext* cx, size_t n, ExecutablePool** poolp, CodeKind type);
149
150 void releasePoolPages(ExecutablePool* pool);
151
152 void addSizeOfCode(JS::CodeSizes* sizes) const;
153
154 private:
155 static const size_t OVERSIZE_ALLOCATION = size_t(-1);
156
157 static size_t roundUpAllocationSize(size_t request, size_t granularity);
158
159 // On OOM, this will return an Allocation where pages is nullptr.
160 ExecutablePool::Allocation systemAlloc(size_t n);
161 static void systemRelease(const ExecutablePool::Allocation& alloc);
162
163 ExecutablePool* createPool(size_t n);
164 ExecutablePool* poolForSize(size_t n);
165
166 static void reprotectPool(JSRuntime* rt, ExecutablePool* pool,
167 ProtectionSetting protection,
168 MustFlushICache flushICache);
169
170 public:
171 [[nodiscard]] static bool makeWritable(void* start, size_t size) {
172 return ReprotectRegion(start, size, ProtectionSetting::Writable,
173 MustFlushICache::No);
174 }
175
176 [[nodiscard]] static bool makeExecutableAndFlushICache(void* start,
177 size_t size) {
178 return ReprotectRegion(start, size, ProtectionSetting::Executable,
179 MustFlushICache::Yes);
180 }
181
182 static void poisonCode(JSRuntime* rt, JitPoisonRangeVector& ranges);
183
184 private:
185 ExecutableAllocator(const ExecutableAllocator&) = delete;
186 void operator=(const ExecutableAllocator&) = delete;
187
188 // These are strong references; they keep pools alive.
189 static const size_t maxSmallPools = 4;
190 using SmallExecPoolVector =
191 js::Vector<ExecutablePool*, maxSmallPools, js::SystemAllocPolicy>;
192 SmallExecPoolVector m_smallPools;
193
194 // All live pools are recorded here, just for stats purposes. These are
195 // weak references; they don't keep pools alive. When a pool is destroyed
196 // its reference is removed from m_pools.
197 using ExecPoolHashSet =
198 js::HashSet<ExecutablePool*, js::DefaultHasher<ExecutablePool*>,
199 js::SystemAllocPolicy>;
200 ExecPoolHashSet m_pools; // All pools, just for stats purposes.
201 };
202
203 } // namespace jit
204 } // namespace js
205
206 #endif /* jit_ExecutableAllocator_h */