[wasm] Emit error for invalid memory limits flag

Additional changes:
- Add reproducing unit test.
- Add parsed memory limit flag to error message.
- Improve naming in memory API in wasm-module-builder.js.

Change-Id: Id9ec5750cdc03560874e6c0219741127182e0c9e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2485227
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70684}
This commit is contained in:
Manos Koukoutos 2020-10-21 15:07:03 +00:00 committed by Commit Bot
parent cd988502a4
commit cf1bb76181
3 changed files with 28 additions and 15 deletions

View File

@ -1544,8 +1544,9 @@ class ModuleDecoderImpl : public Decoder {
case kSharedWithMaximum:
if (!enabled_features_.has_threads()) {
errorf(pc() - 1,
"invalid memory limits flags (enable via "
"--experimental-wasm-threads)");
"invalid memory limits flags 0x%x (enable via "
"--experimental-wasm-threads)",
flags);
}
*has_shared_memory = true;
// V8 does not support shared memory without a maximum.
@ -1559,11 +1560,15 @@ class ModuleDecoderImpl : public Decoder {
case kMemory64WithMaximum:
if (!enabled_features_.has_memory64()) {
errorf(pc() - 1,
"invalid memory limits flags (enable via "
"--experimental-wasm-memory64)");
"invalid memory limits flags 0x%x (enable via "
"--experimental-wasm-memory64)",
flags);
}
*is_memory64 = true;
break;
default:
errorf(pc() - 1, "invalid memory limits flags 0x%x", flags);
break;
}
return flags;
}

View File

@ -75,8 +75,10 @@ let kLocalNamesCode = 2;
let kWasmFunctionTypeForm = 0x60;
let kWasmAnyFunctionTypeForm = 0x70;
let kHasMaximumFlag = 1;
let kSharedHasMaximumFlag = 3;
let kLimitsNoMaximum = 0
let kLimitsHasMaximum = 1;
let kLimitsSharedNoMaximum = 2;
let kLimitsSharedHasMaximum = 3;
// Segment flags
let kActiveNoIndex = 0;
@ -974,8 +976,8 @@ class WasmModuleBuilder {
return this;
}
addMemory(min, max, exp, shared) {
this.memory = {min: min, max: max, exp: exp, shared: shared};
addMemory(min, max, exported, shared) {
this.memory = {min: min, max: max, exported: exported, shared: shared};
return this;
}
@ -1292,12 +1294,9 @@ class WasmModuleBuilder {
section.emit_u8(1); // one memory entry
const has_max = wasm.memory.max !== undefined;
const is_shared = wasm.memory.shared !== undefined;
// Emit flags (bit 0: reszeable max, bit 1: shared memory)
if (is_shared) {
section.emit_u8(has_max ? kSharedHasMaximumFlag : 2);
} else {
section.emit_u8(has_max ? kHasMaximumFlag : 0);
}
section.emit_u8(is_shared
? (has_max ? kLimitsSharedHasMaximum : kLimitsSharedNoMaximum)
: (has_max ? kLimitsHasMaximum : kLimitsNoMaximum));
section.emit_u32v(wasm.memory.min);
if (has_max) section.emit_u32v(wasm.memory.max);
});
@ -1382,7 +1381,7 @@ class WasmModuleBuilder {
}
// Add export table.
var mem_export = (wasm.memory !== undefined && wasm.memory.exp);
var mem_export = (wasm.memory !== undefined && wasm.memory.exported);
var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
if (exports_count > 0) {
if (debug) print("emitting exports @ " + binary.length);

View File

@ -1455,6 +1455,15 @@ TEST_F(WasmModuleVerifyTest, MaxMaximumMemorySize) {
}
}
TEST_F(WasmModuleVerifyTest, InvalidMemoryLimits) {
{
const byte kInvalidLimits = 0x15;
const byte data[] = {
SECTION(Memory, ENTRY_COUNT(1), kInvalidLimits, 0, 10)};
EXPECT_FAILURE_WITH_MSG(data, "invalid memory limits flags 0x15");
}
}
TEST_F(WasmModuleVerifyTest, DataSegment_wrong_init_type) {
const byte data[] = {
SECTION(Memory, ENTRY_COUNT(1), kWithMaximum, 28, 28),