Commit d9cfd775 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Move all V8-specific limitations to wasm-limits.h

R=clemensh@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2551463002
Cr-Commit-Position: refs/heads/master@{#41477}
parent 8f9bb9b8
...@@ -1748,6 +1748,7 @@ v8_source_set("v8_base") { ...@@ -1748,6 +1748,7 @@ v8_source_set("v8_base") {
"src/wasm/wasm-interpreter.h", "src/wasm/wasm-interpreter.h",
"src/wasm/wasm-js.cc", "src/wasm/wasm-js.cc",
"src/wasm/wasm-js.h", "src/wasm/wasm-js.h",
"src/wasm/wasm-limits.h",
"src/wasm/wasm-macro-gen.h", "src/wasm/wasm-macro-gen.h",
"src/wasm/wasm-module-builder.cc", "src/wasm/wasm-module-builder.cc",
"src/wasm/wasm-module-builder.h", "src/wasm/wasm-module-builder.h",
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "src/log-inl.h" #include "src/log-inl.h"
#include "src/wasm/ast-decoder.h" #include "src/wasm/ast-decoder.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h" #include "src/wasm/wasm-opcodes.h"
#include "src/wasm/wasm-text.h" #include "src/wasm/wasm-text.h"
...@@ -1723,9 +1724,8 @@ Node* WasmGraphBuilder::BuildFloatToIntConversionInstruction( ...@@ -1723,9 +1724,8 @@ Node* WasmGraphBuilder::BuildFloatToIntConversionInstruction(
Node* WasmGraphBuilder::GrowMemory(Node* input) { Node* WasmGraphBuilder::GrowMemory(Node* input) {
Diamond check_input_range( Diamond check_input_range(
graph(), jsgraph()->common(), graph(), jsgraph()->common(),
graph()->NewNode( graph()->NewNode(jsgraph()->machine()->Uint32LessThanOrEqual(), input,
jsgraph()->machine()->Uint32LessThanOrEqual(), input, jsgraph()->Uint32Constant(wasm::kV8MaxWasmMemoryPages)),
jsgraph()->Uint32Constant(wasm::WasmModule::kV8MaxPages)),
BranchHint::kTrue); BranchHint::kTrue);
check_input_range.Chain(*control_); check_input_range.Chain(*control_);
......
...@@ -1284,6 +1284,7 @@ ...@@ -1284,6 +1284,7 @@
'wasm/wasm-external-refs.h', 'wasm/wasm-external-refs.h',
'wasm/wasm-js.cc', 'wasm/wasm-js.cc',
'wasm/wasm-js.h', 'wasm/wasm-js.h',
'wasm/wasm-limits.h',
'wasm/wasm-macro-gen.h', 'wasm/wasm-macro-gen.h',
'wasm/wasm-module.cc', 'wasm/wasm-module.cc',
'wasm/wasm-module.h', 'wasm/wasm-module.h',
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "src/v8.h" #include "src/v8.h"
#include "src/wasm/decoder.h" #include "src/wasm/decoder.h"
#include "src/wasm/wasm-limits.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -312,18 +313,18 @@ class ModuleDecoder : public Decoder { ...@@ -312,18 +313,18 @@ class ModuleDecoder : public Decoder {
false, SignatureMap()}); false, SignatureMap()});
expect_u8("element type", kWasmAnyFunctionTypeForm); expect_u8("element type", kWasmAnyFunctionTypeForm);
WasmIndirectFunctionTable* table = &module->function_tables.back(); WasmIndirectFunctionTable* table = &module->function_tables.back();
consume_resizable_limits( consume_resizable_limits("element count", "elements",
"element count", "elements", WasmModule::kV8MaxTableSize, kV8MaxWasmTableSize, &table->min_size,
&table->min_size, &table->has_max, WasmModule::kV8MaxTableSize, &table->has_max, kV8MaxWasmTableSize,
&table->max_size); &table->max_size);
break; break;
} }
case kExternalMemory: { case kExternalMemory: {
// ===== Imported memory ========================================= // ===== Imported memory =========================================
bool has_max = false; bool has_max = false;
consume_resizable_limits("memory", "pages", WasmModule::kV8MaxPages, consume_resizable_limits("memory", "pages", kV8MaxWasmMemoryPages,
&module->min_mem_pages, &has_max, &module->min_mem_pages, &has_max,
WasmModule::kSpecMaxPages, kSpecMaxWasmMemoryPages,
&module->max_mem_pages); &module->max_mem_pages);
module->has_memory = true; module->has_memory = true;
break; break;
...@@ -387,10 +388,9 @@ class ModuleDecoder : public Decoder { ...@@ -387,10 +388,9 @@ class ModuleDecoder : public Decoder {
for (uint32_t i = 0; ok() && i < table_count; i++) { for (uint32_t i = 0; ok() && i < table_count; i++) {
WasmIndirectFunctionTable* table = &module->function_tables.back(); WasmIndirectFunctionTable* table = &module->function_tables.back();
expect_u8("table type", kWasmAnyFunctionTypeForm); expect_u8("table type", kWasmAnyFunctionTypeForm);
consume_resizable_limits("table elements", "elements", consume_resizable_limits(
WasmModule::kV8MaxTableSize, &table->min_size, "table elements", "elements", kV8MaxWasmTableSize, &table->min_size,
&table->has_max, WasmModule::kV8MaxTableSize, &table->has_max, kV8MaxWasmTableSize, &table->max_size);
&table->max_size);
} }
section_iter.advance(); section_iter.advance();
} }
...@@ -407,8 +407,8 @@ class ModuleDecoder : public Decoder { ...@@ -407,8 +407,8 @@ class ModuleDecoder : public Decoder {
for (uint32_t i = 0; ok() && i < memory_count; i++) { for (uint32_t i = 0; ok() && i < memory_count; i++) {
bool has_max = false; bool has_max = false;
consume_resizable_limits( consume_resizable_limits(
"memory", "pages", WasmModule::kV8MaxPages, &module->min_mem_pages, "memory", "pages", kV8MaxWasmMemoryPages, &module->min_mem_pages,
&has_max, WasmModule::kSpecMaxPages, &module->max_mem_pages); &has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages);
} }
module->has_memory = true; module->has_memory = true;
section_iter.advance(); section_iter.advance();
...@@ -1109,7 +1109,8 @@ ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start, ...@@ -1109,7 +1109,8 @@ ModuleResult DecodeWasmModule(Isolate* isolate, const byte* module_start,
isolate->counters()->wasm_decode_module_time()); isolate->counters()->wasm_decode_module_time());
size_t size = module_end - module_start; size_t size = module_end - module_start;
if (module_start > module_end) return ModuleError("start > end"); if (module_start > module_end) return ModuleError("start > end");
if (size >= kMaxModuleSize) return ModuleError("size > maximum module size"); if (size >= kV8MaxWasmModuleSize)
return ModuleError("size > maximum module size");
// TODO(bradnelson): Improve histogram handling of size_t. // TODO(bradnelson): Improve histogram handling of size_t.
isolate->counters()->wasm_module_size_bytes()->AddSample( isolate->counters()->wasm_module_size_bytes()->AddSample(
static_cast<int>(size)); static_cast<int>(size));
...@@ -1148,7 +1149,7 @@ FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, ...@@ -1148,7 +1149,7 @@ FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone,
isolate->counters()->wasm_decode_function_time()); isolate->counters()->wasm_decode_function_time());
size_t size = function_end - function_start; size_t size = function_end - function_start;
if (function_start > function_end) return FunctionError("start > end"); if (function_start > function_end) return FunctionError("start > end");
if (size > kMaxFunctionSize) if (size > kV8MaxWasmFunctionSize)
return FunctionError("size > maximum function size"); return FunctionError("size > maximum function size");
isolate->counters()->wasm_function_size_bytes()->AddSample( isolate->counters()->wasm_function_size_bytes()->AddSample(
static_cast<int>(size)); static_cast<int>(size));
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "src/wasm/ast-decoder.h" #include "src/wasm/ast-decoder.h"
#include "src/wasm/decoder.h" #include "src/wasm/decoder.h"
#include "src/wasm/wasm-external-refs.h" #include "src/wasm/wasm-external-refs.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/zone/accounting-allocator.h" #include "src/zone/accounting-allocator.h"
...@@ -663,7 +664,7 @@ static inline int32_t ExecuteGrowMemory(uint32_t delta_pages, ...@@ -663,7 +664,7 @@ static inline int32_t ExecuteGrowMemory(uint32_t delta_pages,
WasmInstance* instance) { WasmInstance* instance) {
// TODO(ahaas): Move memory allocation to wasm-module.cc for better // TODO(ahaas): Move memory allocation to wasm-module.cc for better
// encapsulation. // encapsulation.
if (delta_pages > wasm::WasmModule::kV8MaxPages) { if (delta_pages > wasm::kV8MaxWasmMemoryPages) {
return -1; return -1;
} }
uint32_t old_size = instance->mem_size; uint32_t old_size = instance->mem_size;
...@@ -679,8 +680,7 @@ static inline int32_t ExecuteGrowMemory(uint32_t delta_pages, ...@@ -679,8 +680,7 @@ static inline int32_t ExecuteGrowMemory(uint32_t delta_pages,
} else { } else {
DCHECK_NOT_NULL(instance->mem_start); DCHECK_NOT_NULL(instance->mem_start);
new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; new_size = old_size + delta_pages * wasm::WasmModule::kPageSize;
if (new_size > if (new_size > wasm::kV8MaxWasmMemoryPages * wasm::WasmModule::kPageSize) {
wasm::WasmModule::kV8MaxPages * wasm::WasmModule::kPageSize) {
return -1; return -1;
} }
new_mem_start = static_cast<byte*>(realloc(instance->mem_start, new_size)); new_mem_start = static_cast<byte*>(realloc(instance->mem_start, new_size));
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "src/wasm/module-decoder.h" #include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-js.h" #include "src/wasm/wasm-js.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects.h" #include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-result.h" #include "src/wasm/wasm-result.h"
...@@ -315,7 +316,7 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -315,7 +316,7 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) {
return; return;
} }
} else { } else {
maximum = static_cast<int>(i::wasm::WasmModule::kV8MaxTableSize); maximum = static_cast<int>(i::wasm::kV8MaxWasmTableSize);
} }
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
......
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_WASM_WASM_LIMITS_H_
#define V8_WASM_WASM_LIMITS_H_
namespace v8 {
namespace internal {
namespace wasm {
const size_t kV8MaxWasmSignatures = 10000000;
const size_t kV8MaxWasmFunctions = 10000000;
const size_t kV8MaxWasmMemoryPages = 16384; // = 1 GiB
const size_t kV8MaxWasmStringSize = 256;
const size_t kV8MaxWasmModuleSize = 1024 * 1024 * 1024; // = 1 GiB
const size_t kV8MaxWasmFunctionSize = 128 * 1024;
const size_t kV8MaxWasmFunctionLocals = 50000;
const size_t kV8MaxWasmTableSize = 16 * 1024 * 1024;
const size_t kSpecMaxWasmMemoryPages = 65536;
const uint64_t kWasmMaxHeapOffset =
static_cast<uint64_t>(
std::numeric_limits<uint32_t>::max()) // maximum base value
+ std::numeric_limits<uint32_t>::max(); // maximum index value
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_WASM_WASM_LIMITS_H_
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "src/wasm/ast-decoder.h" #include "src/wasm/ast-decoder.h"
#include "src/wasm/module-decoder.h" #include "src/wasm/module-decoder.h"
#include "src/wasm/wasm-js.h" #include "src/wasm/wasm-js.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects.h" #include "src/wasm/wasm-objects.h"
#include "src/wasm/wasm-result.h" #include "src/wasm/wasm-result.h"
...@@ -687,7 +688,7 @@ std::pair<int, int> GetFunctionOffsetAndLength( ...@@ -687,7 +688,7 @@ std::pair<int, int> GetFunctionOffsetAndLength(
Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size, Handle<JSArrayBuffer> wasm::NewArrayBuffer(Isolate* isolate, size_t size,
bool enable_guard_regions) { bool enable_guard_regions) {
if (size > (WasmModule::kV8MaxPages * WasmModule::kPageSize)) { if (size > (kV8MaxWasmMemoryPages * WasmModule::kPageSize)) {
// TODO(titzer): lift restriction on maximum memory allocated here. // TODO(titzer): lift restriction on maximum memory allocated here.
return Handle<JSArrayBuffer>::null(); return Handle<JSArrayBuffer>::null();
} }
...@@ -1690,7 +1691,7 @@ class WasmInstanceBuilder { ...@@ -1690,7 +1691,7 @@ class WasmInstanceBuilder {
// Allocate memory for a module instance as a new JSArrayBuffer. // Allocate memory for a module instance as a new JSArrayBuffer.
Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) { Handle<JSArrayBuffer> AllocateMemory(uint32_t min_mem_pages) {
if (min_mem_pages > WasmModule::kV8MaxPages) { if (min_mem_pages > kV8MaxWasmMemoryPages) {
thrower_->RangeError("Out of memory: wasm memory too large"); thrower_->RangeError("Out of memory: wasm memory too large");
return Handle<JSArrayBuffer>::null(); return Handle<JSArrayBuffer>::null();
} }
...@@ -1777,7 +1778,7 @@ class WasmInstanceBuilder { ...@@ -1777,7 +1778,7 @@ class WasmInstanceBuilder {
module_->function_tables[exp.index]; module_->function_tables[exp.index];
if (table_instance.table_object.is_null()) { if (table_instance.table_object.is_null()) {
uint32_t maximum = uint32_t maximum =
table.has_max ? table.max_size : WasmModule::kV8MaxTableSize; table.has_max ? table.max_size : kV8MaxWasmTableSize;
table_instance.table_object = WasmTableObject::New( table_instance.table_object = WasmTableObject::New(
isolate_, table.min_size, maximum, &table_instance.js_wrappers); isolate_, table.min_size, maximum, &table_instance.js_wrappers);
} }
...@@ -2145,7 +2146,7 @@ uint32_t GetMaxInstanceMemorySize(Isolate* isolate, ...@@ -2145,7 +2146,7 @@ uint32_t GetMaxInstanceMemorySize(Isolate* isolate,
isolate->counters()->wasm_max_mem_pages_count()->AddSample( isolate->counters()->wasm_max_mem_pages_count()->AddSample(
compiled_max_pages); compiled_max_pages);
if (compiled_max_pages != 0) return compiled_max_pages; if (compiled_max_pages != 0) return compiled_max_pages;
return WasmModule::kV8MaxPages; return kV8MaxWasmMemoryPages;
} }
Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate, Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
...@@ -2163,7 +2164,7 @@ Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate, ...@@ -2163,7 +2164,7 @@ Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
std::numeric_limits<uint32_t>::max()); std::numeric_limits<uint32_t>::max());
uint32_t new_size = old_size + pages * WasmModule::kPageSize; uint32_t new_size = old_size + pages * WasmModule::kPageSize;
if (new_size <= old_size || max_pages * WasmModule::kPageSize < new_size || if (new_size <= old_size || max_pages * WasmModule::kPageSize < new_size ||
WasmModule::kV8MaxPages * WasmModule::kPageSize < new_size) { kV8MaxWasmMemoryPages * WasmModule::kPageSize < new_size) {
return Handle<JSArrayBuffer>::null(); return Handle<JSArrayBuffer>::null();
} }
......
...@@ -32,20 +32,12 @@ class WasmCompilationUnit; ...@@ -32,20 +32,12 @@ class WasmCompilationUnit;
namespace wasm { namespace wasm {
class ErrorThrower; class ErrorThrower;
const size_t kMaxModuleSize = 1024 * 1024 * 1024;
const size_t kMaxFunctionSize = 128 * 1024;
const size_t kMaxStringSize = 256;
const uint32_t kWasmMagic = 0x6d736100; const uint32_t kWasmMagic = 0x6d736100;
const uint32_t kWasmVersion = 0x0d; const uint32_t kWasmVersion = 0x0d;
const uint8_t kWasmFunctionTypeForm = 0x60; const uint8_t kWasmFunctionTypeForm = 0x60;
const uint8_t kWasmAnyFunctionTypeForm = 0x70; const uint8_t kWasmAnyFunctionTypeForm = 0x70;
const uint64_t kWasmMaxHeapOffset =
static_cast<uint64_t>(
std::numeric_limits<uint32_t>::max()) // maximum base value
+ std::numeric_limits<uint32_t>::max(); // maximum index value
enum WasmSectionCode { enum WasmSectionCode {
kUnknownSectionCode = 0, // code for unknown sections kUnknownSectionCode = 0, // code for unknown sections
kTypeSectionCode = 1, // Function signature declarations kTypeSectionCode = 1, // Function signature declarations
...@@ -183,9 +175,6 @@ struct ModuleWireBytes; ...@@ -183,9 +175,6 @@ struct ModuleWireBytes;
struct V8_EXPORT_PRIVATE WasmModule { struct V8_EXPORT_PRIVATE WasmModule {
static const uint32_t kPageSize = 0x10000; // Page size, 64kb. static const uint32_t kPageSize = 0x10000; // Page size, 64kb.
static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb
static const size_t kV8MaxPages = 16384; // Maximum memory size = 1gb
static const size_t kSpecMaxPages = 65536; // Maximum according to the spec
static const size_t kV8MaxTableSize = 16 * 1024 * 1024;
Zone* owned_zone; Zone* owned_zone;
uint32_t min_mem_pages = 0; // minimum size of the memory in 64k pages uint32_t min_mem_pages = 0; // minimum size of the memory in 64k pages
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment