Skip to content

Commit

Permalink
Fix interp exception handling correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
SoniEx2 committed Oct 5, 2024
1 parent 7d229cf commit 6864626
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
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

0 comments on commit 6864626

Please sign in to comment.