From 7fdccf7147e7c33e5a13a9c099bdafbc4d3d62e2 Mon Sep 17 00:00:00 2001 From: "Soni L." Date: Tue, 8 Oct 2024 16:01:10 -0300 Subject: wasm-interp: Fix off-by-one in DoThrow (#2486) --- src/interp/interp.cc | 3 ++- test/regress/interp-throw-before-try.txt | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/regress/interp-throw-before-try.txt diff --git a/src/interp/interp.cc b/src/interp/interp.cc index cb5623d8..fe333b42 100644 --- a/src/interp/interp.cc +++ b/src/interp/interp.cc @@ -2615,7 +2615,8 @@ RunResult Thread::DoThrow(Exception::Ptr exn) { auto iter = handlers.rbegin(); while (iter != handlers.rend()) { const HandlerDesc& handler = *iter; - if (pc >= handler.try_start_offset && pc < handler.try_end_offset) { + // pc points to the *next* instruction by the time we're in DoThrow. + if (pc > handler.try_start_offset && pc <= handler.try_end_offset) { // For a try-delegate, skip part of the traversal by directly going // up to an outer handler specified by the delegate depth. if (handler.kind == HandlerKind::Delegate) { diff --git a/test/regress/interp-throw-before-try.txt b/test/regress/interp-throw-before-try.txt new file mode 100644 index 00000000..69ea3001 --- /dev/null +++ b/test/regress/interp-throw-before-try.txt @@ -0,0 +1,19 @@ +;;; TOOL: run-interp-spec +;;; ARGS*: --enable-exceptions +(module + (tag $e0) + (func (export "throws") + (throw $e0) + (try $try + (do + ) + (catch $e0) + ) + ) +) + +(assert_exception (invoke "throws")) +(;; STDOUT ;;; +out/test/regress/interp-throw-before-try.txt:15: assert_exception passed +2/2 tests passed. +;;; STDOUT ;;) -- cgit v1.2.3