Debian Codegraph @

js/src/wasm/WasmMemory.cpp

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 2021 Mozilla Foundation
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 #include "wasm/WasmMemory.h"
20
21 #include "mozilla/MathAlgorithms.h"
22
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"
28
29 using mozilla::IsPowerOfTwo;
30
31 using namespace js;
32 using namespace js::wasm;
33
34 const char* wasm::ToString(AddressType addressType) {
35 switch (addressType) {
36 case AddressType::I32:
37 return "i32";
38 case AddressType::I64:
39 return "i64";
40 default:
41 MOZ_CRASH();
42 }
43 }
44
45 bool wasm::ToAddressType(JSContext* cx, HandleValue value,
46 AddressType* addressType) {
47 RootedString typeStr(cx, ToString(cx, value));
48 if (!typeStr) {
49 return false;
50 }
51
52 Rooted<JSLinearString*> typeLinearStr(cx, typeStr->ensureLinear(cx));
53 if (!typeLinearStr) {
54 return false;
55 }
56
57 if (StringEqualsLiteral(typeLinearStr, "i32")) {
58 *addressType = AddressType::I32;
59 } else if (StringEqualsLiteral(typeLinearStr, "i64")) {
60 *addressType = AddressType::I64;
61 } else {
62 JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
63 JSMSG_WASM_BAD_STRING_ADDR_TYPE);
64 return false;
65 }
66 return true;
67 }
68
69 /*
70 * [SMDOC] Linear memory addresses and bounds checking
71 *
72 * (Also see "WASM Linear Memory structure" in vm/ArrayBufferObject.cpp)
73 *
74 *
75 * ## Memory addresses
76 *
77 * A memory address in an access instruction has three components, the "memory
78 * base", the "address", and the "offset". The "memory base" (the HeapReg on
79 * most platforms and a value loaded from the instance on x86) is a native
80 * pointer to the start of the linear memory array; we'll ignore the memory base
81 * in the following. The "address" is the i32 or i64 address into linear memory
82 * from the WebAssembly program; it is usually variable but can be constant. The
83 * "offset" is a constant immediate to the access instruction. For example,
84 * consider the following instructions:
85 *
86 * i32.const 128
87 * f32.load offset=8
88 *
89 * The address is 128; the offset is 8. The memory base is not observable to
90 * wasm. Note that the address comes from wasm value stack, but the offset is an
91 * immediate.
92 *
93 * The "effective address" (EA) is the non-overflowed sum of the address and the
94 * offset. (If the sum overflows, the program traps.) For the above, the
95 * effective address is 136.
96 *
97 * An access has an "access size", which is the number of bytes that are
98 * accessed - currently up to 16 (for V128). The highest-addressed byte to be
99 * accessed is thus the byte at (address + offset + access_size - 1). Note that
100 * (offset + access_size - 1) can be evaluated at compile time.
101 *
102 * Bounds checking ensures that the entire access is in bounds, i.e. that the
103 * highest-addressed byte is within the memory's current byteLength.
104 *
105 *
106 * ## Bounds check avoidance
107 *
108 * To avoid performing an addition with overflow check and a compare-and-branch
109 * bounds check for every memory access, we use some tricks:
110 *
111 * - We allocate an access-protected guard region of size R at the end of each
112 * memory to trap out-of-bounds offsets in the range 0..R-access_size. Thus,
113 * the offset and the access size can be omitted from the bounds check, saving
114 * the add and overflow check. For example, given the following module:
115 *
116 * (memory 1) ;; 1 page, 65536 bytes
117 * (func
118 * (f64.load offset=8 (i32.const 65528))
119 * )
120 *
121 * As long as the address itself is bounds checked, the offset will at worst
122 * cause the access to land in the guard region and trap via signal handling:
123 *
124 * Memory │ Guard Region
125 * ─ ─ ──────────┼────────┬──────── ─ ─
126 * │ access │
127 * ─ ─ ─┬────────┼────────┴──────── ─ ─
128 * 65528 65536
129 *
130 * Therefore, after bounds checking the address, the offset can be added into
131 * the address without an overflow check, either directly before the access or
132 * in the access instruction itself (depending on the ISA).
133 *
134 * This is the second part of the "SLOP" region as defined in "WASM Linear
135 * Memory structure" in ArrayBufferObject.cpp.
136 *
137 * - For 32-bit memories on 64-bit systems where we determine there is plenty of
138 * virtual memory space, we use "huge memories", in which we reserve 4GiB + R
139 * bytes of memory regardless of the memory's byteLength. Since the address
140 * itself has a 4GiB range, this allows us to skip bounds checks on the
141 * address as well. The extra R bytes of guard pages protect against
142 * out-of-bounds offsets as above.
143 *
144 * The offset can be added into the pointer (using 64-bit arithmetic) either
145 * directly before the access or in the access instruction.
146 *
147 * In both cases, accesses with offsets greater than R-access_size must be
148 * explicitly bounds checked in full, with an overflow check, since we cannot
149 * rely on the guard region.
150 *
151 * The value of R may vary depending on the memory allocation strategy and the
152 * amount of address space we can freely reserve. We do not document it here
153 * lest it be absurdly out of date. Search for "OffsetGuardLimit" if you wish.
154 *
155 * All memories in a process use the same strategy, selected at process startup.
156 * This is because the machine code embeds the strategy it's been compiled with,
157 * and may later be exposed to memories originating from different modules or
158 * directly from JS. If the memories did not all use the same strategy, we would
159 * have to recompile the code for each case.
160 *
161 *
162 * ## The boundsCheckLimit and the byteLength
163 *
164 * One would expect the boundsCheckLimit to always equal the memory's current
165 * byteLength. However, because the memory can grow, this means each bounds
166 * check must first load the boundsCheckLimit from the instance.
167 *
168 * We can sometimes avoid this load by observing that, even for non-huge
169 * memories, the signal handler is the final source of truth. In any case where
170 * we make a single memory reservation up front, we can set the boundsCheckLimit
171 * to the maximum possible byteLength. (For example, huge memories and memories
172 * with a max - anything that will NOT move on grow.)
173 *
174 *
175 * b.c. pass b.c. pass b.c. fail
176 * s.h. pass s.h. fail s.h. n/a
177 * ─ ─ ─────────────────┼─────────────────┼────────────── ─ ─
178 *
179 * ─ ─ ─────────────────────────────────────────────────────┐
180 * ─ ─ ─────────────────│─────────────────│─────────────────│
181 * byteLength boundsCheckLimit mappedSize
182 *
183 * ─ ─ ─────────────────┘
184 * COMMITTED
185 * └─────────────────┴─────────────────┘
186 * SLOP
187 *
188 *
189 * Note that this works even if byteLength later grows:
190 *
191 *
192 * b.c. pass b.c. fail
193 * s.h. pass s.h. n/a
194 * ─ ─ ───────────────────────────────────┼────────────── ─ ─
195 *
196 * ─ ─ ─────────────────────────────────────────────────────┐
197 * ─ ─ ───────────────────────────────────│─────────────────│
198 * byteLength mappedSize
199 * boundsCheckLimit
200 *
201 * ─ ─ ───────────────────────────────────┘
202 * COMMITTED
203 * └─────────────────┘
204 * SLOP
205 *
206 *
207 * Therefore, the boundsCheckLimit need only be greater than byteLength, not
208 * equal to byteLength, and the boundsCheckLimit need only be loaded once. This
209 * is the first part of the "SLOP" region as defined in "WASM Linear Memory
210 * structure" in ArrayBufferObject.cpp.
211 *
212 *
213 * ## Size of the boundsCheckLimit
214 *
215 * The boundsCheckLimit that is stored in the instance is always valid and is
216 * always a 64-bit value, and it is always correct to load it and use it as a
217 * 64-bit value. However, in situations when the 32 upper bits are known to be
218 * zero, it is also correct to load just the low 32 bits, and use that value as
219 * the limit. (This does not require a different address, since the limit is
220 * always little-endian when a JIT is enabled)
221 *
222 * On x86 and arm32 (and on any other 32-bit platform, should there ever be
223 * one), we always use explicit bounds checks, and the boundsCheckLimit can
224 * always be treated as a 32-bit quantity.
225 *
226 * On all 64-bit platforms, we may use explicit bounds checking or huge memories
227 * for memory32, but must always use explicit bounds checking for memory64. If
228 * the heap has a known maximum size that is less than 4GiB, then the
229 * boundsCheckLimit can be treated as a 32-bit quantity; otherwise it must be
230 * treated as a 64-bit quantity.
231 *
232 * Asm.js memories are limited to 2GB even on 64-bit platforms, and we can
233 * therefore always assume a 32-bit bounds check limit for asm.js.
234 *
235 *
236 * ## Constant pointers
237 *
238 * If the pointer is constant then the EA can be computed at compile time, and
239 * if (EA + access_size) is below the initial memory size, then the bounds check
240 * can always be elided.
241 *
242 *
243 * ## Alignment checks
244 *
245 * On all platforms, some accesses (currently atomics) require an alignment
246 * check: the EA must be naturally aligned for the datum being accessed.
247 * However, we do not need to compute the EA properly, we care only about the
248 * low bits - a cheap, overflowing add is fine, and if the offset is known to be
249 * aligned, only the address need be checked.
250 */
251
252 // Bounds checks always compare the base of the memory access with the bounds
253 // check limit. If the memory access is unaligned, this means that, even if the
254 // bounds check succeeds, a few bytes of the access can extend past the end of
255 // memory. To guard against this, extra space is included in the guard region to
256 // catch the overflow. MaxMemoryAccessSize is a conservative approximation of
257 // the maximum guard space needed to catch all unaligned overflows.
258 //
259 // Also see "Linear memory addresses and bounds checking" above.
260
261 static const unsigned MaxMemoryAccessSize = LitVal::sizeofLargestValue();
262
263 // All plausible targets must be able to do at least IEEE754 double
264 // loads/stores, hence the lower limit of 8. Some Intel processors support
265 // AVX-512 loads/stores, hence the upper limit of 64.
266 static_assert(MaxMemoryAccessSize >= 8, "MaxMemoryAccessSize too low");
267 static_assert(MaxMemoryAccessSize <= 64, "MaxMemoryAccessSize too high");
268 static_assert((MaxMemoryAccessSize & (MaxMemoryAccessSize - 1)) == 0,
269 "MaxMemoryAccessSize is not a power of two");
270
271 #ifdef WASM_SUPPORTS_HUGE_MEMORY
272
273 static_assert(MaxMemoryAccessSize <= HugeUnalignedGuardPage,
274 "rounded up to static page size");
275 static_assert(HugeOffsetGuardLimit < UINT32_MAX,
276 "checking for overflow against OffsetGuardLimit is enough.");
277
278 // We have only tested huge memory on x64, arm64 and riscv64.
279 # if !(defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM64) || \
280 defined(JS_CODEGEN_RISCV64))
281 # error "Not an expected configuration"
282 # endif
283
284 #endif
285
286 // On !WASM_SUPPORTS_HUGE_MEMORY platforms:
287 // - To avoid OOM in ArrayBuffer::prepareForAsmJS, asm.js continues to use the
288 // original ArrayBuffer allocation which has no guard region at all.
289 // - For WebAssembly memories, an additional GuardSize is mapped after the
290 // accessible region of the memory to catch folded (base+offset) accesses
291 // where `offset < OffsetGuardLimit` as well as the overflow from unaligned
292 // accesses, as described above for MaxMemoryAccessSize.
293
294 static const size_t OffsetGuardLimit = PageSize - MaxMemoryAccessSize;
295
296 static_assert(MaxMemoryAccessSize < GuardSize,
297 "Guard page handles partial out-of-bounds");
298 static_assert(OffsetGuardLimit < UINT32_MAX,
299 "checking for overflow against OffsetGuardLimit is enough.");
300
301 uint64_t wasm::GetMaxOffsetGuardLimit(bool hugeMemory) {
302 #ifdef WASM_SUPPORTS_HUGE_MEMORY
303 return hugeMemory ? HugeOffsetGuardLimit : OffsetGuardLimit;
304 #else
305 return OffsetGuardLimit;
306 #endif
307 }
308
309 // Assert that our minimum offset guard limit covers our inline
310 // memory.copy/fill optimizations.
311 static const size_t MinOffsetGuardLimit = OffsetGuardLimit;
312 static_assert(MaxInlineMemoryCopyLength < MinOffsetGuardLimit, "precondition");
313 static_assert(MaxInlineMemoryFillLength < MinOffsetGuardLimit, "precondition");
314
315 #ifdef JS_64BIT
316 wasm::Pages wasm::MaxMemoryPages(AddressType t) {
317 MOZ_ASSERT_IF(t == AddressType::I64, !IsHugeMemoryEnabled(t));
318 size_t desired = MaxMemoryPagesValidation(t);
319 constexpr size_t actual = ArrayBufferObject::ByteLengthLimit / PageSize;
320 return wasm::Pages(std::min(desired, actual));
321 }
322
323 size_t wasm::MaxMemoryBoundsCheckLimit(AddressType t) {
324 return MaxMemoryPages(t).byteLength();
325 }
326
327 #else
328 // On 32-bit systems, the heap limit must be representable in the nonnegative
329 // range of an int32_t, which means the maximum heap size as observed by wasm
330 // code is one wasm page less than 2GB.
331 wasm::Pages wasm::MaxMemoryPages(AddressType t) {
332 static_assert(ArrayBufferObject::ByteLengthLimit >= INT32_MAX / PageSize);
333 return wasm::Pages(INT32_MAX / PageSize);
334 }
335
336 // The max bounds check limit can be larger than the MaxMemoryPages because it
337 // is really MaxMemoryPages rounded up to the next valid bounds check immediate,
338 // see ComputeMappedSize().
339 size_t wasm::MaxMemoryBoundsCheckLimit(AddressType t) {
340 size_t boundsCheckLimit = size_t(INT32_MAX) + 1;
341 MOZ_ASSERT(IsValidBoundsCheckImmediate(boundsCheckLimit));
342 return boundsCheckLimit;
343 }
344 #endif
345
346 // Because ARM has a fixed-width instruction encoding, ARM can only express a
347 // limited subset of immediates (in a single instruction).
348
349 static const uint64_t HighestValidARMImmediate = 0xff000000;
350
351 // Heap length on ARM should fit in an ARM immediate. We approximate the set
352 // of valid ARM immediates with the predicate:
353 // 2^n for n in [16, 24)
354 // or
355 // 2^24 * n for n >= 1.
356 bool wasm::IsValidARMImmediate(uint32_t i) {
357 bool valid = (IsPowerOfTwo(i) || (i & 0x00ffffff) == 0);
358
359 MOZ_ASSERT_IF(valid, i % PageSize == 0);
360
361 return valid;
362 }
363
364 uint64_t wasm::RoundUpToNextValidARMImmediate(uint64_t i) {
365 MOZ_ASSERT(i <= HighestValidARMImmediate);
366 static_assert(HighestValidARMImmediate == 0xff000000,
367 "algorithm relies on specific constant");
368
369 if (i <= 16 * 1024 * 1024) {
370 i = i ? mozilla::RoundUpPow2(i) : 0;
371 } else {
372 i = (i + 0x00ffffff) & ~0x00ffffff;
373 }
374
375 MOZ_ASSERT(IsValidARMImmediate(i));
376
377 return i;
378 }
379
380 Pages wasm::ClampedMaxPages(AddressType t, Pages initialPages,
381 const mozilla::Maybe<Pages>& sourceMaxPages,
382 bool useHugeMemory) {
383 Pages clampedMaxPages;
384
385 if (sourceMaxPages.isSome()) {
386 // There is a specified maximum, clamp it to the implementation limit of
387 // maximum pages
388 clampedMaxPages = std::min(*sourceMaxPages, wasm::MaxMemoryPages(t));
389
390 #ifndef JS_64BIT
391 static_assert(sizeof(uintptr_t) == 4, "assuming not 64 bit implies 32 bit");
392
393 // On 32-bit platforms, prevent applications specifying a large max (like
394 // MaxMemoryPages()) from unintentially OOMing the browser: they just want
395 // "a lot of memory". Maintain the invariant that initialPages <=
396 // clampedMaxPages.
397 static const uint64_t OneGib = 1 << 30;
398 static const Pages OneGibPages = Pages(OneGib >> wasm::PageBits);
399 static_assert(HighestValidARMImmediate > OneGib,
400 "computing mapped size on ARM requires clamped max size");
401
402 Pages clampedPages = std::max(OneGibPages, initialPages);
403 clampedMaxPages = std::min(clampedPages, clampedMaxPages);
404 #endif
405 } else {
406 // There is not a specified maximum, fill it in with the implementation
407 // limit of maximum pages
408 clampedMaxPages = wasm::MaxMemoryPages(t);
409 }
410
411 // Double-check our invariants
412 MOZ_RELEASE_ASSERT(sourceMaxPages.isNothing() ||
413 clampedMaxPages <= *sourceMaxPages);
414 MOZ_RELEASE_ASSERT(clampedMaxPages <= wasm::MaxMemoryPages(t));
415 MOZ_RELEASE_ASSERT(initialPages <= clampedMaxPages);
416
417 return clampedMaxPages;
418 }
419
420 size_t wasm::ComputeMappedSize(wasm::Pages clampedMaxPages) {
421 // Caller is responsible to ensure that clampedMaxPages has been clamped to
422 // implementation limits.
423 size_t maxSize = clampedMaxPages.byteLength();
424
425 // It is the bounds-check limit, not the mapped size, that gets baked into
426 // code. Thus round up the maxSize to the next valid immediate value
427 // *before* adding in the guard page.
428 //
429 // Also see "Wasm Linear Memory Structure" in vm/ArrayBufferObject.cpp.
430 uint64_t boundsCheckLimit = RoundUpToNextValidBoundsCheckImmediate(maxSize);
431 MOZ_ASSERT(IsValidBoundsCheckImmediate(boundsCheckLimit));
432
433 MOZ_ASSERT(boundsCheckLimit % gc::SystemPageSize() == 0);
434 MOZ_ASSERT(GuardSize % gc::SystemPageSize() == 0);
435 return boundsCheckLimit + GuardSize;
436 }
437
438 bool wasm::IsValidBoundsCheckImmediate(uint32_t i) {
439 #ifdef JS_CODEGEN_ARM
440 return IsValidARMImmediate(i);
441 #else
442 return true;
443 #endif
444 }
445
446 uint64_t wasm::RoundUpToNextValidBoundsCheckImmediate(uint64_t i) {
447 #ifdef JS_CODEGEN_ARM
448 return RoundUpToNextValidARMImmediate(i);
449 #else
450 return i;
451 #endif
452 }