[Tarantool-patches] [PATCH luajit v2] Fix frame traversal for __gc handler frames.

Igor Munkin imun at tarantool.org
Tue Nov 23 15:57:38 MSK 2021


Sergey,

Thanks for the fixes! LGTM, with a tiny nit.

On 19.11.21, Sergey Kaplun wrote:
> From: Mike Pall <mike>
> 
> Reported by Changochen.
> 
> (cherry picked from 53f82e6e2e858a0a62fd1a2ff47e9866693382e6)
> 
> A cframe unwinding is missed for a C protected frame during a search for
> an error function to handle a runtime error. It leads to undefined
> behaviour or crash, when raising a runtime error on stack with the CP
> frame before an error function handler (for example, an error in __gc
> handler).
> 
> This patch adds missing unwinding for CP frame.
> 
> Sergey Kaplun:
> * added the description and the test for the problem
> ---
> 
> LuaJIT issue: https://github.com/LuaJIT/LuaJIT/issues/601
> Branch: https://github.com/tarantool/luajit/tree/skaplun/gh-noticket-fix-gc-finderrfunc
> Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-fix-gc-finderrfunc
> 
> Changes in v2:
> * Create CP and C stack manually in LuaC
> 
>  src/lj_err.c                                  |  1 +
>  test/tarantool-tests/CMakeLists.txt           |  1 +
>  .../lj-601-fix-gc-finderrfunc.test.lua        | 35 ++++++++++++
>  .../lj-601-fix-gc-finderrfunc/CMakeLists.txt  |  1 +
>  .../lj-601-fix-gc-finderrfunc/mixcframe.c     | 55 +++++++++++++++++++
>  5 files changed, 93 insertions(+)
>  create mode 100644 test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
>  create mode 100644 test/tarantool-tests/lj-601-fix-gc-finderrfunc/CMakeLists.txt
>  create mode 100644 test/tarantool-tests/lj-601-fix-gc-finderrfunc/mixcframe.c
> 

<snipped>

> diff --git a/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
> new file mode 100644
> index 00000000..d4c44489
> --- /dev/null
> +++ b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
> @@ -0,0 +1,35 @@

<snipped>

> +local a = newproxy(true)
> +getmetatable(a).__gc = function()
> +  -- Function to raise error via `lj_err_run()` inside __gc.
> +  error('raise error in __gc')
> +end
> +-- luacheck: no unused
> +a = nil
> +
> +-- We need to get the following Lua stack format when raise an
> +-- error:
> +-- + L->stack
> +-- | ...
> +-- | CP -- any C protected frame.
> +-- | ...[L/LP/V]...
> +-- | C -- any C frame.
> +-- | ...[L/LP/V]...
> +-- | CP (with inherited errfunc) -- __gc frame.
> +-- V
> +-- Enter in the C to call CP func. Call `lua_gc()` inside.

Minor: I'd adjust this comment the following way:
| -- Enter in the C land to call a function in a protected C frame
| -- (CP). Spoil host stack (and ergo cframe area) and later call
| -- Lua C function, triggering full GC cycle in a non-protected
| -- frame. As a result, error is raised in __gc metamethod above.

Fixed, squashed, force-pushed to the branch. Diff is below:

================================================================================

diff --git a/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
index d4c44489..2122c7a0 100644
--- a/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
+++ b/test/tarantool-tests/lj-601-fix-gc-finderrfunc.test.lua
@@ -29,7 +29,10 @@ a = nil
 -- | ...[L/LP/V]...
 -- | CP (with inherited errfunc) -- __gc frame.
 -- V
--- Enter in the C to call CP func. Call `lua_gc()` inside.
+-- Enter in the C land to call a function in a protected C frame
+-- (CP). Spoil host stack (and ergo cframe area) and later call
+-- Lua C function, triggering full GC cycle in a non-protected
+-- frame. As a result, error is raised in __gc metamethod above.
 test:ok(mixcframe.test_handle_err(), 'error in __gc is successfully handled')
 
 os.exit(test:check() and 0 or 1)

================================================================================

> +test:ok(mixcframe.test_handle_err(), 'error in __gc is successfully handled')
> +
> +os.exit(test:check() and 0 or 1)

<snipped>

> -- 
> 2.31.0
> 

-- 
Best regards,
IM


More information about the Tarantool-patches mailing list