From c3deae3f5e541eb109357fc9f8f300b4eb291dc9 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Mon, 29 Jun 2020 17:47:42 +0200 Subject: [PATCH 1/2] Add module helper for finding global type --- lib/fizzy/module.hpp | 16 ++++++++-------- lib/fizzy/parser.cpp | 2 +- lib/fizzy/parser_expr.cpp | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/fizzy/module.hpp b/lib/fizzy/module.hpp index 361ec2020..581db1c8c 100644 --- a/lib/fizzy/module.hpp +++ b/lib/fizzy/module.hpp @@ -68,19 +68,19 @@ struct Module return imported_global_types.size() + globalsec.size(); } + GlobalType get_global_type(GlobalIdx idx) const noexcept + { + assert(idx < get_global_count()); + return idx < imported_global_types.size() ? + imported_global_types[idx] : + globalsec[idx - imported_global_types.size()].type; + } + bool has_table() const noexcept { return !tablesec.empty() || !imported_table_types.empty(); } bool has_memory() const noexcept { return !memorysec.empty() || !imported_memory_types.empty(); } - - bool is_global_mutable(GlobalIdx idx) const noexcept - { - assert(idx < get_global_count()); - return idx < imported_global_types.size() ? - imported_global_types[idx].is_mutable : - globalsec[idx - imported_global_types.size()].type.is_mutable; - } }; } // namespace fizzy diff --git a/lib/fizzy/parser.cpp b/lib/fizzy/parser.cpp index c6b70ac5f..56cdc10ca 100644 --- a/lib/fizzy/parser.cpp +++ b/lib/fizzy/parser.cpp @@ -23,7 +23,7 @@ void validate_constant_expression(const ConstantExpression& const_expr, const Mo if (global_idx >= module.get_global_count()) throw validation_error{"invalid global index in constant expression"}; - if (module.is_global_mutable(global_idx)) + if (module.get_global_type(global_idx).is_mutable) throw validation_error{"constant expression can use global.get only for const globals"}; } } // namespace diff --git a/lib/fizzy/parser_expr.cpp b/lib/fizzy/parser_expr.cpp index c449d486f..08058eb2e 100644 --- a/lib/fizzy/parser_expr.cpp +++ b/lib/fizzy/parser_expr.cpp @@ -644,7 +644,7 @@ parser_result parse_expr(const uint8_t* pos, const uint8_t* end, uint32_t if (global_idx >= module.get_global_count()) throw validation_error{"accessing global with invalid index"}; - if (instr == Instr::global_set && !module.is_global_mutable(global_idx)) + if (instr == Instr::global_set && !module.get_global_type(global_idx).is_mutable) throw validation_error{"trying to mutate immutable global"}; push(code.immediates, global_idx); From 31c043366f425c1325596574686cef9d793d0b46 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Tue, 7 Jul 2020 12:22:22 +0200 Subject: [PATCH 2/2] test: global helpers of Module class --- circle.yml | 1 + test/unittests/CMakeLists.txt | 1 + test/unittests/module_test.cpp | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test/unittests/module_test.cpp diff --git a/circle.yml b/circle.yml index 0dec96c15..30e0f655c 100644 --- a/circle.yml +++ b/circle.yml @@ -173,6 +173,7 @@ jobs: ./wat2wasm4cpp.py test/unittests/execute_numeric_test.cpp ./wat2wasm4cpp.py test/unittests/execute_test.cpp ./wat2wasm4cpp.py test/unittests/instantiate_test.cpp + ./wat2wasm4cpp.py test/unittests/module_test.cpp ./wat2wasm4cpp.py test/unittests/parser_test.cpp ./wat2wasm4cpp.py test/unittests/validation_stack_test.cpp ./wat2wasm4cpp.py test/unittests/validation_test.cpp diff --git a/test/unittests/CMakeLists.txt b/test/unittests/CMakeLists.txt index c21e5e552..4593b6fa0 100644 --- a/test/unittests/CMakeLists.txt +++ b/test/unittests/CMakeLists.txt @@ -17,6 +17,7 @@ target_sources( execute_test.cpp instantiate_test.cpp leb128_test.cpp + module_test.cpp parser_expr_test.cpp parser_test.cpp span_test.cpp diff --git a/test/unittests/module_test.cpp b/test/unittests/module_test.cpp new file mode 100644 index 000000000..e24e16edc --- /dev/null +++ b/test/unittests/module_test.cpp @@ -0,0 +1,35 @@ +// Fizzy: A fast WebAssembly interpreter +// Copyright 2019-2020 The Fizzy Authors. +// SPDX-License-Identifier: Apache-2.0 + +#include "module.hpp" +#include "parser.hpp" +#include +#include + +using namespace fizzy; +using namespace fizzy::test; + +TEST(module, globals) +{ + /* wat2wasm + (global (import "m" "g1") (mut i32)) + (global (import "m" "g2") i64) + (global f32 (f32.const 0)) + (global (mut f64) (f64.const 1)) + */ + const auto bin = from_hex( + "0061736d01000000021102016d026731037f01016d026732037e000615027d0043000000000b7c014400000000" + "0000f03f0b"); + const auto module = parse(bin); + + ASSERT_EQ(module.get_global_count(), 4); + EXPECT_EQ(module.get_global_type(0).value_type, ValType::i32); + EXPECT_TRUE(module.get_global_type(0).is_mutable); + EXPECT_EQ(module.get_global_type(1).value_type, ValType::i64); + EXPECT_FALSE(module.get_global_type(1).is_mutable); + EXPECT_EQ(module.get_global_type(2).value_type, ValType::f32); + EXPECT_FALSE(module.get_global_type(2).is_mutable); + EXPECT_EQ(module.get_global_type(3).value_type, ValType::f64); + EXPECT_TRUE(module.get_global_type(3).is_mutable); +}