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 DC1C2690FEF; Wed, 1 Nov 2023 11:19:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DC1C2690FEF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1698826798; bh=PCceacMhkjj7FjvBMN+qEN4g28+nS3RZNcLL6HFYi+Q=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=zHXwdeueTxGQC2vqOi8asIgs91arm1hvT7QDtDNvbY+KeGtZjCcas+Jo3jTKiboNH G0TzVP2T+EsIhkHrAQwiFI8b987CLeiDpnnr24MRzxGT6DvEij3tdGhoF66I1Uxwwp EJWiGG0vQjNVxcENgVqeobOlfI4iBXgqRIDkQ6vg= Received: from smtp59.i.mail.ru (smtp59.i.mail.ru [95.163.41.97]) (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 4FC31690FEF for ; Wed, 1 Nov 2023 11:19:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4FC31690FEF Received: by smtp59.i.mail.ru with esmtpa (envelope-from ) id 1qy6S8-00D6kL-1Z; Wed, 01 Nov 2023 11:19:53 +0300 Date: Wed, 1 Nov 2023 11:15:21 +0300 To: Maksim Kokryashkin Message-ID: References: <20231027125738.29527-1-max.kokryashkin@gmail.com> <20231027125738.29527-2-max.kokryashkin@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231027125738.29527-2-max.kokryashkin@gmail.com> X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD94942643C2CB1DCCA79F1341554117E00C794A49C41DC4FAD182A05F5380850404C228DA9ACA6FE2743F4461F98049A0451CC74A579F6E14F00BEEEBCF3A94ED84DC3A9D9B513FDD2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE757AEC41D7AA04458EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AE01C1A20EF0A1348638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8A447B0EF3FF294151F849C5DFF993B87117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18F04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE140C956E756FBB7A4DB3626BA78294CCD8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE313BDA61BF53F5E1DC0837EA9F3D19764C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CD2DCF9CF1F528DBC2E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F616AD31D0D18CD5C156CCFE7AF13BCA4B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A5E6162D0B85D3F06FE7DAD46786C857F3BC9D70AD38F4B40CF87CCE6106E1FC07E67D4AC08A07B9B0735DFC8FA7AC1207CB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF8D32E41FD7C717CBAE119ADF254CE10CCA244D93C465F185AF225197A436F44FA54F2CFF063EE8C0D8551B728A548A9905075B98562996720F0C358C9E02A898A74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojixD2AZzOsmMDc4pi50XbIw== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769936D09D5C3E2981F51CC74A579F6E14F6ECF975C7BB2667FDEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 1/2] Print errors from __gc finalizers instead of rethrowing them. 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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Maksim! Thanks for the patch! Please, consider my comments below. On 27.10.23, Maksim Kokryashkin wrote: > From: Mike Pall > > Finalizers are not supposed to throw errors -- this is undefined behavior. > Lua 5.1 - 5.3 and (previously) LuaJIT rethrow the error. This randomly > breaks some unrelated code that just happens to do an allocation. Bad. > Lua 5.4 catches the error and emits a warning instead. But warnings are > not enabled by default, so it fails silently. Even worse. > LuaJIT (now) catches the error and emits a VM event. The default event > handler function prints "ERROR in finalizer: ...". > Set a custom handler function with: jit.attach(handler, "errfin") > > (cherry-picked from commit 1c279127050e86e99970100e9c42e0f09cd54ab7) > > The test for an error in a finalizer from the PUC-Rio suite is > disabled since these errors are now handled with VM event. I suppose, that it's worth to mention that there is a regression (and maybe short describe it) inside this commit, that will be fixed in the next commit. > > Sergey Kaplun: Typo: s/Sergey Kaplun/Maxim Kokryashkin/ > * added the test for the problem > > Part of tarantool/tarantool#9145 > --- > src/Makefile.dep.original | 14 +++---- > src/lib_aux.c | 33 ++++++++++++++- > src/lj_gc.c | 10 ++++- > src/lj_vmevent.h | 7 ++-- > test/PUC-Rio-Lua-5.1-tests/gc.lua | 12 +++--- > .../lj-946-print-errors-from-gc-fin.test.lua | 42 +++++++++++++++++++ > 6 files changed, 98 insertions(+), 20 deletions(-) > create mode 100644 test/tarantool-tests/lj-946-print-errors-from-gc-fin.test.lua > > diff --git a/src/Makefile.dep.original b/src/Makefile.dep.original > index d35b6d9a..53426a7e 100644 > --- a/src/Makefile.dep.original > +++ b/src/Makefile.dep.original > diff --git a/src/lib_aux.c b/src/lib_aux.c > index c40565c3..d532a12f 100644 > --- a/src/lib_aux.c > +++ b/src/lib_aux.c > @@ -21,6 +21,7 @@ > #include "lj_state.h" > #include "lj_trace.h" > #include "lj_lib.h" > +#include "lj_vmevent.h" > > #if LJ_TARGET_POSIX > #include > @@ -311,6 +312,18 @@ static int panic(lua_State *L) > return 0; > } > > +#ifndef LUAJIT_DISABLE_VMEVENT > +static int error_finalizer(lua_State *L) > +{ > + const char *s = lua_tostring(L, -1); > + fputs("ERROR in finalizer: ", stderr); > + fputs(s ? s : "?", stderr); > + fputc('\n', stderr); > + fflush(stderr); > + return 0; > +} > +#endif > + > #ifdef LUAJIT_USE_SYSMALLOC > > #if LJ_64 && !LJ_GC64 && !defined(LUAJIT_USE_VALGRIND) > @@ -332,7 +345,15 @@ static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) > LUALIB_API lua_State *luaL_newstate(void) > { > lua_State *L = lua_newstate(mem_alloc, NULL); > - if (L) G(L)->panic = panic; > + if (L) { > + G(L)->panic = panic; > +#ifndef LUAJIT_DISABLE_VMEVENT > + luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE); > + lua_pushcfunction(L, error_finalizer); > + lua_rawseti(L, -2, VMEVENT_HASH(LJ_VMEVENT_ERRFIN)); > + G(L)->vmevmask = VMEVENT_MASK(LJ_VMEVENT_ERRFIN); > +#endif > + } > return L; > } > > @@ -350,7 +371,15 @@ LUALIB_API lua_State *luaL_newstate(void) > #else > L = lua_newstate(lj_alloc_f, ud); > #endif > - if (L) G(L)->panic = panic; > + if (L) { > + G(L)->panic = panic; > +#ifndef LUAJIT_DISABLE_VMEVENT > + luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE); > + lua_pushcfunction(L, error_finalizer); > + lua_rawseti(L, -2, VMEVENT_HASH(LJ_VMEVENT_ERRFIN)); > + G(L)->vmevmask = VMEVENT_MASK(LJ_VMEVENT_ERRFIN); > +#endif > + } > return L; > } Side note: Yes, of course there is no need to declare another routine for this... Copy-past is better. Sigh... Please mention when (at the moment of Lua initialization) the default VM handler is set. > > diff --git a/src/lj_gc.c b/src/lj_gc.c > index 19d4c963..591862b3 100644 > --- a/src/lj_gc.c > +++ b/src/lj_gc.c > @@ -27,6 +27,7 @@ > #include "lj_trace.h" > #include "lj_dispatch.h" > #include "lj_vm.h" > +#include "lj_vmevent.h" > > #define GCSTEPSIZE 1024u > #define GCSWEEPMAX 40 > @@ -515,8 +516,13 @@ static void gc_call_finalizer(global_State *g, lua_State *L, > hook_restore(g, oldh); > if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g); > g->gc.threshold = oldt; /* Restore GC threshold. */ > - if (errcode) > - lj_err_throw(L, errcode); /* Propagate errors. */ > + if (errcode) { > + ptrdiff_t errobj = savestack(L, L->top-1); /* Stack may be resized. */ > + lj_vmevent_send(L, ERRFIN, > + copyTV(L, L->top++, restorestack(L, errobj)); > + ); > + L->top--; > + } > } > > /* Finalize one userdata or cdata object from the mmudata list. */ > diff --git a/src/lj_vmevent.h b/src/lj_vmevent.h > index 050fb4dd..56c0d798 100644 > --- a/src/lj_vmevent.h > +++ b/src/lj_vmevent.h > @@ -24,9 +24,10 @@ > /* VM event IDs. */ > typedef enum { > VMEVENT_DEF(BC, 0x00003883), > - VMEVENT_DEF(TRACE, 0xb2d91467), > - VMEVENT_DEF(RECORD, 0x9284bf4f), > - VMEVENT_DEF(TEXIT, 0xb29df2b0), > + VMEVENT_DEF(TRACE, 0x12d91467), > + VMEVENT_DEF(RECORD, 0x1284bf4f), > + VMEVENT_DEF(TEXIT, 0x129df2b0), > + VMEVENT_DEF(ERRFIN, 0x12d93888), > LJ_VMEVENT__MAX > } VMEvent; AFAICS, Mike removes the high bits of these hashes since they are scratched via the bitwise operation `(hash)<<3` in `VMEVENT_DEF` anyway. Please mention this in the commit message. > > diff --git a/test/PUC-Rio-Lua-5.1-tests/gc.lua b/test/PUC-Rio-Lua-5.1-tests/gc.lua > index 10e1f2dd..05ac0d11 100644 > --- a/test/PUC-Rio-Lua-5.1-tests/gc.lua > +++ b/test/PUC-Rio-Lua-5.1-tests/gc.lua > @@ -260,13 +260,13 @@ u, m = nil > collectgarbage() > assert(m==10) > > - > +-- The test below is disabled for LuaJIT, since it handles errors from > +-- GC finalizers via VM event. > -- errors during collection > -u = newproxy(true) > -getmetatable(u).__gc = function () error "!!!" end > -u = nil > -assert(not pcall(collectgarbage)) > - > +-- u = newproxy(true) > +-- getmetatable(u).__gc = function () error "!!!" end > +-- u = nil > +-- assert(not pcall(collectgarbage)) > Maybe its better just to setup the "errfin" handler to `error()` function? > if not rawget(_G, "_soft") then > print("deep structures") > diff --git a/test/tarantool-tests/lj-946-print-errors-from-gc-fin.test.lua b/test/tarantool-tests/lj-946-print-errors-from-gc-fin.test.lua > new file mode 100644 > index 00000000..cce417d2 > --- /dev/null > +++ b/test/tarantool-tests/lj-946-print-errors-from-gc-fin.test.lua > @@ -0,0 +1,42 @@ > +local tap = require('tap') > +local test = tap.test('lj-946-print-errors-from-gc-fin'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +test:plan(2) > + > +local ffi = require('ffi') > +local error_in_finalizer = false > + > +local function errfin_handler() > + error_in_finalizer = true > +end Is it better just to add `test:ok(true, 'error handler called')` here? > + > +local function new_bad_cdata() > + return ffi.gc(ffi.new('char [?]', 1024), 'uncallable string') > +end > + > +local function test_f() > + collectgarbage('collect') > + -- Make GC aggressive enough to end atomic phase before Typo: s/atomic/the atomic/ > + -- exit from the trace. Typo: s/exit from/exiting/ > + collectgarbage('setstepmul', 400) > + -- Number of iterations is empirical, just big enough for the Typo: s/Number/The number/ > + -- issue to strike. > + for _ = 1, 4000 do > + new_bad_cdata() > + end > +end > + > +jit.opt.start('hotloop=1') > +-- Handler is registered, but never called before the patch, Typo: s/registered,/registered/ > +-- but should be called after the patch. Typo: s/patch, but/patch. It/ > +jit.attach(errfin_handler, 'errfin') > +local status = pcall(test_f) > +-- We have to stop GC now because any step raises the error due to > +-- cursed cdata objects. > +collectgarbage('stop') > +test:ok(status, 'test function completed successfully') > +test:ok(error_in_finalizer, 'error handler called') > + > +test:done(true) I suggest to test the default handler too, within the separate test. Also, maybe we should test other cases (error function (in default case), function with tailcall to error, etc.), any ideas about them? > -- > 2.39.3 (Apple Git-145) > -- Best regards, Sergey Kaplun