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 f254358fc90dfbb8923d199289eca95ff0f57cdd..71feaff95a39319e580dbead26822b8958f72bd8 100644 |
--- a/test/unittests/wasm/module-decoder-unittest.cc |
+++ b/test/unittests/wasm/module-decoder-unittest.cc |
@@ -417,6 +417,100 @@ TEST_F(WasmModuleVerifyTest, MultipleSignatures) { |
EXPECT_OFF_END_FAILURE(data, 1, sizeof(data)); |
} |
+TEST_F(WasmModuleVerifyTest, DataSegmentWithImmutableImportedGlobal) { |
+ // Import 2 globals so that we can initialize data with a global index != 0. |
+ const byte data[] = { |
+ SECTION(Import, 15), // section header |
+ 2, // number of imports |
+ NAME_LENGTH(1), // -- |
+ 'm', // module name |
+ NAME_LENGTH(1), // -- |
+ 'f', // global name |
+ kExternalGlobal, // import kind |
+ kLocalI32, // type |
+ 0, // mutability |
+ NAME_LENGTH(1), // -- |
+ 'n', // module name |
+ NAME_LENGTH(1), // -- |
+ 'g', // global name |
+ kExternalGlobal, // import kind |
+ kLocalI32, // type |
+ 0, // mutability |
+ SECTION(Memory, 4), |
+ ENTRY_COUNT(1), |
+ kResizableMaximumFlag, |
+ 28, |
+ 28, |
+ SECTION(Data, 9), |
+ ENTRY_COUNT(1), |
+ LINEAR_MEMORY_INDEX_0, |
+ WASM_INIT_EXPR_GLOBAL(1), // dest addr |
+ U32V_1(3), // source size |
+ 'a', |
+ 'b', |
+ 'c' // data bytes |
+ }; |
+ ModuleResult result = DecodeModule(data, data + sizeof(data)); |
+ EXPECT_OK(result); |
+ WasmInitExpr expr = result.val->data_segments.back().dest_addr; |
+ EXPECT_EQ(WasmInitExpr::kGlobalIndex, expr.kind); |
+ EXPECT_EQ(1, expr.val.global_index); |
+ if (result.val) delete result.val; |
+} |
+ |
+TEST_F(WasmModuleVerifyTest, DataSegmentWithMutableImportedGlobal) { |
+ // Only an immutable imported global can be used as an init_expr. |
+ const byte data[] = { |
+ SECTION(Import, 8), // section header |
+ 1, // number of imports |
+ NAME_LENGTH(1), // -- |
+ 'm', // module name |
+ NAME_LENGTH(1), // -- |
+ 'f', // global name |
+ kExternalGlobal, // import kind |
+ kLocalI32, // type |
+ 1, // mutability |
+ SECTION(Memory, 4), |
+ ENTRY_COUNT(1), |
+ kResizableMaximumFlag, |
+ 28, |
+ 28, |
+ SECTION(Data, 9), |
+ ENTRY_COUNT(1), |
+ LINEAR_MEMORY_INDEX_0, |
+ WASM_INIT_EXPR_GLOBAL(0), // dest addr |
+ U32V_1(3), // source size |
+ 'a', |
+ 'b', |
+ 'c' // data bytes |
+ }; |
+ EXPECT_FAILURE(data); |
+} |
+TEST_F(WasmModuleVerifyTest, DataSegmentWithImmutableGlobal) { |
+ // Only an immutable imported global can be used as an init_expr. |
+ const byte data[] = { |
+ SECTION(Memory, 4), |
+ ENTRY_COUNT(1), |
+ kResizableMaximumFlag, |
+ 28, |
+ 28, |
+ SECTION(Global, 8), // -- |
+ 1, |
+ kLocalI32, // local type |
+ 0, // immutable |
+ WASM_INIT_EXPR_I32V_3(0x9bbaa), // init |
+ SECTION(Data, 9), |
+ ENTRY_COUNT(1), |
+ LINEAR_MEMORY_INDEX_0, |
+ WASM_INIT_EXPR_GLOBAL(0), // dest addr |
+ U32V_1(3), // source size |
+ 'a', |
+ 'b', |
+ 'c' // data bytes |
+ }; |
+ EXPECT_FAILURE(data); |
+} |
+ |
TEST_F(WasmModuleVerifyTest, OneDataSegment) { |
const byte kDataSegmentSourceOffset = 24; |
const byte data[] = { |
@@ -1378,13 +1472,6 @@ TEST_F(WasmModuleVerifyTest, InitExpr_illegal) { |
EXPECT_INIT_EXPR_FAIL(WASM_IF_ELSE(WASM_ZERO, WASM_ZERO, WASM_ZERO)); |
} |
-TEST_F(WasmModuleVerifyTest, InitExpr_global) { |
- static const byte data[] = {WASM_INIT_EXPR_GLOBAL(37)}; |
- WasmInitExpr expr = DecodeWasmInitExprForTesting(data, data + sizeof(data)); |
- EXPECT_EQ(WasmInitExpr::kGlobalIndex, expr.kind); |
- EXPECT_EQ(37, expr.val.global_index); |
-} |
- |
TEST_F(WasmModuleVerifyTest, Multiple_Named_Sections) { |
static const byte data[] = { |
SECTION(Unknown, 4), 1, 'X', 17, 18, // -- |