summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-02-26 21:21:10 +0100
committerJo-Philipp Wich <jo@mein.io>2021-02-26 21:29:00 +0100
commit9ad9afb24d90cee643c35e254de0b1c734e44940 (patch)
tree71b2d7eeeebb332a1183c0f5ca7245b1ae77a0a0
parent14aace988c32faa1829b6839825ab40d8efa3968 (diff)
compiler: fix try/catch miscompilation
When skipping catch blocks with exception variables, jump beyond the instruction popping the exception variable off the stack to fix a stack position mismatch between compiler and vm. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--compiler.c4
-rw-r--r--tests/03_bugs/01_try_catch_stack_mismatch28
2 files changed, 30 insertions, 2 deletions
diff --git a/compiler.c b/compiler.c
index 3c6f6e1..436f91d 100644
--- a/compiler.c
+++ b/compiler.c
@@ -2485,9 +2485,9 @@ uc_compiler_compile_try(uc_compiler *compiler)
uc_compiler_parse_consume(compiler, TK_RBRACE);
- uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count);
-
uc_compiler_leave_scope(compiler);
+
+ uc_compiler_set_jmpaddr(compiler, jmp_off, chunk->count);
}
static void
diff --git a/tests/03_bugs/01_try_catch_stack_mismatch b/tests/03_bugs/01_try_catch_stack_mismatch
new file mode 100644
index 0000000..ae7c760
--- /dev/null
+++ b/tests/03_bugs/01_try_catch_stack_mismatch
@@ -0,0 +1,28 @@
+When compiling a try/catch statement with an exception variable, the catch
+skip jump incorrectly pointed to the POP instruction popping the exception
+variable off the stack, leading to a stack position mismatch between
+compiler and vm, causing local variables to yield wrong values at runtime.
+
+-- Expect stdout --
+1
+-- End --
+
+-- Testcase --
+{%
+ function f() {
+ let x;
+
+ try {
+ x = 1;
+ }
+ catch(e) {
+
+ }
+
+ // Before the fix, `x` incorrectly yielded the print function value
+ print(x, "\n");
+ }
+
+ f()
+%}
+-- End --