From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id A34F469A6C0; Tue, 3 Oct 2023 22:16:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A34F469A6C0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1696360604; bh=VRjioSd0uHSIZUqtqcsgz22mP/AdPHgIwhKFKkQ9RFs=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JaS6Ss/gO/gyei/APvbjFaxyd7cumljA/fCbCbm4y7Bs63ZHpkExM/mWkUneQPdu1 nLj9SHLbzlVmwoQgdbihtQnc1jc1PJVZfW06rpJJURL+Xwlo8Pwq+Zvm9o0tRwhoNL W8QPUJWOc0aWm/2Xc3z8GODZgbckN7BAQ/C38Zq0= Received: from smtp47.i.mail.ru (smtp47.i.mail.ru [95.163.41.85]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id EEF4868CACB for ; Tue, 3 Oct 2023 22:16:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EEF4868CACB Received: by smtp47.i.mail.ru with esmtpa (envelope-from ) id 1qnkss-009SSX-2z; Tue, 03 Oct 2023 22:16:43 +0300 Message-ID: <9a13eea1-4807-916c-2e34-873237743f97@tarantool.org> Date: Tue, 3 Oct 2023 22:16:42 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Content-Language: en-US To: Maxim Kokryashkin , tarantool-patches@dev.tarantool.org, skaplun@tarantool.org References: <839195a6a6ff32a49c5a690a9f0101cc741901fd.1695968227.git.m.kokryashkin@tarantool.org> In-Reply-To: <839195a6a6ff32a49c5a690a9f0101cc741901fd.1695968227.git.m.kokryashkin@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD94A796AF4779ECFCEACF6BA3155450C7D172A2DC375030A1C182A05F53808504078BC377131D9CAE8F9D8F82DDA9DDA3C5AF0D237A51F9D4DC9DB5A89E2A0188C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE72AC9FB60380F23AEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CF58BB58AE3180708638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D74ED2CC95154A657D3F3BCDEF767257117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC1F8789D36234D406A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FBDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE9647ADFADE5905B1CAA9C333723BB66AD8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE34E7D9683544204AF6E0066C2D8992A16C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637C3090DF2C0002BD1EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A51781AF70D73950E17CB04234CFD0F191023B1F181720444CF87CCE6106E1FC07E67D4AC08A07B9B0735DFC8FA7AC1207CB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF977F12818B7E52E96C04E0AE544834CDAF722F94C23693D2EDB1A747370E678404D7482423E9A0B8CC231B5D63B17C6BB32CF3CB8C74D9E9A51DD62BCF29D353E48CAC7CA610320002C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojH0UbTrDsBkBM66NrCDbO6Q== X-Mailru-Sender: C4F68CFF4024C8867DFDF7C7F25884585830D1DC627F3432A448EB71720B64D644AE72278D3525BA282EC151BADDC1D3523A6D01B4765B2DFB59E2DDD9FE06B14FA522850F29BC30B0DAF586E7D11B3E67EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit v2 5/5] Restore cur_L for specific Lua/C API use case. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Max thanks for the patch! See my comments. On 9/29/23 09:20, Maxim Kokryashkin wrote: > From: Mike Pall > > Thanks to Peter Cawley. > > (cherry-picked from commit e86990f7f24a94b0897061f25a84547fe1108bed) > > Consider the following Lua C API function: > > ``` > static int error_after_coroutine_return(lua_State *L) > { > lua_State *innerL = lua_newthread(L); > luaL_loadstring(innerL, "print('inner coro')"); > lua_pcall(innerL, 0, 0, 0); > luaL_error(L, "my fancy error"); > return 0; > } > ``` > > And the following Lua script: > ``` > local libcur_L = require('libcur_L') > > local function onesnap_f(var) > if var then > return 1 > else > return 0 > end > end > > -- Compile function to trace with snapshot. > if jit then jit.opt.start('hotloop=1') end > onesnap_f(true) > onesnap_f(true) > > local r, s = pcall(libcur_L.error_after_coroutine_return) > onesnap_f(false) > ``` > > This is the only case when `cur_L` is not restored, according to > the analysis done in https://github.com/LuaJIT/LuaJIT/issues/1066. Please remove link to the issue from commit message, this could be a reason of spam pings in the issue itself. > > This patch changes the error-catching routine, so now the patch > sets the actual cur_L there. > Now it is possible to throw errors on non-executing coroutines, > which is a violation of the Lua C API. So, even though it is now > possible, that behavior should be avoided anyway. > > Maxim Kokryashkin: > * added the description and the test for the problem > > Resolves tarantool/tarantool#6323 > --- > src/lj_err.c | 5 ++- > test/tarantool-tests/CMakeLists.txt | 1 + > ...-fix-cur_L-after-coroutine-resume.test.lua | 32 +++++++++++++++++++ > .../CMakeLists.txt | 1 + > .../libcur_L_coroutine.c | 22 +++++++++++++ > 5 files changed, 60 insertions(+), 1 deletion(-) > create mode 100644 test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume.test.lua > create mode 100644 test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/CMakeLists.txt > create mode 100644 test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/libcur_L_coroutine.c > > diff --git a/src/lj_err.c b/src/lj_err.c > index 46fb81ee..1a9a2f2b 100644 > --- a/src/lj_err.c > +++ b/src/lj_err.c > @@ -174,12 +174,15 @@ static void *err_unwind(lua_State *L, void *stopcf, int errcode) > case FRAME_PCALL: /* FF pcall() frame. */ > case FRAME_PCALLH: /* FF pcall() frame inside hook. */ > if (errcode) { > + global_State *g; > if (errcode == LUA_YIELD) { > frame = frame_prevd(frame); > break; > } > + g = G(L); > + setgcref(g->cur_L, obj2gco(L)); > if (frame_typep(frame) == FRAME_PCALL) > - hook_leave(G(L)); > + hook_leave(g); > L->base = frame_prevd(frame) + 1; > L->cframe = cf; > unwindstack(L, L->base); > diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt > index c15d6037..d84072e0 100644 > --- a/test/tarantool-tests/CMakeLists.txt > +++ b/test/tarantool-tests/CMakeLists.txt > @@ -68,6 +68,7 @@ add_subdirectory(lj-727-lightuserdata-itern) > add_subdirectory(lj-802-panic-at-mcode-protfail) > add_subdirectory(lj-flush-on-trace) > add_subdirectory(lj-1004-oom-error-frame) > +add_subdirectory(lj-1066-fix-cur_L-after-coroutine-resume) > > # The part of the memory profiler toolchain is located in tools > # directory, jit, profiler, and bytecode toolchains are located > diff --git a/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume.test.lua b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume.test.lua > new file mode 100644 > index 00000000..3919ae23 > --- /dev/null > +++ b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume.test.lua > @@ -0,0 +1,32 @@ > +local tap = require('tap') > +local test = tap.test('lj-1066-fix-cur_L-after-coroutine-resume'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(1) > + > +local libcur_L_coroutine = require('libcur_L_coroutine') > + > +local function cbool(cond) > + if cond then > + return 1 > + else > + return 0 > + end > +end > + > +-- Compile function to trace with snapshot. > +jit.opt.start('hotloop=1') > +-- First call makes `cbool()` hot enough to be recorded next time. > +cbool(true) > +-- Second call records `cbool()` body (i.e. `if` branch). This is > +-- a root trace for `cbool()`. > +cbool(true) > + > +local res = pcall(libcur_L_coroutine.error_after_coroutine_return) > +assert(res == false, "return from error") > +-- Call with restoration from a snapshot with wrong cur_L. > +cbool(false) > + > +test:ok(true) > +test:done(true) > diff --git a/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/CMakeLists.txt b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/CMakeLists.txt > new file mode 100644 > index 00000000..c8a3731f > --- /dev/null > +++ b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/CMakeLists.txt > @@ -0,0 +1 @@ > +BuildTestCLib(libcur_L_coroutine libcur_L_coroutine.c) > diff --git a/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/libcur_L_coroutine.c b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/libcur_L_coroutine.c > new file mode 100644 > index 00000000..7a71d0f0 > --- /dev/null > +++ b/test/tarantool-tests/lj-1066-fix-cur_L-after-coroutine-resume/libcur_L_coroutine.c > @@ -0,0 +1,22 @@ > +#include "lua.h" > +#include "lauxlib.h" > + > +static int error_after_coroutine_return(lua_State *L) > +{ > + lua_State *innerL = lua_newthread(L); > + luaL_loadstring(innerL, "print('inner coro')"); > + lua_pcall(innerL, 0, 0, 0); > + luaL_error(L, "my fancy error"); > + return 0; > +} > + > +static const struct luaL_Reg libcur_L_coroutine[] = { > + {"error_after_coroutine_return", error_after_coroutine_return}, > + {NULL, NULL} > +}; > + > +LUA_API int luaopen_libcur_L_coroutine(lua_State *L) > +{ > + luaL_register(L, "libcur_L_coroutine", libcur_L_coroutine); > + return 1; > +}