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 04169642394; Thu, 28 Sep 2023 20:11:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 04169642394 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1695921060; bh=2F+12bBkmJmV7wEQE0UOcBnY/hGpzeVXV1e4iRDY6Ik=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ZmaVYAsePm2OmAB6wW8nzVTdxvv2d4AajmJHGfavaw9aF6Ls1jd3oSwdqaKKmiU+r mK7fYSbd8z5AhUQSpaErUnorqlyICaCTA3HTRn5Z5NRqc/iYeEUvhgUra/jN/Qarps wZ85AS1G2YswykjXUASa3BIikyRfFlj4rXyUOtA4= Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 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 35F4664238D for ; Thu, 28 Sep 2023 20:09:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 35F4664238D Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-50305abe5f0so20838443e87.2 for ; Thu, 28 Sep 2023 10:09:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695920946; x=1696525746; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ONCeo9+jcMswKHQuCRFXERpUHX8MzS+HWAkyllFaDO8=; b=MJoRuPSEJx5TXMaTdWzYL4qBsQdRfpyCfyD+ffOkXwXc+f4Xsjxr4CSGE9yvSuL41T 0LUXDWvKRu9wi4C6lKXZLJfq/GtL/jqwD2xa4fgGMYc1stCVODTgVR1Eb0qiXh4rQpO1 +IwahYPjY312Wg81Qzj62qj9aE3XtvxgNaH5Wh32Hp2N/ybJJK70Q77nvas7JBr/9th0 xPJ1mMK/toh8q6FBuxbRyjUg2qle9B351+GTkw1ttyLUt2fcuw3Y6lJJCVXkzRKSJZOR 5q+HmHOFWV7AMi3QeVBidA31PBfRfjGYXo3dsDpvZO45PSxt2F220vBFVrff27vPWtDs HNjw== X-Gm-Message-State: AOJu0YzXyO5QtaqBMzEMPOVjn3Phqsi427UigxkzB4ysIYup6AFYzjgx qlDDFLGF26ioXr6RL+tvcpn0Vd1zWHI= X-Google-Smtp-Source: AGHT+IHlQwCF7CzL4PwBjDOcyg0a4lthHDwaB046tLqLjgvob8VBP0U1UIeCMfHLNxLNl7GQ/32DUQ== X-Received: by 2002:a05:6512:3134:b0:502:fe11:a694 with SMTP id p20-20020a056512313400b00502fe11a694mr1651087lfd.45.1695920946224; Thu, 28 Sep 2023 10:09:06 -0700 (PDT) Received: from localhost.localdomain (95-24-2-172.broadband.corbina.ru. [95.24.2.172]) by smtp.gmail.com with ESMTPSA id bi4-20020a0565120e8400b005044911b328sm2301792lfb.218.2023.09.28.10.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 10:09:05 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Thu, 28 Sep 2023 20:08:49 +0300 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 4/4] 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: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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. 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 for the problem Resolves tarantool/tarantool#6323 --- src/lj_err.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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); -- 2.42.0