Skip to content
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 include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result BeginFunctionBody(Index index, Offset size) override;
Result OnLocalDeclCount(Index count) override;
Result OnLocalDecl(Index decl_index, Index count, Type type) override;
Result EndLocalDecls() override;

Result OnOpcode(Opcode opcode) override;
Result OnOpcodeBare() override;
Expand Down
1 change: 1 addition & 0 deletions include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnLocalDecl(Index decl_index, Index count, Type type) override {
return Result::Ok;
}
Result EndLocalDecls() override { return Result::Ok; }

/* Function expressions; called between BeginFunctionBody and
EndFunctionBody */
Expand Down
1 change: 1 addition & 0 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class BinaryReaderDelegate {
virtual Result BeginFunctionBody(Index index, Offset size) = 0;
virtual Result OnLocalDeclCount(Index count) = 0;
virtual Result OnLocalDecl(Index decl_index, Index count, Type type) = 0;
virtual Result EndLocalDecls() = 0;

/* Function expressions; called between BeginFunctionBody and
EndFunctionBody */
Expand Down
1 change: 1 addition & 0 deletions src/binary-reader-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ DEFINE_BEGIN(BeginCodeSection)
DEFINE_INDEX(OnFunctionBodyCount)
DEFINE_INDEX(EndFunctionBody)
DEFINE_INDEX(OnLocalDeclCount)
DEFINE0(EndLocalDecls)
DEFINE_LOAD_STORE_OPCODE(OnAtomicLoadExpr);
DEFINE_LOAD_STORE_OPCODE(OnAtomicRmwExpr);
DEFINE_LOAD_STORE_OPCODE(OnAtomicRmwCmpxchgExpr);
Expand Down
1 change: 1 addition & 0 deletions src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,7 @@ Result BinaryReader::ReadCodeSection(Offset section_size) {
ERROR_UNLESS(IsConcreteType(local_type), "expected valid local type");
CALLBACK(OnLocalDecl, k, num_local_types, local_type);
}
CALLBACK(EndLocalDecls);

if (options_.skip_function_bodies) {
state_.offset = end_offset;
Expand Down
32 changes: 19 additions & 13 deletions src/interp/binary-reader-interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class BinaryReaderInterp : public BinaryReaderNop {
Result BeginFunctionBody(Index index, Offset size) override;
Result OnLocalDeclCount(Index count) override;
Result OnLocalDecl(Index decl_index, Index count, Type type) override;
Result EndLocalDecls() override;

Result OnOpcode(Opcode Opcode) override;
Result OnAtomicLoadExpr(Opcode opcode,
Expand Down Expand Up @@ -849,17 +850,6 @@ Result BinaryReaderInterp::EndFunctionBody(Index index) {
Result BinaryReaderInterp::OnLocalDeclCount(Index count) {
local_decl_count_ = count;
local_count_ = 0;
// Continuation of the implicit func label, used for exception handling. (See
// BeginFunctionBody.)
// We need the local count for this, so we must do it here.
// NOTE: we don't count the parameters, as they're not part of the frame.
func_->handlers.push_back(HandlerDesc{HandlerKind::Catch,
istream_.end(),
Istream::kInvalidOffset,
{},
{Istream::kInvalidOffset},
static_cast<u32>(local_decl_count_),
0});
return Result::Ok;
}

Expand All @@ -870,10 +860,26 @@ Result BinaryReaderInterp::OnLocalDecl(Index decl_index,

local_count_ += count;
func_->locals.push_back(LocalDesc{type, count, local_count_});
return Result::Ok;
}

if (decl_index == local_decl_count_ - 1) {
Result BinaryReaderInterp::EndLocalDecls() {
if (local_count_ != 0) {
istream_.Emit(Opcode::InterpAlloca, local_count_);
}
// Continuation of the implicit func label, used for exception handling. (See
// BeginFunctionBody.)
// We need the local count for this, which is only available after processing
// all local decls.
// NOTE: we don't count the parameters, as they're not part of the frame.
func_->handlers.push_back(HandlerDesc{HandlerKind::Catch,
istream_.end(),
Istream::kInvalidOffset,
{},
{Istream::kInvalidOffset},
static_cast<u32>(local_count_),
0});

return Result::Ok;
}

Expand Down Expand Up @@ -1522,7 +1528,7 @@ Result BinaryReaderInterp::OnTryExpr(Type sig_type) {
validator_.GetCatchCount(label_stack_.size() - 1, &exn_stack_height));
// NOTE: *NOT* GetLocalCount. we don't count the parameters, as they're not
// part of the frame.
u32 value_stack_height = validator_.type_stack_size() + local_decl_count_;
u32 value_stack_height = validator_.type_stack_size() + local_count_;
CHECK_RESULT(validator_.OnTry(GetLocation(), sig_type));
// Push a label that tracks mapping of exn -> catch
PushLabel(LabelKind::Try, Istream::kInvalidOffset, Istream::kInvalidOffset,
Expand Down
1 change: 1 addition & 0 deletions test/interp/basic-logging.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ BeginModule(version: 1)
OnFunctionBodyCount(1)
BeginFunctionBody(0, size:5)
OnLocalDeclCount(0)
EndLocalDecls
OnI32ConstExpr(42 (0x2a))
OnReturnExpr
OnEndExpr
Expand Down
18 changes: 15 additions & 3 deletions test/regress/interp-ehv3-locals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;;; NOTE: ref: issue-2476
(module
(tag $e0)
(func (export "broken-local") (result i32)
(func (export "set-local") (result i32)
(local $value i32)
(try $try
(do
Expand All @@ -14,9 +14,21 @@
)
(local.get $value)
)
(func (export "multiple-locals") (result i32)
(local $a i32)
(local $b i32)
(try $try
(do
(throw $e0)
)
(catch $e0)
)
(local.get $a)
)
)

(assert_return (invoke "broken-local") (i32.const 1))
(assert_return (invoke "set-local") (i32.const 1))
(assert_return (invoke "multiple-locals") (i32.const 0))
(;; STDOUT ;;;
2/2 tests passed.
3/3 tests passed.
;;; STDOUT ;;)
Loading