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 2F77B6D466E; Wed, 22 Nov 2023 02:17:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2F77B6D466E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1700608676; bh=fHFVweRR8dOOgiVtEQp2I2zxl1SR8h3BNyN9wA0ffDY=; 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=lQEjQzQK8AZj1d2qdj7k/i1yI8r1zhgTHEYKsVdoY2gBe4Z8dtddEu/bVjsyisH18 4be4dgEKUgYwto/8h8/aoQTgarfUymPAoedxnATmUK3mwEOB3d5hMnIT+mP8Dxx6kr i1eYq/5SDAzVdRlMSaUg+nZT89RA6fTBI4UCK4CI= Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 EC9FC6D466E for ; Wed, 22 Nov 2023 02:17:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EC9FC6D466E Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-50a938dda08so8739362e87.3 for ; Tue, 21 Nov 2023 15:17:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700608624; x=1701213424; 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=ilqCf9xfi+1pnqnOakopJ3r4kDWC7avCSIHqvcwIz68=; b=OJMjjGFfJom9affDlwBJ+30VfwgeIQJw58TSMgQ1oL5Jps4tAmtZz89tXQOcLjiFMY Qe5tiUUdqUwHwN/c+40Kep0ZN7Y6LSxb5A+frRA03/gDDWj1ErgA6ziAFxRnR2Z1i9LH jxzlmfpZJ13mPI6ysyEJZ2hCg+TvJh6kP9YPewU7mK1U805pNpfDDBvLMsqsG8bOdnQh 1aVWr+NdB+sb0vsd2FQ4IPa1d9fF80yvc2PR5H+Ocnx9wilsVbb5OzlQe4CyTM3a1npS Pv5J6Ekg9lU2zP6nFzYTY/5mgkN1noscaSJknrf4rps2zbfasxGeQyDhPhgF1LtphKD1 qgZw== X-Gm-Message-State: AOJu0YxXOfEcoX0mQCutxx8blrauBbJK2DdG0nn+nBxCmamiDTPMwELb nGOZ6WpMH3nXO1F+0SOuw4hfidQyjDth+Q== X-Google-Smtp-Source: AGHT+IForWdXUaWQf7tuo8JW9hXSGoTfCPU2t4AUGlPf0qyl7t3O3P7LLJ0GKbrqxKF6wDMt7z8nww== X-Received: by 2002:a19:7b0a:0:b0:509:4c8a:525d with SMTP id w10-20020a197b0a000000b005094c8a525dmr442521lfc.35.1700608623793; Tue, 21 Nov 2023 15:17:03 -0800 (PST) Received: from localhost.localdomain (95-24-1-238.broadband.corbina.ru. [95.24.1.238]) by smtp.gmail.com with ESMTPSA id z1-20020a056512370100b004f755ceafbcsm1636371lfr.217.2023.11.21.15.17.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 21 Nov 2023 15:17:03 -0800 (PST) To: tarantool-patches@dev.tarantool.org, sergeyb@tarantool.org, skaplun@tarantool.org, m.kokryashkin@tarantool.org Date: Wed, 22 Nov 2023 02:16:46 +0300 Message-Id: <20231121231647.92696-3-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20231121231647.92696-1-max.kokryashkin@gmail.com> References: <20231121231647.92696-1-max.kokryashkin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2 2/3] Cleanup stack overflow handling. 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: Maksim Kokryashkin via Tarantool-patches Reply-To: Maksim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Reported by Peter Cawley. (cherry-picked from commit d2f6c55b05c716e5dbb479b7e684abaee7cf6e12) After the previous patch, it is possible to trigger the `stack overflow` error prematurely. Consider the following situation: there are already 33000 slots allocated on a Lua stack, and then there are 30 additional slots needed. In this case, the actual allocated amount would be twice the already allocated size, shrunk to the `LJ_STACK_MAXEX` size, which would lead to the stack overflow error, despite the fact there is plenty of unused space. This patch completely reworks the logic of error handling during stack growth to address the issue. Another important thing to notice is that the `LJ_ERR_STKOV` is thrown only if the `L->status` is `LUA_OK` and that status is set to `LUA_ERRRUN` just before throwing the error. The status is set to `LUA_ERRRUN` to avoid the second stack overflow during the `err_msgv` execution. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#9145 --- src/lj_state.c | 15 +++++-- .../lj-962-premature-stack-overflow.test.c | 41 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c diff --git a/src/lj_state.c b/src/lj_state.c index 76153bad..d8a5134c 100644 --- a/src/lj_state.c +++ b/src/lj_state.c @@ -121,8 +121,17 @@ void lj_state_shrinkstack(lua_State *L, MSize used) void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) { MSize n; - if (L->stacksize > LJ_STACK_MAXEX) /* Overflow while handling overflow? */ - lj_err_throw(L, LUA_ERRERR); + if (L->stacksize >= LJ_STACK_MAXEX) { + /* 4. Throw 'error in error handling' when we are _over_ the limit. */ + if (L->stacksize > LJ_STACK_MAXEX) + lj_err_throw(L, LUA_ERRERR); /* Does not invoke an error handler. */ + /* 1. We are _at_ the limit after the last growth. */ + if (!L->status) { /* 2. Throw 'stack overflow'. */ + L->status = LUA_ERRRUN; /* Prevent ending here again for pushed msg. */ + lj_err_msg(L, LJ_ERR_STKOV); /* May invoke an error handler. */ + } + /* 3. Add space (over the limit) for pushed message and error handler. */ + } n = L->stacksize + need; if (n > LJ_STACK_MAX) { n += 2*LUA_MINSTACK; @@ -132,8 +141,6 @@ void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) n = LJ_STACK_MAX; } resizestack(L, n); - if (L->stacksize >= LJ_STACK_MAXEX) - lj_err_msg(L, LJ_ERR_STKOV); } void LJ_FASTCALL lj_state_growstack1(lua_State *L) diff --git a/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c b/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c new file mode 100644 index 00000000..fd7557a7 --- /dev/null +++ b/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c @@ -0,0 +1,41 @@ +#include "lua.h" +#include "lauxlib.h" + +#include "test.h" +#include "utils.h" + +#include "lj_obj.h" +#include "luaconf.h" + +static int cur_slots = -1; + +static int fill_stack(lua_State *L) +{ + cur_slots = L->base - tvref(L->stack); + + while(lua_gettop(L) < LUAI_MAXSTACK) { + cur_slots += 1; + lua_pushinteger(L, 42); + } + + return 0; +} + +static int premature_stackoverflow(void *test_state) +{ + lua_State *L = test_state; + lua_cpcall(L, fill_stack, NULL); + assert_true(cur_slots == LUAI_MAXSTACK - 1); + return TEST_EXIT_SUCCESS; +} + +int main(void) +{ + lua_State *L = utils_lua_init(); + const struct test_unit tgroup[] = { + test_unit_def(premature_stackoverflow), + }; + const int test_result = test_run_group(tgroup, L); + utils_lua_close(L); + return test_result; +} -- 2.39.3 (Apple Git-145)