Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Unified Diff: test/unittests/wasm/module-decoder-unittest.cc

Issue 1765843002: wasm: use strings for section names (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Silence compiler warning Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/wasm/wasm-module-builder.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/unittests/wasm/module-decoder-unittest.cc
diff --git a/test/unittests/wasm/module-decoder-unittest.cc b/test/unittests/wasm/module-decoder-unittest.cc
index 77d05d3b5c0c6d5f790bc24883cd2fed07f020d5..136b9f26acd800cec35335bb57accce9d8edc83a 100644
--- a/test/unittests/wasm/module-decoder-unittest.cc
+++ b/test/unittests/wasm/module-decoder-unittest.cc
@@ -13,10 +13,18 @@ namespace internal {
namespace wasm {
#define EMPTY_FUNCTION(sig_index) 0, SIG_INDEX(sig_index), U16_LE(0)
+#define EMPTY_FUNCTION_SIZE ((size_t)5)
#define EMPTY_BODY 0
+#define EMPTY_BODY_SIZE ((size_t)1)
#define NOP_BODY 2, 0, kExprNop
+#define NOP_BODY_SIZE ((size_t)3)
#define VOID_VOID_SIG 0, kLocalVoid
+#define VOID_VOID_SIG_SIZE ((size_t)2)
#define INT_INT_SIG 1, kLocalI32, kLocalI32
+#define INT_INT_SIG_SIZE ((size_t)3)
+
+#define SECTION(NAME, EXTRA_SIZE) \
+ U32V_1(WASM_SECTION_##NAME##_SIZE + (EXTRA_SIZE)), WASM_SECTION_##NAME
#define EXPECT_VERIFIES(data) \
do { \
@@ -41,6 +49,15 @@ namespace wasm {
} \
} while (false)
+static size_t SizeOfVarInt(size_t value) {
+ size_t size = 0;
+ do {
+ size++;
+ value = value >> 7;
+ } while (value > 0);
+ return size;
+}
+
struct LocalTypePair {
uint8_t code;
LocalType type;
@@ -74,7 +91,7 @@ class WasmModuleVerifyTest : public TestWithZone {
TEST_F(WasmModuleVerifyTest, WrongMagic) {
for (uint32_t x = 1; x; x <<= 1) {
const byte data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion),
- kDeclEnd};
+ SECTION(END, 0)};
ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data));
EXPECT_FALSE(result.ok());
if (result.val) delete result.val;
@@ -84,7 +101,7 @@ TEST_F(WasmModuleVerifyTest, WrongMagic) {
TEST_F(WasmModuleVerifyTest, WrongVersion) {
for (uint32_t x = 1; x; x <<= 1) {
const byte data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x),
- kDeclEnd};
+ SECTION(END, 0)};
ModuleResult result = DecodeModuleNoHeader(data, data + sizeof(data));
EXPECT_FALSE(result.ok());
if (result.val) delete result.val;
@@ -92,17 +109,18 @@ TEST_F(WasmModuleVerifyTest, WrongVersion) {
}
TEST_F(WasmModuleVerifyTest, DecodeEmpty) {
- static const byte data[] = {kDeclEnd};
+ static const byte data[] = {SECTION(END, 0)};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, OneGlobal) {
static const byte data[] = {
- kDeclGlobals,
+ SECTION(GLOBALS, 7), // --
1,
- NAME_LENGTH(1), 'g', // name
- kMemI32, // memory type
- 0, // exported
+ NAME_LENGTH(1),
+ 'g', // name
+ kMemI32, // memory type
+ 0, // exported
};
{
@@ -129,7 +147,8 @@ TEST_F(WasmModuleVerifyTest, OneGlobal) {
TEST_F(WasmModuleVerifyTest, ZeroGlobals) {
static const byte data[] = {
- kDeclGlobals, 0, // declare 0 globals
+ SECTION(GLOBALS, 1), // --
+ 0, // declare 0 globals
};
ModuleResult result = DecodeModule(data, data + arraysize(data));
EXPECT_TRUE(result.ok());
@@ -161,8 +180,13 @@ TEST_F(WasmModuleVerifyTest, NGlobals) {
for (uint32_t i = 0; i < 1000000; i = i * 13 + 1) {
std::vector<byte> buffer;
- buffer.push_back(kDeclGlobals);
- AppendUint32v(buffer, i);
+ size_t size =
+ WASM_SECTION_GLOBALS_SIZE + SizeOfVarInt(i) + i * sizeof(data);
+ const byte globals[] = {U32V_5(size), WASM_SECTION_GLOBALS};
+ for (size_t g = 0; g != sizeof(globals); ++g) {
+ buffer.push_back(globals[g]);
+ }
+ AppendUint32v(buffer, i); // Number of globals.
for (uint32_t j = 0; j < i; j++) {
buffer.insert(buffer.end(), data, data + sizeof(data));
}
@@ -173,12 +197,23 @@ TEST_F(WasmModuleVerifyTest, NGlobals) {
}
}
+TEST_F(WasmModuleVerifyTest, GlobalWithInvalidNameOffset) {
+ static const byte data[] = {
+ SECTION(GLOBALS, 7),
+ 1, // declare one global
+ NO_NAME, // name offset
+ 33, // memory type
+ 0, // exported
+ };
+
+ EXPECT_FAILURE(data);
+}
TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) {
static const byte data[] = {
- kDeclGlobals,
+ SECTION(GLOBALS, 7),
1, // declare one global
- NO_NAME, // name length
+ NO_NAME, // name offset
33, // memory type
0, // exported
};
@@ -189,7 +224,7 @@ TEST_F(WasmModuleVerifyTest, GlobalWithInvalidMemoryType) {
TEST_F(WasmModuleVerifyTest, TwoGlobals) {
static const byte data[] = {
- kDeclGlobals,
+ SECTION(GLOBALS, 13),
2,
NO_NAME, // #0: name length
kMemF32, // memory type
@@ -229,12 +264,14 @@ TEST_F(WasmModuleVerifyTest, TwoGlobals) {
TEST_F(WasmModuleVerifyTest, OneSignature) {
{
- static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG};
+ static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1,
+ VOID_VOID_SIG};
EXPECT_VERIFIES(data);
}
{
- static const byte data[] = {kDeclSignatures, 1, INT_INT_SIG};
+ static const byte data[] = {SECTION(SIGNATURES, 1 + INT_INT_SIG_SIZE), 1,
+ INT_INT_SIG};
EXPECT_VERIFIES(data);
}
}
@@ -242,7 +279,7 @@ TEST_F(WasmModuleVerifyTest, OneSignature) {
TEST_F(WasmModuleVerifyTest, MultipleSignatures) {
static const byte data[] = {
- kDeclSignatures,
+ SECTION(SIGNATURES, 10),
3,
0,
kLocalVoid, // void -> void
@@ -275,7 +312,7 @@ TEST_F(WasmModuleVerifyTest, MultipleSignatures) {
TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) {
static const byte data[] = {
- kDeclFunctions, 1,
+ SECTION(FUNCTIONS, 25), 1,
// func#0 ------------------------------------------------------
SIG_INDEX(0), // signature index
NO_NAME, // name length
@@ -296,15 +333,15 @@ TEST_F(WasmModuleVerifyTest, FunctionWithoutSig) {
TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) {
- const int kCodeStartOffset = 30;
+ const int kCodeStartOffset = 51;
const int kCodeEndOffset = kCodeStartOffset + 1;
static const byte data[] = {
- kDeclSignatures, 1,
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1,
// sig#0 -------------------------------------------------------
VOID_VOID_SIG,
// func#0 ------------------------------------------------------
- kDeclFunctions, 1,
+ SECTION(FUNCTIONS, 19), 1,
kDeclFunctionLocals | kDeclFunctionExport | kDeclFunctionName,
SIG_INDEX(0), // signature index
NAME_LENGTH(2), 'h', 'i', // name
@@ -328,7 +365,7 @@ TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) {
WasmFunction* function = &result.val->functions.back();
- EXPECT_EQ(18, function->name_offset);
+ EXPECT_EQ(39, function->name_offset);
EXPECT_EQ(2, function->name_length);
EXPECT_EQ(kCodeStartOffset, function->code_start_offset);
EXPECT_EQ(kCodeEndOffset, function->code_end_offset);
@@ -344,15 +381,15 @@ TEST_F(WasmModuleVerifyTest, OneEmptyVoidVoidFunction) {
if (result.val) delete result.val;
}
- EXPECT_OFF_END_FAILURE(data, 5, sizeof(data));
+ EXPECT_OFF_END_FAILURE(data, 16, sizeof(data));
}
TEST_F(WasmModuleVerifyTest, OneFunctionImported) {
static const byte data[] = {
- kDeclSignatures, 1,
+ SECTION(SIGNATURES, VOID_VOID_SIG_SIZE), 1,
// sig#0 -------------------------------------------------------
- VOID_VOID_SIG, kDeclFunctions, 1,
+ VOID_VOID_SIG, SECTION(FUNCTIONS, 6), 1,
// func#0 ------------------------------------------------------
kDeclFunctionImport, // no name, no locals, imported
SIG_INDEX(0),
@@ -379,14 +416,14 @@ TEST_F(WasmModuleVerifyTest, OneFunctionImported) {
}
TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) {
- static const byte kCodeStartOffset = 19;
+ static const byte kCodeStartOffset = 40;
static const byte kCodeEndOffset = kCodeStartOffset + 1;
static const byte data[] = {
- kDeclSignatures, 1,
+ SECTION(SIGNATURES, 3), 1,
// sig#0 -------------------------------------------------------
0, 0, // void -> void
- kDeclFunctions, 1,
+ SECTION(FUNCTIONS, 7), 1,
// func#0 ------------------------------------------------------
0, // no name, no locals
0, 0, // signature index
@@ -416,14 +453,14 @@ TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody) {
TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) {
- static const byte kCodeStartOffset = 27;
+ static const byte kCodeStartOffset = 48;
static const byte kCodeEndOffset = kCodeStartOffset + 1;
static const byte data[] = {
- kDeclSignatures, 1,
+ SECTION(SIGNATURES, 3), 1,
// sig#0 -------------------------------------------------------
0, 0, // void -> void
- kDeclFunctions, 1,
+ SECTION(FUNCTIONS, 15), 1,
// func#0 ------------------------------------------------------
kDeclFunctionLocals, 0, 0, // signature index
1, 2, // local int32 count
@@ -456,20 +493,21 @@ TEST_F(WasmModuleVerifyTest, OneFunctionWithNopBody_WithLocals) {
TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) {
- static const byte kCodeStartOffset = 8 + 4 + 5 + 4 + 18;
+ static const byte kCodeStartOffset = 75;
static const byte kCodeEndOffset = kCodeStartOffset + 3;
- static const byte kDataSegmentSourceOffset = kCodeEndOffset + 6;
+ static const byte kDataSegmentSourceOffset = kCodeEndOffset + 20;
static const byte data[] = {
- kDeclMemory, 28, 28, 1,
+ SECTION(MEMORY, 3), 28, 28, 1,
// global#0 --------------------------------------------------
- kDeclGlobals, 1, 0, // name length
- kMemU8, // memory type
- 0, // exported
+ SECTION(GLOBALS, 7), 1,
+ 0, // name length
+ kMemU8, // memory type
+ 0, // exported
// sig#0 -----------------------------------------------------
- kDeclSignatures, 1, 0, 0, // void -> void
+ SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void
// func#0 ----------------------------------------------------
- kDeclFunctions, 1, kDeclFunctionLocals | kDeclFunctionName, 0,
+ SECTION(FUNCTIONS, 20), 1, kDeclFunctionLocals | kDeclFunctionName, 0,
0, // signature index
2, 'h', 'i', // name
1, 2, // local int32 count
@@ -481,11 +519,12 @@ TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) {
kExprNop, // func#0 body
kExprNop, // func#0 body
// segment#0 -------------------------------------------------
- kDeclDataSegments, 1, U32V_3(0x8b3ae), // dest addr
- U32V_1(5), // source size
- 0, 1, 2, 3, 4, // data bytes
+ SECTION(DATA_SEGMENTS, 14), 1,
+ U32V_3(0x8b3ae), // dest addr
+ U32V_1(5), // source size
+ 0, 1, 2, 3, 4, // data bytes
// rest ------------------------------------------------------
- kDeclEnd,
+ SECTION(END, 0),
};
{
@@ -504,7 +543,7 @@ TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) {
WasmFunction* function = &result.val->functions.back();
- EXPECT_EQ(27, function->name_offset);
+ EXPECT_EQ(63, function->name_offset);
EXPECT_EQ(2, function->name_length);
EXPECT_EQ(kCodeStartOffset, function->code_start_offset);
EXPECT_EQ(kCodeEndOffset, function->code_end_offset);
@@ -525,13 +564,19 @@ TEST_F(WasmModuleVerifyTest, OneGlobalOneFunctionWithNopBodyOneDataSegment) {
TEST_F(WasmModuleVerifyTest, OneDataSegment) {
- const byte kDataSegmentSourceOffset = 8 + 10;
+ const byte kDataSegmentSourceOffset = 39;
const byte data[] = {
- kDeclMemory, 28, 28, 1,
- kDeclDataSegments, 1,
+ SECTION(MEMORY, 3),
+ 28,
+ 28,
+ 1,
+ SECTION(DATA_SEGMENTS, 8),
+ 1,
U32V_3(0x9bbaa), // dest addr
U32V_1(3), // source size
- 'a', 'b', 'c' // data bytes
+ 'a',
+ 'b',
+ 'c' // data bytes
};
{
@@ -552,23 +597,39 @@ TEST_F(WasmModuleVerifyTest, OneDataSegment) {
if (result.val) delete result.val;
}
- EXPECT_OFF_END_FAILURE(data, 5, sizeof(data));
+ EXPECT_OFF_END_FAILURE(data, 13, sizeof(data));
}
TEST_F(WasmModuleVerifyTest, TwoDataSegments) {
- const byte kDataSegment0SourceOffset = 8 + 10;
- const byte kDataSegment1SourceOffset = 8 + 10 + 8;
+ const byte kDataSegment0SourceOffset = 39;
+ const byte kDataSegment1SourceOffset = 39 + 8;
const byte data[] = {
- kDeclMemory, 28, 28, 1,
- kDeclDataSegments, 2,
- U32V_3(0x7ffee), // #0: dest addr
- U32V_1(4), // source size
- 1, 2, 3, 4, // data bytes
- U32V_3(0x6ddcc), // #1: dest addr
- U32V_1(10), // source size
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // data bytes
+ SECTION(MEMORY, 3),
+ 28,
+ 28,
+ 1,
+ SECTION(DATA_SEGMENTS, 31),
+ 2, // segment count
+ U32V_3(0x7ffee), // #0: dest addr
+ U32V_1(4), // source size
+ 1,
+ 2,
+ 3,
+ 4, // data bytes
+ U32V_3(0x6ddcc), // #1: dest addr
+ U32V_1(10), // source size
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9,
+ 10 // data bytes
};
{
@@ -594,10 +655,9 @@ TEST_F(WasmModuleVerifyTest, TwoDataSegments) {
if (result.val) delete result.val;
}
- EXPECT_OFF_END_FAILURE(data, 5, sizeof(data));
+ EXPECT_OFF_END_FAILURE(data, 13, sizeof(data));
}
-
TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) {
const int source_size = 3;
@@ -606,17 +666,17 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) {
for (int dest_addr = mem_size - source_size;
dest_addr < mem_size + source_size; dest_addr++) {
- byte data[] = {
- kDeclMemory,
- mem_pages,
- mem_pages,
- 1,
- kDeclDataSegments,
- 1,
- U32V_3(dest_addr),
- U32V_1(source_size),
- 'a', 'b', 'c'
- };
+ byte data[] = {SECTION(MEMORY, 3),
+ mem_pages,
+ mem_pages,
+ 1,
+ SECTION(DATA_SEGMENTS, 14),
+ 1,
+ U32V_3(dest_addr),
+ U32V_1(source_size),
+ 'a',
+ 'b',
+ 'c'};
if (dest_addr <= (mem_size - source_size)) {
EXPECT_VERIFIES(data);
@@ -634,11 +694,11 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) {
TEST_F(WasmModuleVerifyTest, OneIndirectFunction) {
static const byte data[] = {
// sig#0 -------------------------------------------------------
- kDeclSignatures, 1, 0, 0, // void -> void
+ SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void
// func#0 ------------------------------------------------------
- kDeclFunctions, 1, FUNCTION(0, 0),
+ SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 0),
// indirect table ----------------------------------------------
- kDeclFunctionTable, 1, U32V_1(0)};
+ SECTION(FUNCTION_TABLE, 2), 1, U32V_1(0)};
ModuleResult result = DecodeModule(data, data + arraysize(data));
EXPECT_TRUE(result.ok());
@@ -655,15 +715,15 @@ TEST_F(WasmModuleVerifyTest, OneIndirectFunction) {
TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) {
static const byte data[] = {
// sig#0 -------------------------------------------------------
- kDeclSignatures, 2, 0, 0, // void -> void
- 0, kLocalI32, // void -> i32
+ SECTION(SIGNATURES, 5), 2, 0, 0, // void -> void
+ 0, kLocalI32, // void -> i32
// func#0 ------------------------------------------------------
- kDeclFunctions, 4, FUNCTION(0, 1), // --
- FUNCTION(1, 1), // --
- FUNCTION(0, 1), // --
- FUNCTION(1, 1), // --
+ SECTION(FUNCTIONS, 13), 4, FUNCTION(0, 1), // --
+ FUNCTION(1, 1), // --
+ FUNCTION(0, 1), // --
+ FUNCTION(1, 1), // --
// indirect table ----------------------------------------------
- kDeclFunctionTable, 8,
+ SECTION(FUNCTION_TABLE, 9), 8,
U32V_1(0), // --
U32V_1(1), // --
U32V_1(2), // --
@@ -691,9 +751,9 @@ TEST_F(WasmModuleVerifyTest, MultipleIndirectFunctions) {
TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) {
static const byte data[] = {
// sig#0 -------------------------------------------------------
- kDeclSignatures, 1, 0, 0, // void -> void
+ SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void
// indirect table ----------------------------------------------
- kDeclFunctionTable, 1, 0, 0,
+ SECTION(FUNCTION_TABLE, 3), 1, 0, 0,
};
EXPECT_FAILURE(data);
@@ -703,11 +763,11 @@ TEST_F(WasmModuleVerifyTest, IndirectFunctionNoFunctions) {
TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) {
static const byte data[] = {
// sig#0 -------------------------------------------------------
- kDeclSignatures, 1, 0, 0, // void -> void
+ SECTION(SIGNATURES, 3), 1, 0, 0, // void -> void
// functions ---------------------------------------------------
- kDeclFunctions, 1, FUNCTION(0, 1),
+ SECTION(FUNCTIONS, 4), 1, FUNCTION(0, 1),
// indirect table ----------------------------------------------
- kDeclFunctionTable, 1, 1, 0,
+ SECTION(FUNCTION_TABLE, 3), 1, 1, 0,
};
EXPECT_FAILURE(data);
@@ -874,85 +934,84 @@ TEST_F(WasmFunctionVerifyTest, Ok_v_v_empty) {
if (result.val) delete result.val;
}
-TEST_F(WasmModuleVerifyTest, UnknownSectionNoLen) {
+TEST_F(WasmModuleVerifyTest, SectionWithoutNameLength) {
+ const byte data[] = {1};
+ EXPECT_FAILURE(data);
+}
+
+TEST_F(WasmModuleVerifyTest, TheLoneliestOfValidModulesTheTrulyEmptyOne) {
const byte data[] = {
- kMaxModuleSectionCode, // unknown section without length.
+ 1, // Section size.
+ 0, // Empty section name.
+ // No section name, no content, nothing but sadness.
};
- EXPECT_FAILURE(data);
+ EXPECT_VERIFIES(data);
}
-TEST_F(WasmModuleVerifyTest, UnknownSectionEmpty) {
- for (int i = 0; i < 255 - kMaxModuleSectionCode; ++i) {
- const byte data[] = {
- byte(kMaxModuleSectionCode + i), 0, // empty unknown section
- };
- ModuleResult result = DecodeModule(data, data + arraysize(data));
- EXPECT_TRUE(result.ok());
- if (result.val) delete result.val;
- }
+TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionEmpty) {
+ const byte data[] = {
+ 5, // Section size.
+ 4, 'l', 'u', 'l', 'z', // unknown section.
+ };
+ EXPECT_VERIFIES(data);
}
-TEST_F(WasmModuleVerifyTest, UnknownSectionOne) {
- static const byte data[] = {
- kMaxModuleSectionCode,
- 1, // LEB128 1
- 0, // one byte section
+TEST_F(WasmModuleVerifyTest, OnlyUnknownSectionNonEmpty) {
+ const byte data[] = {
+ 10, // Section size.
+ 4, 'l', 'u', 'l', 'z', // unknown section.
+ // Section content:
+ 0xff, 0xff, 0xff, 0xff, 0xff,
};
- ModuleResult result = DecodeModule(data, data + arraysize(data));
- EXPECT_TRUE(result.ok());
- if (result.val) delete result.val;
+ EXPECT_VERIFIES(data);
}
-TEST_F(WasmModuleVerifyTest, UnknownSectionTen) {
- static const byte data[] = {
- kMaxModuleSectionCode,
- 10, // LEB128 10
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10, // 10 byte section
+TEST_F(WasmModuleVerifyTest, SignatureFollowedByEmptyUnknownSection) {
+ const byte data[] = {
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG,
+ // -----------------------------------------------------------
+ 5, // Section size.
+ 4, 'l', 'u', 'l', 'z', // unknown section.
};
- ModuleResult result = DecodeModule(data, data + arraysize(data));
- EXPECT_TRUE(result.ok());
- if (result.val) delete result.val;
+ EXPECT_VERIFIES(data);
+}
+
+TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSection) {
+ const byte data[] = {
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG,
+ // -----------------------------------------------------------
+ 10, // Section size.
+ 4, 'l', 'u', 'l', 'z', // unknown section.
+ 0xff, 0xff, 0xff, 0xff, 0xff,
+ };
+ EXPECT_VERIFIES(data);
+}
+
+TEST_F(WasmModuleVerifyTest, SignatureFollowedByUnknownSectionWithLongLEB) {
+ const byte data[] = {
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE), 1, VOID_VOID_SIG,
+ // -----------------------------------------------------------
+ 0x85, 0x80, 0x80, 0x80, 0x00, // Section size: 1 but in a 5-byte LEB.
+ 4, 'l', 'u', 'l', 'z', // unknown section.
+ };
+ EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, UnknownSectionOverflow) {
static const byte data[] = {
- kMaxModuleSectionCode,
- 11, // LEB128 11
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10, // 10 byte section
+ 13, // Section size.
+ 1, // Section name length.
+ '\0', // Section name.
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // 10 byte section
};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) {
static const byte data[] = {
- kMaxModuleSectionCode,
- 0xff,
- 0xff,
- 0xff,
- 0xff,
- 0x0f, // LEB128 0xffffffff
- 1,
- 2,
- 3,
- 4, // 4 byte section
+ 0xff, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xffffffff
+ 1, '\0', // Section name and name length.
+ 1, 2, 3, 4, // 4 byte section
};
EXPECT_FAILURE(data);
}
@@ -960,26 +1019,20 @@ TEST_F(WasmModuleVerifyTest, UnknownSectionUnderflow) {
TEST_F(WasmModuleVerifyTest, UnknownSectionLoop) {
// Would infinite loop decoding if wrapping and allowed.
static const byte data[] = {
- kMaxModuleSectionCode,
- 0xfa,
- 0xff,
- 0xff,
- 0xff,
- 0x0f, // LEB128 0xfffffffa
- 1,
- 2,
- 3,
- 4, // 4 byte section
+ 0xfa, 0xff, 0xff, 0xff, 0x0f, // Section size LEB128 0xfffffffa
+ 1, '\0', // Section name and name length.
+ 1, 2, 3, 4, // 4 byte section
};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) {
static const byte data[] = {
- kMaxModuleSectionCode,
- 1, // LEB128 1
- 0, // one byte section
- kDeclGlobals,
+ 3, // Section size.
+ 1,
+ '\0', // Section name: LEB128 1, string '\0'
+ 0, // one byte section
+ SECTION(GLOBALS, 7),
1,
0, // name length
kMemI32, // memory type
@@ -1003,131 +1056,167 @@ TEST_F(WasmModuleVerifyTest, UnknownSectionSkipped) {
}
TEST_F(WasmModuleVerifyTest, ImportTable_empty) {
- static const byte data[] = {kDeclSignatures, 0, kDeclImportTable, 0};
+ static const byte data[] = {SECTION(SIGNATURES, 1), 0,
+ SECTION(IMPORT_TABLE, 1), 0};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, ImportTable_nosigs) {
- static const byte data[] = {kDeclImportTable, 0};
+ static const byte data[] = {SECTION(IMPORT_TABLE, 1), 0};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ImportTable_invalid_sig) {
static const byte data[] = {
- kDeclSignatures, 0, kDeclImportTable, 1,
- IMPORT_SIG_INDEX(0), // sig index
- NAME_LENGTH(1), 'm', // module name
- NAME_LENGTH(1), 'f', // function name
+ SECTION(SIGNATURES, 1), 0, SECTION(IMPORT_TABLE, 6), 1,
+ IMPORT_SIG_INDEX(0), // sig index
+ NAME_LENGTH(1), 'm', // module name
+ NAME_LENGTH(1), 'f', // function name
};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ImportTable_one_sig) {
static const byte data[] = {
- kDeclSignatures,
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
1,
VOID_VOID_SIG,
- kDeclImportTable,
+ SECTION(IMPORT_TABLE, 6),
1, // --
IMPORT_SIG_INDEX(0), // sig index
- NAME_LENGTH(1), 'm', // module name
- NAME_LENGTH(1), 'f', // function name
+ NAME_LENGTH(1),
+ 'm', // module name
+ NAME_LENGTH(1),
+ 'f', // function name
};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, ImportTable_invalid_module) {
static const byte data[] = {
- kDeclSignatures,
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
1,
VOID_VOID_SIG,
- kDeclImportTable,
+ SECTION(IMPORT_TABLE, 6),
1, // --
IMPORT_SIG_INDEX(0), // sig index
NO_NAME, // module name
- NAME_LENGTH(1), 'f' // function name
+ NAME_LENGTH(1),
+ 'f' // function name
};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ImportTable_off_end) {
static const byte data[] = {
- kDeclSignatures, 1, VOID_VOID_SIG, kDeclImportTable, 1,
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1,
+ VOID_VOID_SIG,
+ SECTION(IMPORT_TABLE, 6),
+ 1,
IMPORT_SIG_INDEX(0), // sig index
- NAME_LENGTH(1), 'm', // module name
- NAME_LENGTH(1), 'f', // function name
+ NAME_LENGTH(1),
+ 'm', // module name
+ NAME_LENGTH(1),
+ 'f', // function name
};
- EXPECT_OFF_END_FAILURE(data, 5, sizeof(data));
+ EXPECT_OFF_END_FAILURE(data, 16, sizeof(data));
}
TEST_F(WasmModuleVerifyTest, ExportTable_empty1) {
- static const byte data[] = {kDeclSignatures, 1, VOID_VOID_SIG,
- kDeclFunctions, 1, EMPTY_FUNCTION(0),
- kDeclExportTable, 0};
+ static const byte data[] = {SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1,
+ VOID_VOID_SIG,
+ SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE),
+ 1,
+ EMPTY_FUNCTION(0),
+ SECTION(EXPORT_TABLE, 1),
+ 0};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, ExportTable_empty2) {
- static const byte data[] = {kDeclSignatures, 0, kDeclFunctions, 0,
- kDeclExportTable, 0};
+ static const byte data[] = {SECTION(SIGNATURES, 1), 0,
+ SECTION(FUNCTIONS, 1), 0,
+ SECTION(EXPORT_TABLE, 1), 0};
// TODO(titzer): current behavior treats empty functions section as missing.
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions1) {
- static const byte data[] = {kDeclSignatures, 0, kDeclExportTable, 0};
+ static const byte data[] = {SECTION(SIGNATURES, 1), 0,
+ SECTION(EXPORT_TABLE, 1), 0};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ExportTable_NoFunctions2) {
- static const byte data[] = {kDeclExportTable, 0};
+ static const byte data[] = {SECTION(EXPORT_TABLE, 1), 0};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, ExportTableOne) {
static const byte data[] = {
- kDeclSignatures, 1, // sigs
- VOID_VOID_SIG, // --
- kDeclFunctions, 1, // functions
- EMPTY_FUNCTION(0), // --
- kDeclExportTable, 1, // exports
- FUNC_INDEX(0), // --
- NO_NAME // --
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1, // sigs
+ VOID_VOID_SIG, // --
+ SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE),
+ 1, // functions
+ EMPTY_FUNCTION(0), // --
+ SECTION(EXPORT_TABLE, 7),
+ 1, // exports
+ FUNC_INDEX(0), // --
+ NO_NAME // --
};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, ExportTableTwo) {
static const byte data[] = {
- kDeclSignatures, 1, // sigs
- VOID_VOID_SIG, // --
- kDeclFunctions, 1, // functions
- EMPTY_FUNCTION(0), // --
- kDeclExportTable, 2, // exports
- FUNC_INDEX(0), // --
- NAME_LENGTH(4), 'n', 'a', 'm', 'e', // --
- FUNC_INDEX(0), // --
- NAME_LENGTH(3), 'n', 'o', 'm' // --
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1, // sigs
+ VOID_VOID_SIG, // --
+ SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE),
+ 1, // functions
+ EMPTY_FUNCTION(0), // --
+ SECTION(EXPORT_TABLE, 12),
+ 2, // exports
+ FUNC_INDEX(0), // --
+ NAME_LENGTH(4),
+ 'n',
+ 'a',
+ 'm',
+ 'e', // --
+ FUNC_INDEX(0), // --
+ NAME_LENGTH(3),
+ 'n',
+ 'o',
+ 'm' // --
};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, ExportTableThree) {
static const byte data[] = {
- kDeclSignatures, 1, // sigs
- VOID_VOID_SIG, // --
- kDeclFunctions, 3, // functions
- EMPTY_FUNCTION(0), // --
- EMPTY_FUNCTION(0), // --
- EMPTY_FUNCTION(0), // --
- kDeclExportTable, 3, // exports
- FUNC_INDEX(0), // --
- NAME_LENGTH(1), 'a', // --
- FUNC_INDEX(1), // --
- NAME_LENGTH(1), 'b', // --
- FUNC_INDEX(2), // --
- NAME_LENGTH(1), 'c' // --
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1, // sigs
+ VOID_VOID_SIG, // --
+ SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE),
+ 3, // functions
+ EMPTY_FUNCTION(0), // --
+ EMPTY_FUNCTION(0), // --
+ EMPTY_FUNCTION(0), // --
+ SECTION(EXPORT_TABLE, 10),
+ 3, // exports
+ FUNC_INDEX(0), // --
+ NAME_LENGTH(1),
+ 'a', // --
+ FUNC_INDEX(1), // --
+ NAME_LENGTH(1),
+ 'b', // --
+ FUNC_INDEX(2), // --
+ NAME_LENGTH(1),
+ 'c' // --
};
EXPECT_VERIFIES(data);
}
@@ -1135,15 +1224,20 @@ TEST_F(WasmModuleVerifyTest, ExportTableThree) {
TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) {
for (int i = 0; i < 6; i++) {
const byte data[] = {
- kDeclSignatures, 1, // sigs
- VOID_VOID_SIG, // --
- kDeclFunctions, 3, // functions
- EMPTY_FUNCTION(0), // --
- EMPTY_FUNCTION(0), // --
- EMPTY_FUNCTION(0), // --
- kDeclExportTable, 1, // exports
- FUNC_INDEX(i), // --
- NAME_LENGTH(2), 'e', 'x', // --
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1, // sigs
+ VOID_VOID_SIG, // --
+ SECTION(FUNCTIONS, 1 + 3 * EMPTY_FUNCTION_SIZE),
+ 3, // functions
+ EMPTY_FUNCTION(0), // --
+ EMPTY_FUNCTION(0), // --
+ EMPTY_FUNCTION(0), // --
+ SECTION(EXPORT_TABLE, 5),
+ 1, // exports
+ FUNC_INDEX(i), // --
+ NAME_LENGTH(2),
+ 'e',
+ 'x', // --
};
if (i < 3) {
@@ -1156,16 +1250,19 @@ TEST_F(WasmModuleVerifyTest, ExportTableThreeOne) {
TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) {
static const byte data[] = {
- kDeclSignatures, 1, // sigs
- VOID_VOID_SIG, // --
- kDeclFunctions, 1, // functions
- EMPTY_FUNCTION(0), // --
- kDeclExportTable, 1, // exports
- FUNC_INDEX(0), // --
- NO_NAME // --
+ SECTION(SIGNATURES, 1 + VOID_VOID_SIG_SIZE),
+ 1, // sigs
+ VOID_VOID_SIG, // --
+ SECTION(FUNCTIONS, 1 + EMPTY_FUNCTION_SIZE),
+ 1, // functions
+ EMPTY_FUNCTION(0), // --
+ SECTION(EXPORT_TABLE, 1 + 6),
+ 1, // exports
+ FUNC_INDEX(0), // --
+ NO_NAME // --
};
- for (int length = 13; length < sizeof(data); length++) {
+ for (int length = 33; length < sizeof(data); length++) {
ModuleResult result = DecodeModule(data, data + length);
EXPECT_FALSE(result.ok());
if (result.val) delete result.val;
@@ -1173,23 +1270,21 @@ TEST_F(WasmModuleVerifyTest, ExportTableOne_off_end) {
}
#define SIGNATURES_SECTION(count, ...) \
- kDeclSignatures, U32V_1(count), __VA_ARGS__
+ SECTION(SIGNATURES, 1 + 3 * (count)), U32V_1(count), __VA_ARGS__
#define FUNCTION_SIGNATURES_SECTION(count, ...) \
- kDeclFunctionSignatures, U32V_1(count), __VA_ARGS__
-#define FUNCTION_BODIES_SECTION(count, ...) \
- kDeclFunctionBodies, U32V_1(count), __VA_ARGS__
-#define NAMES_SECTION(count, ...) kDeclNames, U32V_1(count), __VA_ARGS__
+ SECTION(FUNCTION_SIGNATURES, 1 + (count)), U32V_1(count), __VA_ARGS__
#define FOO_STRING 3, 'f', 'o', 'o'
#define NO_LOCAL_NAMES 0
-#define EMPTY_SIGNATURES_SECTION kDeclSignatures, 0
-#define EMPTY_FUNCTION_SIGNATURES_SECTION kDeclFunctionSignatures, 0
-#define EMPTY_FUNCTION_BODIES_SECTION kDeclFunctionBodies, 0
-#define EMPTY_NAMES_SECTION kDeclNames, 0
+#define EMPTY_SIGNATURES_SECTION SECTION(SIGNATURES, 1), 0
+#define EMPTY_FUNCTION_SIGNATURES_SECTION SECTION(FUNCTION_SIGNATURES, 1), 0
+#define EMPTY_FUNCTION_BODIES_SECTION SECTION(FUNCTION_BODIES, 1), 0
+#define EMPTY_NAMES_SECTION SECTION(NAMES, 1), 0
TEST_F(WasmModuleVerifyTest, FunctionSignatures_empty) {
- static const byte data[] = {kDeclSignatures, 0, kDeclFunctionSignatures, 0};
+ static const byte data[] = {SECTION(SIGNATURES, 1), 0,
+ SECTION(FUNCTION_SIGNATURES, 1), 0};
EXPECT_VERIFIES(data);
}
@@ -1207,30 +1302,34 @@ TEST_F(WasmModuleVerifyTest, FunctionBodies_empty) {
}
TEST_F(WasmModuleVerifyTest, FunctionBodies_one_empty) {
- static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG),
- FUNCTION_SIGNATURES_SECTION(1, 0),
- FUNCTION_BODIES_SECTION(1, EMPTY_BODY)};
+ static const byte data[] = {
+ SIGNATURES_SECTION(1, VOID_VOID_SIG), FUNCTION_SIGNATURES_SECTION(1, 0),
+ SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1, EMPTY_BODY};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, FunctionBodies_one_nop) {
- static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG),
- FUNCTION_SIGNATURES_SECTION(1, 0),
- FUNCTION_BODIES_SECTION(1, NOP_BODY)};
+ static const byte data[] = {
+ SIGNATURES_SECTION(1, VOID_VOID_SIG), FUNCTION_SIGNATURES_SECTION(1, 0),
+ SECTION(FUNCTION_BODIES, 1 + NOP_BODY_SIZE), 1, NOP_BODY};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch1) {
static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG),
FUNCTION_SIGNATURES_SECTION(2, 0, 0),
- FUNCTION_BODIES_SECTION(1, EMPTY_BODY)};
+ SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE), 1,
+ EMPTY_BODY};
EXPECT_FAILURE(data);
}
TEST_F(WasmModuleVerifyTest, FunctionBodies_count_mismatch2) {
static const byte data[] = {SIGNATURES_SECTION(1, VOID_VOID_SIG),
FUNCTION_SIGNATURES_SECTION(1, 0),
- FUNCTION_BODIES_SECTION(2, NOP_BODY, NOP_BODY)};
+ SECTION(FUNCTION_BODIES, 1 + 2 * NOP_BODY_SIZE),
+ 2,
+ NOP_BODY,
+ NOP_BODY};
EXPECT_FAILURE(data);
}
@@ -1243,21 +1342,33 @@ TEST_F(WasmModuleVerifyTest, Names_empty) {
TEST_F(WasmModuleVerifyTest, Names_one_empty) {
static const byte data[] = {
- SIGNATURES_SECTION(1, VOID_VOID_SIG), // --
- FUNCTION_SIGNATURES_SECTION(1, 0), // --
- FUNCTION_BODIES_SECTION(1, EMPTY_BODY), // --
- NAMES_SECTION(1, FOO_STRING, NO_LOCAL_NAMES) // --
+ SIGNATURES_SECTION(1, VOID_VOID_SIG), // --
+ FUNCTION_SIGNATURES_SECTION(1, 0), // --
+ SECTION(FUNCTION_BODIES, 1 + EMPTY_BODY_SIZE),
+ 1,
+ EMPTY_BODY, // --
+ SECTION(NAMES, 1 + 5),
+ 1,
+ FOO_STRING,
+ NO_LOCAL_NAMES // --
};
EXPECT_VERIFIES(data);
}
TEST_F(WasmModuleVerifyTest, Names_two_empty) {
static const byte data[] = {
- SIGNATURES_SECTION(1, VOID_VOID_SIG), // --
- FUNCTION_SIGNATURES_SECTION(2, 0, 0), // --
- FUNCTION_BODIES_SECTION(2, EMPTY_BODY, EMPTY_BODY), // --
- NAMES_SECTION(2, FOO_STRING, NO_LOCAL_NAMES, // --
- FOO_STRING, NO_LOCAL_NAMES) // --
+ SIGNATURES_SECTION(1, VOID_VOID_SIG), // --
+ FUNCTION_SIGNATURES_SECTION(2, 0, 0), // --
+ SECTION(FUNCTION_BODIES, 1 + 2 * EMPTY_BODY_SIZE), // --
+ 2,
+ EMPTY_BODY,
+ EMPTY_BODY, // --
+ SECTION(NAMES, 1 + 10),
+ 2, // --
+ FOO_STRING,
+ NO_LOCAL_NAMES, // --
+ FOO_STRING,
+ NO_LOCAL_NAMES, // --
};
EXPECT_VERIFIES(data);
}
« no previous file with comments | « test/mjsunit/wasm/wasm-module-builder.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698