Commit f2b90bd4 authored by Deepti Gandluri's avatar Deepti Gandluri Committed by Commit Bot

[wasm] Catch invalid flags correctly

Cleanup decoding of flags so that invalid flags for sections other than
memory are caught correctly.

Bug: chromium:853453
Change-Id: Ia347d5f7672eee93ca3f6a743f06fba629f55cb5
Reviewed-on: https://chromium-review.googlesource.com/1104976
Commit-Queue: Deepti Gandluri <gdeepti@chromium.org>
Reviewed-by: 's avatarBen Smith <binji@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53972}
parent 9044da68
...@@ -479,20 +479,21 @@ class ModuleDecoderImpl : public Decoder { ...@@ -479,20 +479,21 @@ class ModuleDecoderImpl : public Decoder {
WasmIndirectFunctionTable* table = &module_->function_tables.back(); WasmIndirectFunctionTable* table = &module_->function_tables.back();
table->imported = true; table->imported = true;
expect_u8("element type", kWasmAnyFunctionTypeCode); expect_u8("element type", kWasmAnyFunctionTypeCode);
uint8_t flags = validate_table_flags("element count");
consume_resizable_limits( consume_resizable_limits(
"element count", "elements", FLAG_wasm_max_table_size, "element count", "elements", FLAG_wasm_max_table_size,
&table->initial_size, &table->has_maximum_size, &table->initial_size, &table->has_maximum_size,
FLAG_wasm_max_table_size, &table->maximum_size); FLAG_wasm_max_table_size, &table->maximum_size, flags);
break; break;
} }
case kExternalMemory: { case kExternalMemory: {
// ===== Imported memory ========================================= // ===== Imported memory =========================================
if (!AddMemory(module_.get())) break; if (!AddMemory(module_.get())) break;
uint8_t flags = validate_memory_flags(&module_->has_shared_memory);
consume_resizable_limits( consume_resizable_limits(
"memory", "pages", FLAG_wasm_max_mem_pages, "memory", "pages", FLAG_wasm_max_mem_pages,
&module_->initial_pages, &module_->has_maximum_pages, &module_->initial_pages, &module_->has_maximum_pages,
kSpecMaxWasmMemoryPages, &module_->maximum_pages, kSpecMaxWasmMemoryPages, &module_->maximum_pages, flags);
&module_->has_shared_memory);
break; break;
} }
case kExternalGlobal: { case kExternalGlobal: {
...@@ -553,10 +554,11 @@ class ModuleDecoderImpl : public Decoder { ...@@ -553,10 +554,11 @@ class ModuleDecoderImpl : public Decoder {
module_->function_tables.emplace_back(); module_->function_tables.emplace_back();
WasmIndirectFunctionTable* table = &module_->function_tables.back(); WasmIndirectFunctionTable* table = &module_->function_tables.back();
expect_u8("table type", kWasmAnyFunctionTypeCode); expect_u8("table type", kWasmAnyFunctionTypeCode);
consume_resizable_limits("table elements", "elements", uint8_t flags = validate_table_flags("table elements");
FLAG_wasm_max_table_size, &table->initial_size, consume_resizable_limits(
&table->has_maximum_size, "table elements", "elements", FLAG_wasm_max_table_size,
FLAG_wasm_max_table_size, &table->maximum_size); &table->initial_size, &table->has_maximum_size,
FLAG_wasm_max_table_size, &table->maximum_size, flags);
} }
} }
...@@ -565,10 +567,11 @@ class ModuleDecoderImpl : public Decoder { ...@@ -565,10 +567,11 @@ class ModuleDecoderImpl : public Decoder {
for (uint32_t i = 0; ok() && i < memory_count; i++) { for (uint32_t i = 0; ok() && i < memory_count; i++) {
if (!AddMemory(module_.get())) break; if (!AddMemory(module_.get())) break;
uint8_t flags = validate_memory_flags(&module_->has_shared_memory);
consume_resizable_limits( consume_resizable_limits(
"memory", "pages", FLAG_wasm_max_mem_pages, &module_->initial_pages, "memory", "pages", FLAG_wasm_max_mem_pages, &module_->initial_pages,
&module_->has_maximum_pages, kSpecMaxWasmMemoryPages, &module_->has_maximum_pages, kSpecMaxWasmMemoryPages,
&module_->maximum_pages, &module_->has_shared_memory); &module_->maximum_pages, flags);
} }
} }
...@@ -1095,33 +1098,43 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1095,33 +1098,43 @@ class ModuleDecoderImpl : public Decoder {
return index; return index;
} }
void consume_resizable_limits(const char* name, const char* units, uint8_t validate_table_flags(const char* name) {
uint32_t max_initial, uint32_t* initial,
bool* has_max, uint32_t max_maximum,
uint32_t* maximum,
bool* has_shared_memory = nullptr) {
uint8_t flags = consume_u8("resizable limits flags"); uint8_t flags = consume_u8("resizable limits flags");
const byte* pos = pc(); const byte* pos = pc();
if (flags & 0xFE) {
errorf(pos - 1, "invalid %s limits flags", name);
}
return flags;
}
uint8_t validate_memory_flags(bool* has_shared_memory) {
uint8_t flags = consume_u8("resizable limits flags");
const byte* pos = pc();
*has_shared_memory = false;
if (FLAG_experimental_wasm_threads) { if (FLAG_experimental_wasm_threads) {
bool is_memory = (strcmp(name, "memory") == 0); if (flags & 0xFC) {
if (flags & 0xFC || (!is_memory && (flags & 0xFE))) { errorf(pos - 1, "invalid memory limits flags");
errorf(pos - 1, "invalid %s limits flags", name); } else if (flags == 3) {
}
if (flags == 3) {
DCHECK_NOT_NULL(has_shared_memory); DCHECK_NOT_NULL(has_shared_memory);
*has_shared_memory = true; *has_shared_memory = true;
} else if (flags == 2) { } else if (flags == 2) {
errorf(pos - 1, errorf(pos - 1,
"%s limits flags should have maximum defined if shared is true", "memory limits flags should have maximum defined if shared is "
name); "true");
} }
} else { } else {
if (flags & 0xFE) { if (flags & 0xFE) {
errorf(pos - 1, "invalid %s limits flags", name); errorf(pos - 1, "invalid memory limits flags");
} }
} }
return flags;
}
void consume_resizable_limits(const char* name, const char* units,
uint32_t max_initial, uint32_t* initial,
bool* has_max, uint32_t max_maximum,
uint32_t* maximum, uint8_t flags) {
const byte* pos = pc();
*initial = consume_u32v("initial size"); *initial = consume_u32v("initial size");
*has_max = false; *has_max = false;
if (*initial > max_initial) { if (*initial > max_initial) {
......
// Copyright 2018 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.
// Flags: --experimental-wasm-threads
assertThrows(() => new WebAssembly.Module(
new Uint8Array([
0x00, 0x61, 0x73, 0x6d, // wasm magic
0x01, 0x00, 0x00, 0x00, // wasm version
0x04, // section code
0x04, // section length
/* Section: Table */
0x01, // table count
0x70, // table type
0x03, // resizable limits flags
0x00])),
WebAssembly.CompileError);
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