no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / js / src / wasm / WasmMemory.cpp
blob01ef36e8a36ca18aa9cd3c11d6c92268f852f898
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:
4 * Copyright 2021 Mozilla Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "wasm/WasmMemory.h"
21 #include "mozilla/MathAlgorithms.h"
23 #include "js/Conversions.h"
24 #include "js/ErrorReport.h"
25 #include "vm/ArrayBufferObject.h"
26 #include "wasm/WasmCodegenTypes.h"
27 #include "wasm/WasmProcess.h"
29 using mozilla::IsPowerOfTwo;
31 using namespace js;
32 using namespace js::wasm;
34 const char* wasm::ToString(IndexType indexType) {
35 switch (indexType) {
36 case IndexType::I32:
37 return "i32";
38 case IndexType::I64:
39 return "i64";
40 default:
41 MOZ_CRASH();
45 bool wasm::ToIndexType(JSContext* cx, HandleValue value, IndexType* indexType) {
46 RootedString typeStr(cx, ToString(cx, value));
47 if (!typeStr) {
48 return false;
51 Rooted<JSLinearString*> typeLinearStr(cx, typeStr->ensureLinear(cx));
52 if (!typeLinearStr) {
53 return false;
56 if (StringEqualsLiteral(typeLinearStr, "i32")) {
57 *indexType = IndexType::I32;
58 } else if (StringEqualsLiteral(typeLinearStr, "i64")) {
59 *indexType = IndexType::I64;
60 } else {
61 JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
62 JSMSG_WASM_BAD_STRING_IDX_TYPE);
63 return false;
65 return true;
69 * [SMDOC] Linear memory addresses and bounds checking
71 * (Also see "WASM Linear Memory structure" in vm/ArrayBufferObject.cpp)
74 * Memory addresses, bounds check avoidance, and the huge memory trick.
76 * A memory address in an access instruction has three components, the "memory
77 * base", the "pointer", and the "offset". The "memory base" - the HeapReg on
78 * most platforms and a value loaded from the instance on x86 - is a native
79 * pointer that points to the start of the linear memory array; we'll ignore the
80 * memory base in the following. The "pointer" is the i32 or i64 index supplied
81 * by the program as a separate value argument to the access instruction; it is
82 * usually variable but can be constant. The "offset" is a constant encoded in
83 * the access instruction.
85 * The "effective address" (EA) is the non-overflowed sum of the pointer and the
86 * offset (if the sum overflows the program traps); the pointer, offset, and EA
87 * all have the same type, i32 or i64.
89 * An access has an "access size", which is the number of bytes that are
90 * accessed - currently up to 16 (for V128). The highest-addressed byte to be
91 * accessed by an access is thus the byte at (pointer+offset+access_size-1),
92 * where offset+access_size-1 is compile-time evaluable.
94 * Bounds checking ensures that the entire access is in bounds, ie, that the
95 * highest-addressed byte is at an offset in the linear memory below that of the
96 * memory's current byteLength.
98 * To avoid performing an addition with overflow check and a compare-and-branch
99 * bounds check for every memory access, we use some tricks:
101 * - An access-protected guard region of size R at the end of each memory is
102 * used to trap accesses to out-of-bounds offsets in the range
103 * 0..R-access_size. Thus the offset and the access size need not be added
104 * into the pointer before the bounds check, saving the add and overflow
105 * check. The offset is added into the pointer without an overflow check
106 * either directly before the access or in the access instruction itself
107 * (depending on the ISA). The pointer must still be explicitly
108 * bounds-checked.
110 * - On 64-bit systems where we determine there is plenty of virtual memory
111 * space (and ideally we determine that the VM system uses overcommit), a
112 * 32-bit memory is implemented as a 4GB + R reservation, where the memory
113 * from the current heap length through the end of the reservation is
114 * access-protected. The protected area R allows offsets up to R-access_size
115 * to be encoded in the access instruction. The pointer need not be bounds
116 * checked explicitly, since it has only a 4GB range and thus points into the
117 * 4GB part of the reservation. The offset can be added into the pointer
118 * (using 64-bit arithmetic) either directly before the access or in the
119 * access instruction.
121 * The value of R differs in the two situations; in the first case it tends to
122 * be small, currently 64KB; in the second case it is large, currently 2GB+64KB.
123 * The difference is due to explicit bounds checking tending to be used on
124 * 32-bit systems where memory and address space are scarce, while the implicit
125 * bounds check is used only on 64-bit systems after ensuring that sufficient
126 * address space is available in the process. (2GB is really overkill, and
127 * there's nothing magic about it; we could use something much smaller.)
129 * The implicit bounds checking strategy with the large reservation is known
130 * below and elsewhere as the "huge memory trick" or just "huge memory".
132 * All memories in a process use the same strategy, selected at process startup.
133 * The immediate reason for that is that the machine code embeds the strategy
134 * it's been compiled with, and may later be exposed to memories originating
135 * from different modules or directly from JS. If the memories did not all use
136 * the same strategy, and the same strategy as the code, linking would fail or
137 * we would have to recompile the code.
140 * The boundsCheckLimit.
142 * The bounds check limit that is stored in the instance is always valid and is
143 * always a 64-bit datum, and it is always correct to load it and use it as a
144 * 64-bit value. However, in situations when the 32 upper bits are known to be
145 * zero, it is also correct to load just the low 32 bits from the address of the
146 * limit (which is always little-endian when a JIT is enabled), and use that
147 * value as the limit.
149 * On x86 and arm32 (and on any other 32-bit platform, should there ever be
150 * one), there is explicit bounds checking and the heap, whether memory32 or
151 * memory64, is limited to 2GB; the bounds check limit can be treated as a
152 * 32-bit quantity.
154 * On all 64-bit platforms, we may use explicit bounds checking or the huge
155 * memory trick for memory32, but must always use explicit bounds checking for
156 * memory64. If the heap does not have a known maximum size or the known
157 * maximum is greater than or equal to 4GB, then the bounds check limit must be
158 * treated as a 64-bit quantity; otherwise it can be treated as a 32-bit
159 * quantity.
161 * On x64 and arm64 with Baseline and Ion, we allow 32-bit memories up to 4GB,
162 * and 64-bit memories can be larger.
164 * On mips64, memories are limited to 2GB, for now.
166 * Asm.js memories are limited to 2GB even on 64-bit platforms, and we can
167 * always assume a 32-bit bounds check limit for asm.js.
170 * Constant pointers.
172 * If the pointer is constant then the EA can be computed at compile time, and
173 * if the EA is below the initial memory size then the bounds check can be
174 * elided.
177 * Alignment checks.
179 * On all platforms, some accesses (currently atomics) require an alignment
180 * check: the EA must be naturally aligned for the datum being accessed.
181 * However, we do not need to compute the EA properly, we care only about the
182 * low bits - a cheap, overflowing add is fine, and if the offset is known
183 * to be aligned, only the pointer need be checked.
186 // Bounds checks always compare the base of the memory access with the bounds
187 // check limit. If the memory access is unaligned, this means that, even if the
188 // bounds check succeeds, a few bytes of the access can extend past the end of
189 // memory. To guard against this, extra space is included in the guard region to
190 // catch the overflow. MaxMemoryAccessSize is a conservative approximation of
191 // the maximum guard space needed to catch all unaligned overflows.
193 // Also see "Linear memory addresses and bounds checking" above.
195 static const unsigned MaxMemoryAccessSize = LitVal::sizeofLargestValue();
197 // All plausible targets must be able to do at least IEEE754 double
198 // loads/stores, hence the lower limit of 8. Some Intel processors support
199 // AVX-512 loads/stores, hence the upper limit of 64.
200 static_assert(MaxMemoryAccessSize >= 8, "MaxMemoryAccessSize too low");
201 static_assert(MaxMemoryAccessSize <= 64, "MaxMemoryAccessSize too high");
202 static_assert((MaxMemoryAccessSize & (MaxMemoryAccessSize - 1)) == 0,
203 "MaxMemoryAccessSize is not a power of two");
205 #ifdef WASM_SUPPORTS_HUGE_MEMORY
207 static_assert(MaxMemoryAccessSize <= HugeUnalignedGuardPage,
208 "rounded up to static page size");
209 static_assert(HugeOffsetGuardLimit < UINT32_MAX,
210 "checking for overflow against OffsetGuardLimit is enough.");
212 // We have only tested huge memory on x64, arm64 and riscv64.
213 # if !(defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM64) || \
214 defined(JS_CODEGEN_RISCV64))
215 # error "Not an expected configuration"
216 # endif
218 #endif
220 // On !WASM_SUPPORTS_HUGE_MEMORY platforms:
221 // - To avoid OOM in ArrayBuffer::prepareForAsmJS, asm.js continues to use the
222 // original ArrayBuffer allocation which has no guard region at all.
223 // - For WebAssembly memories, an additional GuardSize is mapped after the
224 // accessible region of the memory to catch folded (base+offset) accesses
225 // where `offset < OffsetGuardLimit` as well as the overflow from unaligned
226 // accesses, as described above for MaxMemoryAccessSize.
228 static const size_t OffsetGuardLimit = PageSize - MaxMemoryAccessSize;
230 static_assert(MaxMemoryAccessSize < GuardSize,
231 "Guard page handles partial out-of-bounds");
232 static_assert(OffsetGuardLimit < UINT32_MAX,
233 "checking for overflow against OffsetGuardLimit is enough.");
235 size_t wasm::GetMaxOffsetGuardLimit(bool hugeMemory) {
236 #ifdef WASM_SUPPORTS_HUGE_MEMORY
237 return hugeMemory ? HugeOffsetGuardLimit : OffsetGuardLimit;
238 #else
239 return OffsetGuardLimit;
240 #endif
243 // Assert that our minimum offset guard limit covers our inline
244 // memory.copy/fill optimizations.
245 static const size_t MinOffsetGuardLimit = OffsetGuardLimit;
246 static_assert(MaxInlineMemoryCopyLength < MinOffsetGuardLimit, "precondition");
247 static_assert(MaxInlineMemoryFillLength < MinOffsetGuardLimit, "precondition");
249 #ifdef JS_64BIT
250 wasm::Pages wasm::MaxMemoryPages(IndexType t) {
251 MOZ_ASSERT_IF(t == IndexType::I64, !IsHugeMemoryEnabled(t));
252 size_t desired = MaxMemoryLimitField(t);
253 constexpr size_t actual = ArrayBufferObject::ByteLengthLimit / PageSize;
254 return wasm::Pages(std::min(desired, actual));
257 size_t wasm::MaxMemoryBoundsCheckLimit(IndexType t) {
258 return MaxMemoryPages(t).byteLength();
261 #else
262 // On 32-bit systems, the heap limit must be representable in the nonnegative
263 // range of an int32_t, which means the maximum heap size as observed by wasm
264 // code is one wasm page less than 2GB.
265 wasm::Pages wasm::MaxMemoryPages(IndexType t) {
266 static_assert(ArrayBufferObject::ByteLengthLimit >= INT32_MAX / PageSize);
267 return wasm::Pages(INT32_MAX / PageSize);
270 // The max bounds check limit can be larger than the MaxMemoryPages because it
271 // is really MaxMemoryPages rounded up to the next valid bounds check immediate,
272 // see ComputeMappedSize().
273 size_t wasm::MaxMemoryBoundsCheckLimit(IndexType t) {
274 size_t boundsCheckLimit = size_t(INT32_MAX) + 1;
275 MOZ_ASSERT(IsValidBoundsCheckImmediate(boundsCheckLimit));
276 return boundsCheckLimit;
278 #endif
280 // Because ARM has a fixed-width instruction encoding, ARM can only express a
281 // limited subset of immediates (in a single instruction).
283 static const uint64_t HighestValidARMImmediate = 0xff000000;
285 // Heap length on ARM should fit in an ARM immediate. We approximate the set
286 // of valid ARM immediates with the predicate:
287 // 2^n for n in [16, 24)
288 // or
289 // 2^24 * n for n >= 1.
290 bool wasm::IsValidARMImmediate(uint32_t i) {
291 bool valid = (IsPowerOfTwo(i) || (i & 0x00ffffff) == 0);
293 MOZ_ASSERT_IF(valid, i % PageSize == 0);
295 return valid;
298 uint64_t wasm::RoundUpToNextValidARMImmediate(uint64_t i) {
299 MOZ_ASSERT(i <= HighestValidARMImmediate);
300 static_assert(HighestValidARMImmediate == 0xff000000,
301 "algorithm relies on specific constant");
303 if (i <= 16 * 1024 * 1024) {
304 i = i ? mozilla::RoundUpPow2(i) : 0;
305 } else {
306 i = (i + 0x00ffffff) & ~0x00ffffff;
309 MOZ_ASSERT(IsValidARMImmediate(i));
311 return i;
314 Pages wasm::ClampedMaxPages(IndexType t, Pages initialPages,
315 const Maybe<Pages>& sourceMaxPages,
316 bool useHugeMemory) {
317 Pages clampedMaxPages;
319 if (sourceMaxPages.isSome()) {
320 // There is a specified maximum, clamp it to the implementation limit of
321 // maximum pages
322 clampedMaxPages = std::min(*sourceMaxPages, wasm::MaxMemoryPages(t));
324 #ifndef JS_64BIT
325 static_assert(sizeof(uintptr_t) == 4, "assuming not 64 bit implies 32 bit");
327 // On 32-bit platforms, prevent applications specifying a large max (like
328 // MaxMemoryPages()) from unintentially OOMing the browser: they just want
329 // "a lot of memory". Maintain the invariant that initialPages <=
330 // clampedMaxPages.
331 static const uint64_t OneGib = 1 << 30;
332 static const Pages OneGibPages = Pages(OneGib >> wasm::PageBits);
333 static_assert(HighestValidARMImmediate > OneGib,
334 "computing mapped size on ARM requires clamped max size");
336 Pages clampedPages = std::max(OneGibPages, initialPages);
337 clampedMaxPages = std::min(clampedPages, clampedMaxPages);
338 #endif
339 } else {
340 // There is not a specified maximum, fill it in with the implementation
341 // limit of maximum pages
342 clampedMaxPages = wasm::MaxMemoryPages(t);
345 // Double-check our invariants
346 MOZ_RELEASE_ASSERT(sourceMaxPages.isNothing() ||
347 clampedMaxPages <= *sourceMaxPages);
348 MOZ_RELEASE_ASSERT(clampedMaxPages <= wasm::MaxMemoryPages(t));
349 MOZ_RELEASE_ASSERT(initialPages <= clampedMaxPages);
351 return clampedMaxPages;
354 size_t wasm::ComputeMappedSize(wasm::Pages clampedMaxPages) {
355 // Caller is responsible to ensure that clampedMaxPages has been clamped to
356 // implementation limits.
357 size_t maxSize = clampedMaxPages.byteLength();
359 // It is the bounds-check limit, not the mapped size, that gets baked into
360 // code. Thus round up the maxSize to the next valid immediate value
361 // *before* adding in the guard page.
363 // Also see "Wasm Linear Memory Structure" in vm/ArrayBufferObject.cpp.
364 uint64_t boundsCheckLimit = RoundUpToNextValidBoundsCheckImmediate(maxSize);
365 MOZ_ASSERT(IsValidBoundsCheckImmediate(boundsCheckLimit));
367 MOZ_ASSERT(boundsCheckLimit % gc::SystemPageSize() == 0);
368 MOZ_ASSERT(GuardSize % gc::SystemPageSize() == 0);
369 return boundsCheckLimit + GuardSize;
372 bool wasm::IsValidBoundsCheckImmediate(uint32_t i) {
373 #ifdef JS_CODEGEN_ARM
374 return IsValidARMImmediate(i);
375 #else
376 return true;
377 #endif
380 uint64_t wasm::RoundUpToNextValidBoundsCheckImmediate(uint64_t i) {
381 #ifdef JS_CODEGEN_ARM
382 return RoundUpToNextValidARMImmediate(i);
383 #else
384 return i;
385 #endif