Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module helper for finding global type #411

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/fizzy/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fizzy/parser_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ parser_result<Code> 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);
Expand Down
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/unittests/module_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#include <test/utils/hex.hpp>

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);
}