1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #if !V8_ENABLE_WEBASSEMBLY 6 #error This header should only be included if WebAssembly is enabled. 7 #endif // !V8_ENABLE_WEBASSEMBLY 8 9 #ifndef V8_WASM_WASM_LIMITS_H_ 10 #define V8_WASM_WASM_LIMITS_H_ 11 12 #include <cstddef> 13 #include <cstdint> 14 #include <limits> 15 16 #include "src/base/macros.h" 17 #include "src/wasm/wasm-constants.h" 18 19 namespace v8 { 20 namespace internal { 21 namespace wasm { 22 23 // This constant limits the amount of *declared* memory. At runtime, memory can 24 // only grow up to kV8MaxWasmMemoryPages. 25 constexpr size_t kSpecMaxMemoryPages = 65536; 26 27 // The following limits are imposed by V8 on WebAssembly modules. 28 // The limits are agreed upon with other engines for consistency. 29 constexpr size_t kV8MaxWasmTypes = 1000000; 30 constexpr size_t kV8MaxWasmFunctions = 1000000; 31 constexpr size_t kV8MaxWasmImports = 100000; 32 constexpr size_t kV8MaxWasmExports = 100000; 33 constexpr size_t kV8MaxWasmGlobals = 1000000; 34 constexpr size_t kV8MaxWasmTags = 1000000; 35 constexpr size_t kV8MaxWasmExceptionTypes = 1000000; 36 constexpr size_t kV8MaxWasmDataSegments = 100000; 37 // This indicates the maximum memory size our implementation supports. 38 // Do not use this limit directly; use {max_mem_pages()} instead to take the 39 // spec'ed limit as well as command line flag into account. 40 // Also, do not use this limit to validate declared memory, use 41 // kSpecMaxMemoryPages for that. 42 constexpr size_t kV8MaxWasmMemoryPages = kSystemPointerSize == 4 43 ? 32767 // = 2 GiB - 64Kib 44 : 65536; // = 4 GiB 45 constexpr size_t kV8MaxWasmStringSize = 100000; 46 constexpr size_t kV8MaxWasmModuleSize = 1024 * 1024 * 1024; // = 1 GiB 47 constexpr size_t kV8MaxWasmFunctionSize = 7654321; 48 constexpr size_t kV8MaxWasmFunctionLocals = 50000; 49 constexpr size_t kV8MaxWasmFunctionParams = 1000; 50 constexpr size_t kV8MaxWasmFunctionReturns = 1000; 51 constexpr size_t kV8MaxWasmFunctionBrTableSize = 65520; 52 // Don't use this limit directly, but use the value of FLAG_wasm_max_table_size. 53 constexpr size_t kV8MaxWasmTableSize = 10000000; 54 constexpr size_t kV8MaxWasmTableInitEntries = 10000000; 55 constexpr size_t kV8MaxWasmTables = 100000; 56 constexpr size_t kV8MaxWasmMemories = 1; 57 58 // GC proposal. These limits are not standardized yet. 59 constexpr size_t kV8MaxWasmStructFields = 999; 60 constexpr uint32_t kV8MaxRttSubtypingDepth = 31; 61 constexpr size_t kV8MaxWasmArrayInitLength = 999; 62 63 static_assert(kV8MaxWasmTableSize <= 4294967295, // 2^32 - 1 64 "v8 should not exceed WebAssembly's non-web embedding limits"); 65 static_assert(kV8MaxWasmTableInitEntries <= kV8MaxWasmTableSize, 66 "JS-API should not exceed v8's limit"); 67 68 constexpr uint64_t kWasmMaxHeapOffset = 69 static_cast<uint64_t>( 70 std::numeric_limits<uint32_t>::max()) // maximum base value 71 + std::numeric_limits<uint32_t>::max(); // maximum index value 72 73 // The following functions are defined in wasm-engine.cc. 74 75 // Maximum number of pages we can allocate. This might be lower than the number 76 // of pages that can be declared (e.g. as maximum): kSpecMaxMemoryPages. 77 // TODO(wasm): Make this size_t for wasm64. Currently the --wasm-max-mem-pages 78 // flag is only uint32_t. 79 V8_EXPORT_PRIVATE uint32_t max_mem_pages(); 80 max_mem_bytes()81inline uint64_t max_mem_bytes() { 82 return uint64_t{max_mem_pages()} * kWasmPageSize; 83 } 84 85 uint32_t max_table_init_entries(); 86 size_t max_module_size(); 87 88 } // namespace wasm 89 } // namespace internal 90 } // namespace v8 91 92 #endif // V8_WASM_WASM_LIMITS_H_ 93